Implement php code parser

This commit is contained in:
Dennis Eichhorn 2016-03-28 13:58:42 +02:00
parent 96a9cf57bf
commit 6255bd9ab2
7 changed files with 744 additions and 176 deletions

View File

@ -1,176 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\System;
/**
* Array parser class.
*
* Parsing/serializing arrays to and from php file
*
* @category System
* @package Framework
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class ArrayParser
{
/**
* File path.
*
* @var string
* @since 1.0.0
*/
public $file = null;
/**
* File path.
*
* @var array
* @since 1.0.0
*/
public $array = null;
/**
* Constructor.
*
* @param string $file File path
* @param string $arr_name Array to parse
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct(string $file, string $arr_name)
{
if (file_exists($file)) {
$this->file = $file;
/** @noinspection PhpIncludeInspection */
include $this->file;
if (isset(${$arr_name})) {
$this->array = ${$arr_name};
}
}
}
/**
* Set or add new value.
*
* @param mixed $id Array ID to add/edit
* @param mixed $val Value to add/insert
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function set($id, $val)
{
$this->array[$id] = $val;
}
/**
* Remove value.
*
* @param mixed $id Array ID to add/edit
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function delete($id)
{
unset($this->array[$id]);
}
/**
* Saving array to file.
*
* @param string $name Name of new array
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function save(string $name)
{
$arr = '<' . '?php' . PHP_EOL
. '$' . $name . ' = '
. $this->serializeArray($this->array)
. ';';
file_put_contents($this->file, $arr);
}
/**
* Serializing array (recursively).
*
* @param array $arr Array to serialize
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function serializeArray(array $arr) : string
{
$stringify = '[' . PHP_EOL;
foreach ($arr as $key => $val) {
if(is_string($key)) {
$key = '"' . $key . '"';
}
$stringify .= ' ' . $key . ' => ' . $this->arrayifyValue($val). ',' . PHP_EOL;
}
return $stringify . ']';
}
/**
* Serialize array value.
*
* @param mixed $value Value to serialzie
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private function arrayifyValue($value) : string
{
if(is_array($value)) {
return '[' . PHP_EOL . $this->serializeArray($value) . PHP_EOL . ']';
} elseif(is_string($value)) {
return '"' . $value . '"';
} elseif(is_scalar($value)) {
return $value;
} elseif(is_null($value)) {
return 'null';
} elseif($value instanceOf \Serializable) {
return $this->arrayifyValue($value->serialize());
} else {
throw new \UnexpectedValueException();
}
}
}

View File

@ -0,0 +1,78 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Utils\Parser\Php;
/**
* Array parser class.
*
* Parsing/serializing arrays to and from php file
*
* @category System
* @package Framework
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class ArrayParser
{
/**
* Saving array to file.
*
* @param string $name Name of new array
* @param array $arr Array to parse
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function createFile(string $name, array $arr) : string
{
$out = '<' . '?php' . PHP_EOL
. '$' . $name . ' = ' . self::serializeArray($this->arr) . ';';
return $out;
}
/**
* Serializing array (recursively).
*
* @param array $arr Array to serialize
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function serializeArray(array $arr) : string
{
$stringify = '[' . PHP_EOL;
foreach ($arr as $key => $val) {
if(is_string($key)) {
$key = '"' . $key . '"';
}
$stringify .= ' ' . $key . ' => ' . MemberParser::parseVariable($val). ',' . PHP_EOL;
}
return $stringify . ']';
}
}

View File

@ -0,0 +1,308 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Utils\Parser\Php;
/**
* Array parser class.
*
* Parsing/serializing arrays to and from php file
*
* @category System
* @package Framework
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class ClassParser
{
private $isFinal = false;
private $isAbstract = false;
private $type = ClassType::_CLASS;
private $extends = null;
private $namespace = null;
private $includes = [];
private $requires = [];
private $use = [];
private $name = '';
private $implements = [];
private $traits = [];
private $members = [];
private $functions = [];
public function __construct() {}
/**
* Saving class to file.
*
* @param string $path Path
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function createFile(string $path)
{
}
public function setFinal(bool $final)
{
$this->isFinal = $final;
}
public function isFinal() : bool
{
return $this->isFinal;
}
public function setAbstract(bool $abstract)
{
$this->isAbstract = $abstract;
}
public function isAbstract() : bool
{
return $this->isAbstract;
}
public function setType(string $type)
{
$this->type = $type;
}
public function getType() : string
{
return $this->type;
}
public function setExtends(string $extends)
{
$this->extends = $extends;
}
public function getExtends() : string
{
return $this->extends;
}
public function removeExtends()
{
$this->extends = null;
}
public function setNamespace(string $namespace)
{
$this->namespace = $namespace;
}
public function getNamespace() : string
{
return $this->namespace;
}
public function removeNamespace()
{
$this->namespace = null;
}
public function addUse(string $namespace, string $as = null)
{
if(isset($as)) {
$this->use[$as] = $namespace;
} else {
$this->use[] = $namespace;
}
}
public function removeUse($id) : bool
{
if(isset($this->use[$id])) {
unset($this->use[$id]);
return true;
}
return false;
}
public function setName(string $name) {
$this->name = $name;
}
public function getName() : string
{
return $this->name;
}
public function addImplements(string $implements)
{
$this->implements[] = $implements;
array_unique($this->implements);
}
public function addInclude(string $include)
{
$this->include[] = $include;
array_unique($this->include);
}
public function addRequire(string $require)
{
$this->require[] = $require;
array_unique($this->require);
}
public function addTrait(string $trait, string $as = null)
{
if(isset($as)) {
$this->traits[$as] = $trait;
} else {
$this->traits[] = $trait;
}
}
public function addMember(MemberParser $member)
{
$this->members[$member->getName()] = $member;
}
public function removeMember(string $name) : bool
{
if(isset($this->members[$name])) {
unset($this->members[$name]);
return true;
}
return false;
}
public function getMember($name) : string
{
return $this->members[$name] ?? '';
}
public function addFunction(functionParser $function)
{
$this->functions[$function->getName()] = $function;
}
public function removeFunction(string $name) : bool
{
if(isset($this->functions[$name])) {
unset($this->functions[$name]);
return true;
}
return false;
}
public function getFunction($name) : string
{
return $this->functions[$name] ?? '';
}
public function parse() : string
{
$class = '';
if(!empty($this->requires)) {
foreach($this->requires as $require) {
$class .= 'require_once "' . $require . '";' . PHP_EOL;
}
$class .= PHP_EOL;
}
if(!empty($this->includes)) {
foreach($this->includes as $include) {
$class .= 'include_once "' . $include . '";' . PHP_EOL;
}
$class .= PHP_EOL;
}
if(isset($namespace)) {
$class = $namespace . ';' . PHP_EOL . PHP_EOL;
}
if(!empty($this->use)) {
foreach($this->use as $as => $use) {
$class .= 'use ' . $use . (is_string($as) ? ' as ' . $as : '') . ';' . PHP_EOL;
}
$class .= PHP_EOL;
}
if($this->isfinal) {
$class .= 'final ';
}
if($this->isAbstract) {
$class .= 'abstract ';
}
$class .= $this->type . ' ' . $this->name . ' ';
if(isset($this->extends)) {
$class .= 'extends ' . $this->extends . ' ';
}
if(!empty($this->implements)) {
$class .= 'implements ' . implode(', ', $this->implements) . PHP_EOL;
}
$class .= '{' . PHP_EOL . PHP_EOL;
if(!empty($this->traits)) {
foreach($this->traits as $as => $trait) {
$class .= 'use ' . $trait . ';' . PHP_EOL;
}
$class .= PHP_EOL;
}
foreach($this->members as $name => $member) {
$class .= $member->parse() . PHP_EOL . PHP_EOL;
}
foreach($this->functions as $name => $function) {
$class .= $function->parse() . PHP_EOL . PHP_EOL;
}
$class .= '}';
return $class;
}
}

View File

@ -0,0 +1,38 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Utils\Parser\Php;
use phpOMS\Datatypes\Enum;
/**
* Database type enum.
*
* Database types that are supported by the application
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
abstract class ClassType extends Enum
{
const _CLASS = 'class';
const _TRAIT = 'trait';
const _INTERFACE = 'interface';
}

View File

@ -0,0 +1,146 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Utils\Parser\Php;
/**
* Member parser class.
*
* Parsing/serializing variables
*
* @category System
* @package Framework
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class FunctionParser
{
const INDENT = 4;
private $name = '';
private $visibility = Visibility::_PUBLIC;
private $isStatic = false;
private $isAbstract = false;
private $isFinal = false;
private $return = null;
private $parameters = [];
public function setName(string $name) {
$this->name = $name;
}
public function getName() : string
{
return $this->string;
}
public function setVisibility(string $visibility)
{
$this->visibility = $visibility;
}
public function getVisibility() : string
{
return $this->visibility;
}
public function setStatic(bool $static) {
$this->isStatic = $static;
if($this->isStatic) {
$this->isConst = false;
}
}
public function isStatic() : bool
{
return $this->isStatic;
}
public function setFinal(bool $final)
{
$this->isFinal = $final;
}
public function isFinal() : bool
{
return $this->isFinal;
}
public function setAbstract(bool $abstract)
{
$this->isAbstract = $abstract;
}
public function isAbstract() : bool
{
return $this->isAbstract;
}
public function setReturn(string $return)
{
$this->return = $return;
}
public function removeReturn()
{
$this->return = null;
}
public function getReturn()
{
return $this->return;
}
public function parse() : string
{
$function = '';
$member .= str_repeat(' ', self::INDENT);
if($this->isFinal) {
$member .= 'final ';
}
if($this->isAbstract) {
$member .= 'abstract ';
}
$member .= $this->visibility . ' ';
if($this->isStatic) {
$member .= 'static ';
}
$member .= 'fucntion ' . $this->name . '(';
$parameters = '';
foreach($this->parameters as $name => $para) {
$parameters = (isset($para['typehint']) ? $para['typehint'] . ' ': '') . $para['name'] . (array_key_exists('default', $para) ? ' = ' . MemberParser::parseVariable($para['default']) : '') . ', ';
}
$member .= rtrim($parameters, ', ') . ') ';
$member .= ($this->return ?? '') . PHP_EOL;
$member .= '{' . PHP_EOL . $this->body . PHP_EOL . '}';
}
}

View File

@ -0,0 +1,136 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Utils\Parser\Php;
/**
* Member parser class.
*
* Parsing/serializing variables
*
* @category System
* @package Framework
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class MemberParser
{
const INDENT = 4;
private $name = '';
private $visibility = Visibility::_PUBLIC;
private $isStatic = false;
private $isConst = false;
private $default = null;
public function setName(string $name) {
$this->name = $name;
}
public function getName() : string
{
return $this->string;
}
public function setVisibility(string $visibility)
{
$this->visibility = $visibility;
}
public function getVisibility() : string
{
return $this->visibility;
}
public function setStatic(bool $static) {
$this->isStatic = $static;
if($this->isStatic) {
$this->isConst = false;
}
}
public function isStatic() : bool
{
return $this->isStatic;
}
public function setConst(bool $const)
{
$this->isConst = $const;
if($this->isConst) {
$this->isStatic = false;
}
}
public function setDefault($default) {
$this->default = $default;
}
public function parse() : string
{
$member = '';
$member .= str_repeat(' ', self::INDENT);
$member .= $this->visibility . ' ';
if($this->isStatic) {
$member .= 'static ';
}
if($this->isConst) {
$member .= 'const ';
}
$member .= (!$this->isConst ? '$' : '') . $name . ' = ' . self::parseVariable($this->default) . ';';
}
/**
* Serialize value.
*
* @param mixed $value Value to serialzie
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function parseVariable($value) : string
{
if(is_array($value)) {
return ArrayParser::serializeArray($value) . PHP_EOL;
} elseif(is_string($value)) {
return '"' . $value . '"';
} elseif(is_scalar($value)) {
return $value;
} elseif(is_null($value)) {
return 'null';
} elseif(is_bool($value)) {
return $value ? 'true' : 'false';
} elseif($value instanceOf \Serializable) {
return self::parseVariable($value->serialize());
} else {
throw new \UnexpectedValueException();
}
}
}

View File

@ -0,0 +1,38 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Utils\Parser\Php;
use phpOMS\Datatypes\Enum;
/**
* Database type enum.
*
* Database types that are supported by the application
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
abstract class MemberVisibility extends Enum
{
const _PUBLIC = 'public';
const _PRIVATE = 'private';
const _PROTECTED = 'protected';
}