public function __toString()
 {
     $content = AbstractClass::tab() . $this->scope;
     if ($this->isStatic) {
         $content .= ' static';
     }
     $content .= ' $' . $this->name;
     if ($this->default) {
         if (is_array($this->default)) {
             $defaults = $this->default;
             foreach ($defaults as $key => $default) {
                 $defaults[$key] = $default;
             }
             $content .= ' = array(' . "\n" . AbstractClass::tab(2);
             $content .= implode(",\n" . AbstractClass::tab(2), $defaults);
             $content .= "\n" . AbstractClass::tab() . ")";
         } else {
             $content .= ' = ' . $this->default;
         }
     }
     $content .= ";\n";
     return $content;
 }
Exemple #2
0
<?php

include dirname(__DIR__) . "/vendor/autoload.php";
use Sb\PhpClassGenerator\AbstractClass;
use Sb\PhpClassGenerator\AbstractClassMethod;
use Sb\PhpClassGenerator\AbstractMethodParam;
use Sb\PhpClassGenerator\AbstractClassField;
$class = new AbstractClass("Coupon");
$class->setNamespace("ns");
$class->setExtends("BasicCoupon");
$class->addImplements("BasicInterface");
$class->addUse("\\Something\\Class", "SClass");
$method = new AbstractClassMethod("test");
$param = new AbstractMethodParam("param1");
$param2 = new AbstractMethodParam("param2");
$param2->setType("ParamClass");
$param2->setDefaultValue(1);
$method->setContent(AbstractClass::tab(2) . "echo 1;");
$method->addParam($param);
$method->addParam($param2);
$method->setReturn("ReturnClass");
$class->addMethod($method);
$field = new AbstractClassField("field");
$field->setDefault('"123"');
$field2 = new AbstractClassField("field2");
$field2->setDefault(array('"123"', 4));
$field2->setStatic();
$class->addField($field);
$class->addField($field2);
$class->generateSettersAndGetters();
echo $class;
 public function generate($options = array())
 {
     $dbScheme = new Scheme($this->di);
     $tables = $dbScheme->getScheme($options);
     $this->relations = [];
     //var_dump($tables);
     if (!is_dir($this->entityDir . "\\" . 'Generated')) {
         mkdir($this->entityDir . "\\" . 'Generated', 0777, true);
     }
     $this->generateCommonMethods();
     foreach ($tables as $table) {
         $tableClass = new AbstractClass($table['model']);
         $tableClass->setExtends('Basic');
         $tableClass->setNamespace($this->entityNamespaceGenerated);
         $tableClassChild = new AbstractClass($table['model']);
         $tableClassChild->setExtends("Generated\\" . $table['model']);
         $tableClassChild->setNamespace($this->entityNamespace);
         foreach ($table['columns'] as $field) {
             $fieldField = new AbstractClassField($field);
             $fieldField->setScope('protected');
             $tableClass->addField($fieldField);
         }
         $getSourceMethod = new AbstractClassMethod("getSource");
         $getSourceMethod->setScope("public");
         $getSourceMethod->addContentLine("return '{$table['name']}';");
         $tableClass->addMethod($getSourceMethod);
         /*$onConstructMethod = new AbstractClassMethod("onConstruct");
                     $tableClass->addMethod($onConstructMethod);
         
                     $beforeSaveMethod = new AbstractClassMethod("beforeSave");
                     $beforeSaveMethod->addContentLine("parent::beforeSave()");
                     $tableClass->addMethod($beforeSaveMethod);
         
                     $beforeSaveMethod = new AbstractClassMethod("beforeCreate");
                     $beforeSaveMethod->addContentLine("parent::beforeCreate()");
                     $tableClass->addMethod($beforeSaveMethod);
         
                     $beforeSaveMethod = new AbstractClassMethod("beforeUpdate");
                     $beforeSaveMethod->addContentLine("parent::beforeUpdate()");
                     $tableClass->addMethod($beforeSaveMethod);
         
                     $afterFetchMethod = new AbstractClassMethod("afterFetch");
                     $beforeSaveMethod->addContentLine("parent::afterFetch()");
                     $tableClass->addMethod($afterFetchMethod);*/
         $initializeMethod = new AbstractClassMethod("initialize");
         $initializeMethod->addContentLine('$this->useDynamicUpdate(true);');
         if (isset($table['ref_many_to_one'])) {
             $table['ref_many_to_one'] = $this->prepareRef($table['ref_many_to_one']);
             foreach ($table['ref_many_to_one'] as $ref) {
                 $aliasModel = $ref['alias'];
                 $initializeMethod->addContentLine("\$this->belongsTo('{$ref['column']}', '{$this->entityNamespace}\\{$ref['model']}', '{$ref['ref_column']}', array('alias' => '{$aliasModel}', 'reusable' => true));");
                 $getMethod = new AbstractClassMethod('get' . $aliasModel);
                 $getMethod->addContentLine("return \$this->getRelated('{$aliasModel}', \$parameters);");
                 $getMethod->setReturn("\\{$this->entityNamespace}\\{$ref['model']}");
                 $getMethodParam1 = new AbstractMethodParam("parameters");
                 $getMethodParam1->setDefaultValue("null");
                 $getMethod->addParam($getMethodParam1);
                 $variableName = lcfirst($aliasModel);
                 $setMethod = new AbstractClassMethod('set' . $aliasModel);
                 $setMethodParam1 = new AbstractMethodParam($variableName);
                 $setMethodParam1->setDocType("\\{$this->entityNamespace}\\{$ref['model']}|null");
                 $setMethod->addParam($setMethodParam1);
                 $setMethod->addContentLine("\$this->{$ref['column']} = \${$variableName} ? \${$variableName}->getId() : null;");
                 $setMethod->addContentLine("return \$this;");
                 $setMethod->setReturn('$this');
                 $tableClass->addMethod($getMethod);
                 $tableClass->addMethod($setMethod);
             }
         }
         if (isset($table['ref_one_to_many'])) {
             $table['ref_one_to_many'] = $this->prepareRef($table['ref_one_to_many'], 'ref_column');
             foreach ($table['ref_one_to_many'] as $ref) {
                 $aliasModel = $ref['alias'];
                 $initializeMethod->addContentLine("\$this->hasMany('{$ref['column']}', '{$this->entityNamespace}\\{$ref['model']}', '{$ref['ref_column']}', array('alias' => '{$aliasModel}', 'reusable' => true));");
                 $variableName = lcfirst($aliasModel);
                 $varNameMany = SbUtils::getNameMany(lcfirst($aliasModel));
                 $getMethod = new AbstractClassMethod('get' . SbUtils::getNameMany($aliasModel));
                 $getMethod->addContentLine("return \$this->getRelated('{$aliasModel}', \$parameters);");
                 $getMethodParam1 = new AbstractMethodParam('parameters');
                 $getMethodParam1->setDefaultValue('null');
                 $getMethod->addParam($getMethodParam1);
                 $getMethod->setReturn("\\{$this->entityNamespace}\\{$ref['model']}[]");
                 $tableClass->addMethod($getMethod);
                 $addMethod = new AbstractClassMethod('add' . $aliasModel);
                 $addMethod->addContentLine("\$this->_related['{$aliasModel}'][] = \${$variableName};");
                 $addMethod->addContentLine("return \$this;");
                 $addMethodParam1 = new AbstractMethodParam($variableName);
                 $addMethodParam1->setType("\\{$this->entityNamespace}\\{$ref['model']}");
                 $addMethod->addParam($addMethodParam1);
                 $addMethod->setReturn('$this');
                 $tableClass->addMethod($addMethod);
             }
         }
         if (isset($table['ref_one_to_one'])) {
             $table['ref_one_to_one'] = $this->prepareRef($table['ref_one_to_one'], 'ref_column');
             foreach ($table['ref_one_to_one'] as $ref) {
                 $aliasModel = $ref['alias'];
                 $variableName = lcfirst($aliasModel);
                 $initializeMethod->addContentLine("\$this->hasOne('{$ref['column']}', '{$this->entityNamespace}\\{$ref['model']}', '{$ref['ref_column']}', array('alias' => '{$aliasModel}', 'reusable' => true));");
                 $getMethod = new AbstractClassMethod('get' . $aliasModel);
                 $getMethod->addContentLine("\${$variableName} = \$this->getRelated('{$aliasModel}', \$parameters);");
                 $getMethod->addContentLine("if (false === \${$variableName}) {");
                 $getMethod->addContentLine(AbstractClass::tab() . "\${$variableName} = new \\{$this->entityNamespace}\\{$ref['model']}();");
                 $getMethod->addContentLine(AbstractClass::tab() . "\${$variableName}->set" . \Phalcon\Text::camelize($ref['ref_column']) . "(\$this->getId());");
                 $getMethod->addContentLine('}');
                 $getMethod->addContentLine("return \${$variableName};");
                 $getMethod->setReturn("\\{$this->entityNamespace}\\{$ref['model']}");
                 $getMethodParam1 = new AbstractMethodParam('parameters');
                 $getMethodParam1->setDefaultValue('null');
                 $getMethod->addParam($getMethodParam1);
                 $tableClass->addMethod($getMethod);
             }
         }
         if (isset($table['ref_many_to_many'])) {
             $table['ref_many_to_many'] = $this->prepareRefMany($table['ref_many_to_many']);
             foreach ($table['ref_many_to_many'] as $ref) {
                 $aliasModel = $ref['alias'];
                 $variableName = lcfirst($aliasModel);
                 $varNameMany = SbUtils::getNameMany(lcfirst($aliasModel));
                 $intermediateModel = $ref['intermediate_model'];
                 $initializeMethod->addContentLine("\$this->hasManyToMany('{$ref['intermediate_column']}', " . "'{$this->entityNamespace}\\{$ref['intermediate_model']}', '{$ref['intermediate_ref_column']}', " . "'{$ref['column']}', '{$this->entityNamespace}\\{$ref['model']}', '{$ref['ref_column']}', " . "array('alias' => '{$aliasModel}', 'reusable' => true));");
                 $getMethod = new AbstractClassMethod('get' . SbUtils::getNameMany($aliasModel));
                 $getMethod->addContentLine("return \$this->getRelated('{$aliasModel}', \$parameters);");
                 $getMethod->setReturn("\\{$this->entityNamespace}\\{$ref['model']}[]|null");
                 $getMethodParam1 = new AbstractMethodParam("parameters");
                 $getMethodParam1->setDefaultValue("null");
                 $getMethod->addParam($getMethodParam1);
                 $tableClass->addMethod($getMethod);
                 $addMethod = new AbstractClassMethod('add' . $aliasModel);
                 $addMethod->addContentLine("\$this->_related['{$aliasModel}'][] = \${$variableName};");
                 $addMethod->addContentLine("return \$this;");
                 $addMethodParam1 = new AbstractMethodParam($variableName);
                 $addMethodParam1->setType("\\{$this->entityNamespace}\\{$ref['model']}");
                 $addMethod->addParam($addMethodParam1);
                 $addMethod->setReturn('$this');
                 $tableClass->addMethod($addMethod);
                 $deleteMethod = new AbstractClassMethod('delete' . $aliasModel);
                 $deleteMethod->addContentLine("\$this->{$intermediateModel}->delete(function(\$object) use (\${$variableName}) {");
                 $deleteMethod->addContentLine(AbstractClass::tab() . "/** @var \\{$this->entityNamespace}\\{$intermediateModel} \$object */");
                 $deleteMethod->addContentLine(AbstractClass::tab() . "return \$object->get{$ref['model']}Id() === \${$variableName}->getId();");
                 $deleteMethod->addContentLine('});');
                 $deleteMethod->addContentLine("return \$this;");
                 $deleteMethodParam1 = new AbstractMethodParam($variableName);
                 $deleteMethodParam1->setType("\\{$this->entityNamespace}\\{$ref['model']}");
                 $deleteMethod->addParam($deleteMethodParam1);
                 $deleteMethod->setReturn('$this');
                 $tableClass->addMethod($deleteMethod);
             }
         }
         $tableClass->addMethod($initializeMethod);
         $tableClass->generateSettersAndGetters();
         file_put_contents($this->entityDir . "\\Generated\\" . $table['model'] . '.php', $tableClass);
         if (!is_file($this->entityDir . "\\" . $table['model'] . '.php')) {
             file_put_contents($this->entityDir . "\\" . $table['model'] . '.php', $tableClassChild);
         }
         $modelName = $table['model'] . 'Model';
         if (!is_file($this->modelDir . "\\" . $modelName . '.php')) {
             $modelClass = new AbstractClass($modelName);
             $modelClass->setNamespace('Model');
             $modelClass->setExtends('BasicModel');
             $modelClass->addUse('\\' . $this->entityNamespace . '\\' . $table['model']);
             if (isset($table['primary']) && count($table['primary']) == 1) {
                 $getByIdMethodParam = $table['primary'][0];
                 $getByIdMethod = new AbstractClassMethod("get{$table['model']}By" . \Phalcon\Text::camelize($getByIdMethodParam));
                 $getByIdMethod->addContentLine("return {$table['model']}::findFirst([");
                 $getByIdMethod->addContentLine(AbstractClass::tab() . "'{$table['primary'][0]} = ?1',");
                 $getByIdMethod->addContentLine(AbstractClass::tab() . "'bind' => [1 => \$" . lcfirst(\Phalcon\Text::camelize($getByIdMethodParam)) . "]");
                 $getByIdMethod->addContentLine("]);");
                 $getByIdMethod->setReturn("{$table['model']}");
                 $getMethodParam1 = new AbstractMethodParam(lcfirst(\Phalcon\Text::camelize($getByIdMethodParam)));
                 $getByIdMethod->addParam($getMethodParam1);
                 $modelClass->addMethod($getByIdMethod);
             }
             file_put_contents($this->modelDir . "\\" . $modelName . '.php', $modelClass);
         }
     }
     $metaFile = '.phpstorm.meta.php';
     if (is_file($metaFile)) {
         $metaContent = file_get_contents($metaFile);
         $modelsMeta = '\\Sb\\Phalcon\\Model\\Repository::getModel(\'\') => [' . "\n";
         $modelsMeta .= '            "PageContent\\\\\\PageContent" instanceof \\Model\\PageContent\\PageContentModel,' . "\n";
         foreach ($tables as $table) {
             $modelName = $table['model'];
             $modelsMeta .= "            \"{$modelName}\" instanceof \\Model\\{$modelName}Model,\n";
         }
         $modelsMeta = rtrim($modelsMeta, "\n");
         $modelsMeta .= "\n        ]";
         if (preg_match("#\\\\Sb\\\\Phalcon\\\\Model\\\\Repository::getModel\\(''\\) => \\[(.*?)\\]#is", $metaContent, $m)) {
             $metaContent = preg_replace("#\\\\Sb\\\\Phalcon\\\\Model\\\\Repository::getModel\\(''\\) => \\[(.*?)\\]#is", $modelsMeta, $metaContent);
         }
         file_put_contents($metaFile, $metaContent);
     }
 }