From 6255bd9ab2990c4bbc0033c5385d7bd5797c443e Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Mon, 28 Mar 2016 13:58:42 +0200 Subject: [PATCH] Implement php code parser --- System/ArrayParser.php | 176 ---------------- Utils/Parser/Php/ArrayParser.php | 78 +++++++ Utils/Parser/Php/ClassParser.php | 308 ++++++++++++++++++++++++++++ Utils/Parser/Php/ClassType.php | 38 ++++ Utils/Parser/Php/FunctionParser.php | 146 +++++++++++++ Utils/Parser/Php/MemberParser.php | 136 ++++++++++++ Utils/Parser/Php/Visibility.php | 38 ++++ 7 files changed, 744 insertions(+), 176 deletions(-) delete mode 100644 System/ArrayParser.php create mode 100644 Utils/Parser/Php/ArrayParser.php create mode 100644 Utils/Parser/Php/ClassParser.php create mode 100644 Utils/Parser/Php/ClassType.php create mode 100644 Utils/Parser/Php/FunctionParser.php create mode 100644 Utils/Parser/Php/MemberParser.php create mode 100644 Utils/Parser/Php/Visibility.php diff --git a/System/ArrayParser.php b/System/ArrayParser.php deleted file mode 100644 index e5e24196c..000000000 --- a/System/ArrayParser.php +++ /dev/null @@ -1,176 +0,0 @@ - - * @author Dennis Eichhorn - * @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 - * @author Dennis Eichhorn - * @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 - */ - 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 - */ - 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 - */ - 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 - */ - 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 - */ - 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 - */ - 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(); - } - } -} diff --git a/Utils/Parser/Php/ArrayParser.php b/Utils/Parser/Php/ArrayParser.php new file mode 100644 index 000000000..78b1d5bf5 --- /dev/null +++ b/Utils/Parser/Php/ArrayParser.php @@ -0,0 +1,78 @@ + + * @author Dennis Eichhorn + * @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 + * @author Dennis Eichhorn + * @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 + */ + 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 + */ + 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 . ']'; + } +} diff --git a/Utils/Parser/Php/ClassParser.php b/Utils/Parser/Php/ClassParser.php new file mode 100644 index 000000000..7ca30b225 --- /dev/null +++ b/Utils/Parser/Php/ClassParser.php @@ -0,0 +1,308 @@ + + * @author Dennis Eichhorn + * @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 + * @author Dennis Eichhorn + * @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 + */ + 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; + } +} \ No newline at end of file diff --git a/Utils/Parser/Php/ClassType.php b/Utils/Parser/Php/ClassType.php new file mode 100644 index 000000000..e86ad9586 --- /dev/null +++ b/Utils/Parser/Php/ClassType.php @@ -0,0 +1,38 @@ + + * @author Dennis Eichhorn + * @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 + * @author Dennis Eichhorn + * @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'; +} diff --git a/Utils/Parser/Php/FunctionParser.php b/Utils/Parser/Php/FunctionParser.php new file mode 100644 index 000000000..d5978274b --- /dev/null +++ b/Utils/Parser/Php/FunctionParser.php @@ -0,0 +1,146 @@ + + * @author Dennis Eichhorn + * @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 + * @author Dennis Eichhorn + * @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 . '}'; + } +} \ No newline at end of file diff --git a/Utils/Parser/Php/MemberParser.php b/Utils/Parser/Php/MemberParser.php new file mode 100644 index 000000000..b12ebd161 --- /dev/null +++ b/Utils/Parser/Php/MemberParser.php @@ -0,0 +1,136 @@ + + * @author Dennis Eichhorn + * @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 + * @author Dennis Eichhorn + * @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 + */ + 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(); + } + } +} \ No newline at end of file diff --git a/Utils/Parser/Php/Visibility.php b/Utils/Parser/Php/Visibility.php new file mode 100644 index 000000000..269ade0aa --- /dev/null +++ b/Utils/Parser/Php/Visibility.php @@ -0,0 +1,38 @@ + + * @author Dennis Eichhorn + * @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 + * @author Dennis Eichhorn + * @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'; +}