From 3a1a94d84f7cd4cc47608a389d4c2c15bb5f2ae9 Mon Sep 17 00:00:00 2001 From: Aziz Date: Sun, 21 Mar 2021 20:49:14 +0300 Subject: [PATCH 1/8] ISSUE-1478: Fixed findFirst return type --- CHANGELOG.md | 4 ++++ src/Generator/Snippet.php | 4 ++-- .../_data/console/app/models/files/TestModel.php | 4 ++-- .../console/app/models/files/TestModel2.php | 4 ++-- .../console/app/models/files/TestModel3.php | 4 ++-- .../console/app/models/files/TestModel5.php | 16 +++------------- .../console/app/models/files/Testmodel4.php | 4 ++-- 7 files changed, 17 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9e07c17d..77d31967c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# [4.0.6](https://github.com/phalcon/cphalcon/releases/tag/v4.0.6) +## Fixed +- Fixed model findFirst return type error [#1478](https://github.com/phalcon/phalcon-devtools/issues/1478) + # [4.0.5](https://github.com/phalcon/cphalcon/releases/tag/v4.0.5) (2021-03-14) ## Fixed - Fixed model creation failure in webtools due to wrong variable mutation [#1415](https://github.com/phalcon/phalcon-devtools/issues/1415) diff --git a/src/Generator/Snippet.php b/src/Generator/Snippet.php index 2cc872248..89946e4d7 100644 --- a/src/Generator/Snippet.php +++ b/src/Generator/Snippet.php @@ -265,9 +265,9 @@ public function getModelFindFirst($className) * Allows to query the first record that match the specified conditions * * @param mixed \$parameters - * @return %s|\Phalcon\Mvc\Model\ResultInterface + * @return %s|\Phalcon\Mvc\Model\ResultInterface|\Phalcon\Mvc\ModelInterface|null */ - public static function findFirst(\$parameters = null) + public static function findFirst(\$parameters = null): ?\Phalcon\Mvc\ModelInterface { return parent::findFirst(\$parameters); } diff --git a/tests/_data/console/app/models/files/TestModel.php b/tests/_data/console/app/models/files/TestModel.php index 69b95efad..f304a9b41 100644 --- a/tests/_data/console/app/models/files/TestModel.php +++ b/tests/_data/console/app/models/files/TestModel.php @@ -57,9 +57,9 @@ public static function find($parameters = null): \Phalcon\Mvc\Model\ResultsetInt * Allows to query the first record that match the specified conditions * * @param mixed $parameters - * @return TestModel|\Phalcon\Mvc\Model\ResultInterface + * @return TestModel|\Phalcon\Mvc\Model\ResultInterface|\Phalcon\Mvc\ModelInterface|null */ - public static function findFirst($parameters = null) + public static function findFirst($parameters = null): ?\Phalcon\Mvc\ModelInterface { return parent::findFirst($parameters); } diff --git a/tests/_data/console/app/models/files/TestModel2.php b/tests/_data/console/app/models/files/TestModel2.php index 4455b90e6..0dd97ff8c 100644 --- a/tests/_data/console/app/models/files/TestModel2.php +++ b/tests/_data/console/app/models/files/TestModel2.php @@ -43,9 +43,9 @@ public static function find($parameters = null): \Phalcon\Mvc\Model\ResultsetInt * Allows to query the first record that match the specified conditions * * @param mixed $parameters - * @return TestModel2|\Phalcon\Mvc\Model\ResultInterface + * @return TestModel2|\Phalcon\Mvc\Model\ResultInterface|\Phalcon\Mvc\ModelInterface|null */ - public static function findFirst($parameters = null) + public static function findFirst($parameters = null): ?\Phalcon\Mvc\ModelInterface { return parent::findFirst($parameters); } diff --git a/tests/_data/console/app/models/files/TestModel3.php b/tests/_data/console/app/models/files/TestModel3.php index 1cda62983..6d92c31d1 100644 --- a/tests/_data/console/app/models/files/TestModel3.php +++ b/tests/_data/console/app/models/files/TestModel3.php @@ -43,9 +43,9 @@ public static function find($parameters = null): \Phalcon\Mvc\Model\ResultsetInt * Allows to query the first record that match the specified conditions * * @param mixed $parameters - * @return TestModel3|\Phalcon\Mvc\Model\ResultInterface + * @return TestModel3|\Phalcon\Mvc\Model\ResultInterface|\Phalcon\Mvc\ModelInterface|null */ - public static function findFirst($parameters = null) + public static function findFirst($parameters = null): ?\Phalcon\Mvc\ModelInterface { return parent::findFirst($parameters); } diff --git a/tests/_data/console/app/models/files/TestModel5.php b/tests/_data/console/app/models/files/TestModel5.php index 73c623879..c279eaec0 100644 --- a/tests/_data/console/app/models/files/TestModel5.php +++ b/tests/_data/console/app/models/files/TestModel5.php @@ -27,23 +27,13 @@ public function initialize() $this->setSource("TestModel5"); } - /** - * Returns table name mapped in the model. - * - * @return string - */ - public function getSource() - { - return 'TestModel5'; - } - /** * Allows to query a set of records that match the specified conditions * * @param mixed $parameters * @return TestModel5[]|TestModel5|\Phalcon\Mvc\Model\ResultSetInterface */ - public static function find($parameters = null) + public static function find($parameters = null): \Phalcon\Mvc\Model\ResultsetInterface { return parent::find($parameters); } @@ -52,9 +42,9 @@ public static function find($parameters = null) * Allows to query the first record that match the specified conditions * * @param mixed $parameters - * @return TestModel5|\Phalcon\Mvc\Model\ResultInterface + * @return TestModel5|\Phalcon\Mvc\Model\ResultInterface|\Phalcon\Mvc\ModelInterface|null */ - public static function findFirst($parameters = null) + public static function findFirst($parameters = null): ?\Phalcon\Mvc\ModelInterface { return parent::findFirst($parameters); } diff --git a/tests/_data/console/app/models/files/Testmodel4.php b/tests/_data/console/app/models/files/Testmodel4.php index 2f8de1a67..358b7834f 100644 --- a/tests/_data/console/app/models/files/Testmodel4.php +++ b/tests/_data/console/app/models/files/Testmodel4.php @@ -43,9 +43,9 @@ public static function find($parameters = null): \Phalcon\Mvc\Model\ResultsetInt * Allows to query the first record that match the specified conditions * * @param mixed $parameters - * @return Testmodel4|\Phalcon\Mvc\Model\ResultInterface + * @return Testmodel4|\Phalcon\Mvc\Model\ResultInterface|\Phalcon\Mvc\ModelInterface|null */ - public static function findFirst($parameters = null) + public static function findFirst($parameters = null): ?\Phalcon\Mvc\ModelInterface { return parent::findFirst($parameters); } From 3f2ca727d6f72dd9b184e1ee4417ed863d4f6ef7 Mon Sep 17 00:00:00 2001 From: Aziz Date: Sun, 21 Mar 2021 23:45:59 +0300 Subject: [PATCH 2/8] ISSUE-1477: Added trailing semicolon to scaffolding crud views --- CHANGELOG.md | 1 + src/Builder/Component/Scaffold.php | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77d31967c..2fce9334d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # [4.0.6](https://github.com/phalcon/cphalcon/releases/tag/v4.0.6) ## Fixed - Fixed model findFirst return type error [#1478](https://github.com/phalcon/phalcon-devtools/issues/1478) +- Added trailing semicolon to scaffolding crud views getters [#1477](https://github.com/phalcon/phalcon-devtools/issues/1477) # [4.0.5](https://github.com/phalcon/cphalcon/releases/tag/v4.0.5) (2021-03-14) ## Fixed diff --git a/src/Builder/Component/Scaffold.php b/src/Builder/Component/Scaffold.php index efeb1fcd0..3bcfe4ded 100644 --- a/src/Builder/Component/Scaffold.php +++ b/src/Builder/Component/Scaffold.php @@ -309,33 +309,33 @@ private function makeField(string $attribute, int $dataType, $relationField, arr $code .= "\t\t" . 'tag->select(["' . $attribute . '", $' . $selectDefinition[$attribute]['varName'] . ', "using" => "' . $selectDefinition[$attribute]['primaryKey'] . ',' . $selectDefinition[$attribute]['detail'] . - '", "useDummy" => true), "class" => "form-control", "id" => "' . $id . '"] ?>'; + '", "useDummy" => true), "class" => "form-control", "id" => "' . $id . '"]; ?>'; } else { switch ($dataType) { case Column::TYPE_ENUM: // enum $code .= "\t\t" . 'tag->selectStatic(["' . $attribute . - '", [], "class" => "form-control", "id" => "' . $id . '"]) ?>'; + '", [], "class" => "form-control", "id" => "' . $id . '"]); ?>'; break; case Column::TYPE_CHAR: $code .= "\t\t" . 'tag->textField(["' . $attribute . - '", "class" => "form-control", "id" => "' . $id . '"]) ?>'; + '", "class" => "form-control", "id" => "' . $id . '"]); ?>'; break; case Column::TYPE_DECIMAL: case Column::TYPE_INTEGER: $code .= "\t\t" . 'tag->textField(["' . $attribute . - '", "type" => "number", "class" => "form-control", "id" => "' . $id . '"]) ?>'; + '", "type" => "number", "class" => "form-control", "id" => "' . $id . '"]); ?>'; break; case Column::TYPE_DATE: $code .= "\t\t" . 'tag->textField(["' . $attribute . - '", "type" => "date", "class" => "form-control", "id" => "' . $id . '"]) ?>'; + '", "type" => "date", "class" => "form-control", "id" => "' . $id . '"]); ?>'; break; case Column::TYPE_TEXT: $code .= "\t\t" . 'tag->textArea(["' . $attribute . - '", "cols" => 30, "rows" => 4, "class" => "form-control", "id" => "' . $id . '"]) ?>'; + '", "cols" => 30, "rows" => 4, "class" => "form-control", "id" => "' . $id . '"]); ?>'; break; default: $code .= "\t\t" . 'tag->textField(["' . $attribute . - '", "size" => 30, "class" => "form-control", "id" => "' . $id . '"]) ?>'; + '", "size" => 30, "class" => "form-control", "id" => "' . $id . '"]); ?>'; break; } } @@ -565,8 +565,8 @@ private function makeLayouts() // View model layout $code = ''; if ($this->options->has('theme')) { - $code .= 'tag->stylesheetLink("themes/lightness/style") ?>'.PHP_EOL; - $code .= 'tag->stylesheetLink("themes/base") ?>'.PHP_EOL; + $code .= 'tag->stylesheetLink("themes/lightness/style"); ?>'.PHP_EOL; + $code .= 'tag->stylesheetLink("themes/base"); ?>'.PHP_EOL; $code .= '
' . PHP_EOL; } else { $code .= '
' . PHP_EOL; @@ -733,9 +733,9 @@ private function makeViewSearch(): void } else { $detailField = ucfirst($this->options->get('allReferences')[$fieldName]['detail']); $rowCode .= '$' . $this->options->get('singular') . '->get' . - $this->options->get('allReferences')[$fieldName]['tableName'] . '()->get' . $detailField . '()'; + $this->options->get('allReferences')[$fieldName]['tableName'] . '()->get' . $detailField . '();'; } - $rowCode .= ' ?>' . PHP_EOL; + $rowCode .= '; ?>' . PHP_EOL; } $idField = $this->options->get('attributes')[0]; From 921da1766cd01c6dc841705562a0effc813815ae Mon Sep 17 00:00:00 2001 From: Aziz Date: Mon, 22 Mar 2021 00:03:41 +0300 Subject: [PATCH 3/8] ISSUE-1491: Fixed optional options checks on model create --- CHANGELOG.md | 1 + src/Builder/Component/Model.php | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fce9334d..504654098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Fixed - Fixed model findFirst return type error [#1478](https://github.com/phalcon/phalcon-devtools/issues/1478) - Added trailing semicolon to scaffolding crud views getters [#1477](https://github.com/phalcon/phalcon-devtools/issues/1477) +- Fixed optional options (namespace, abstract) checks on model create [#1491](https://github.com/phalcon/phalcon-devtools/issues/1491) # [4.0.5](https://github.com/phalcon/cphalcon/releases/tag/v4.0.5) (2021-03-14) ## Fixed diff --git a/src/Builder/Component/Model.php b/src/Builder/Component/Model.php index e43177aa2..b526cba37 100644 --- a/src/Builder/Component/Model.php +++ b/src/Builder/Component/Model.php @@ -172,7 +172,7 @@ public function build(): void } $entityNamespace = ''; - if ($this->modelOptions->getOption('namespace')) { + if ($this->modelOptions->hasOption('namespace')) { $entityNamespace = $this->modelOptions->getOption('namespace')."\\"; } @@ -190,7 +190,7 @@ public function build(): void foreach ($db->describeReferences($this->modelOptions->getOption('name'), $schema) as $reference) { $entityNamespace = ''; - if ($this->modelOptions->getOption('namespace')) { + if ($this->modelOptions->hasOption('namespace')) { $entityNamespace = $this->modelOptions->getOption('namespace'); } From 5a33136336c25b94df9e2225f028a597c85e6200 Mon Sep 17 00:00:00 2001 From: Aziz Date: Mon, 22 Mar 2021 00:39:57 +0300 Subject: [PATCH 4/8] ISSUE-1468: Fixed wrong request filtering --- CHANGELOG.md | 1 + src/Builder/Component/Scaffold.php | 18 ++++++++---------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 504654098..6f4a79435 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Fixed model findFirst return type error [#1478](https://github.com/phalcon/phalcon-devtools/issues/1478) - Added trailing semicolon to scaffolding crud views getters [#1477](https://github.com/phalcon/phalcon-devtools/issues/1477) - Fixed optional options (namespace, abstract) checks on model create [#1491](https://github.com/phalcon/phalcon-devtools/issues/1491) +- Fixed wrong request filtering [#1468](https://github.com/phalcon/phalcon-devtools/issues/1468) # [4.0.5](https://github.com/phalcon/cphalcon/releases/tag/v4.0.5) (2021-03-14) ## Fixed diff --git a/src/Builder/Component/Scaffold.php b/src/Builder/Component/Scaffold.php index 3bcfe4ded..5cbfcc446 100644 --- a/src/Builder/Component/Scaffold.php +++ b/src/Builder/Component/Scaffold.php @@ -183,7 +183,7 @@ public function build(): bool $attributes = $metaData->getAttributes($entity); $dataTypes = $metaData->getDataTypes($entity); $identityField = $metaData->getIdentityField($entity); - $identityField = $identityField ? $identityField : null; + $identityField = $identityField ?: null; $primaryKeys = $metaData->getPrimaryKeyAttributes($entity); $setParams = []; @@ -210,7 +210,7 @@ public function build(): bool // Build Controller $this->makeController(); - if ($this->options->get('templateEngine') == 'volt') { + if ($this->options->get('templateEngine') === 'volt') { $this->makeLayoutsVolt(); $this->makeViewVolt('index'); $this->makeViewSearchVolt(); @@ -233,7 +233,7 @@ public function build(): bool * @param string $var * @param mixed $fields * @param bool $useGetSetters - * @param string $identityField + * @param null|string $identityField * * @return string */ @@ -241,18 +241,16 @@ private function captureFilterInput(string $var, $fields, bool $useGetSetters, s { $code = ''; foreach ($fields as $field => $dataType) { - if ($identityField !== null && $field == $identityField) { + if ($identityField !== null && $field === $identityField) { continue; } - if (is_int($dataType) !== false) { + if (\in_array($dataType, [Column::TYPE_DECIMAL, Column::TYPE_INTEGER])) { $fieldCode = '$this->request->getPost("'.$field.'", "int")'; + } elseif ($field === 'email') { + $fieldCode = '$this->request->getPost("'.$field.'", "email")'; } else { - if ($field == 'email') { - $fieldCode = '$this->request->getPost("'.$field.'", "email")'; - } else { - $fieldCode = '$this->request->getPost("'.$field.'")'; - } + $fieldCode = '$this->request->getPost("'.$field.'")'; } $code .= '$' . Utils::lowerCamelizeWithDelimiter($var, '-', true) . '->'; From 0ef84da44c87672cecee25225c281ba33d542814 Mon Sep 17 00:00:00 2001 From: Aziz Date: Mon, 22 Mar 2021 00:57:57 +0300 Subject: [PATCH 5/8] ISSUE-1467: Fixed empty namespace generating --- CHANGELOG.md | 1 + src/Builder/Component/Controller.php | 6 ++++-- src/Builder/Component/Model.php | 7 ++++--- src/Builder/Component/Scaffold.php | 11 +++++------ 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f4a79435..5a4513629 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Added trailing semicolon to scaffolding crud views getters [#1477](https://github.com/phalcon/phalcon-devtools/issues/1477) - Fixed optional options (namespace, abstract) checks on model create [#1491](https://github.com/phalcon/phalcon-devtools/issues/1491) - Fixed wrong request filtering [#1468](https://github.com/phalcon/phalcon-devtools/issues/1468) +- Fixed empty namespace generation [#1467](https://github.com/phalcon/phalcon-devtools/issues/1467) # [4.0.5](https://github.com/phalcon/cphalcon/releases/tag/v4.0.5) (2021-03-14) ## Fixed diff --git a/src/Builder/Component/Controller.php b/src/Builder/Component/Controller.php index 6d8baf752..7d2e668e6 100644 --- a/src/Builder/Component/Controller.php +++ b/src/Builder/Component/Controller.php @@ -103,12 +103,14 @@ public function build() */ protected function constructNamespace(): string { - $namespace = $this->options->get('namespace'); + $namespace = $this->options->has('namespace') + ? (string) $this->options->get('namespace') : null; + if ($namespace === null) { return ''; } - if ($this->checkNamespace((string)$namespace)) { + if ($this->checkNamespace($namespace) && !empty(trim($namespace))) { return 'namespace ' . $this->options->get('namespace') . ';' . PHP_EOL . PHP_EOL; } diff --git a/src/Builder/Component/Model.php b/src/Builder/Component/Model.php index b526cba37..b4c84f431 100644 --- a/src/Builder/Component/Model.php +++ b/src/Builder/Component/Model.php @@ -111,9 +111,10 @@ public function build(): void require_once $config->devtools->loader; } - $namespace = ''; - if ($this->modelOptions->hasOption('namespace') && - $this->checkNamespace((string)$this->modelOptions->getOption('namespace'))) { + $namespace = $this->modelOptions->hasOption('namespace') + ? (string) $this->modelOptions->getOption('namespace') : ''; + + if ($this->checkNamespace($namespace) && !empty(trim($namespace))) { $namespace = 'namespace ' . $this->modelOptions->getOption('namespace') . ';' . PHP_EOL . PHP_EOL; } diff --git a/src/Builder/Component/Scaffold.php b/src/Builder/Component/Scaffold.php index 5cbfcc446..dbc2f21bb 100644 --- a/src/Builder/Component/Scaffold.php +++ b/src/Builder/Component/Scaffold.php @@ -459,14 +459,13 @@ private function makeController(): void $code = file_get_contents($this->options->get('templatePath') . '/scaffold/no-forms/Controller.php'); $usesNamespaces = false; - $controllerNamespace = (string)$this->options->get('controllersNamespace'); - if ($this->options->has('controllersNamespace') && - $controllerNamespace && - $this->checkNamespace($controllerNamespace) - ) { + $controllerNamespace = $this->options->has('controllersNamespace') + ? (string) $this->options->get('controllersNamespace') : ''; + + if (!empty(trim($controllerNamespace)) && $this->checkNamespace($controllerNamespace)) { $code = str_replace( '$namespace$', - 'namespace ' . $this->options->get('controllersNamespace').';' . PHP_EOL, + 'namespace ' . $controllerNamespace.';' . PHP_EOL, $code ); $usesNamespaces = true; From f1ae3aafe8623636f228fdcaddaf2d7ec279c295 Mon Sep 17 00:00:00 2001 From: Aziz Date: Mon, 22 Mar 2021 02:17:50 +0300 Subject: [PATCH 6/8] ISSUE-1297: Removed getSource() method due to final implementation --- CHANGELOG.md | 2 ++ src/Builder/Component/Model.php | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a4513629..7b5cbc4eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - Fixed optional options (namespace, abstract) checks on model create [#1491](https://github.com/phalcon/phalcon-devtools/issues/1491) - Fixed wrong request filtering [#1468](https://github.com/phalcon/phalcon-devtools/issues/1468) - Fixed empty namespace generation [#1467](https://github.com/phalcon/phalcon-devtools/issues/1467) +- Removed `model->getSource()` method generation due to its becoming final in `Phalcon\Mvc\Model` [#1297](https://github.com/phalcon/phalcon-devtools/issues/1297) + # [4.0.5](https://github.com/phalcon/cphalcon/releases/tag/v4.0.5) (2021-03-14) ## Fixed diff --git a/src/Builder/Component/Model.php b/src/Builder/Component/Model.php index b4c84f431..80c6bc362 100644 --- a/src/Builder/Component/Model.php +++ b/src/Builder/Component/Model.php @@ -226,8 +226,6 @@ public function build(): void } } - $possibleMethods['getSource'] = true; - /** @noinspection PhpIncludeInspection */ require_once $modelPath; From e249db0b26a9d28ec882a4a46fcb7c09e7dd6dda Mon Sep 17 00:00:00 2001 From: Aziz Date: Mon, 22 Mar 2021 23:58:04 +0300 Subject: [PATCH 7/8] ISSUE-1317: Fix model --force creation bugs --- CHANGELOG.md | 2 + src/Builder/Component/AllModels.php | 7 +- src/Builder/Component/Model.php | 204 +++++++++++++--------------- 3 files changed, 98 insertions(+), 115 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b5cbc4eb..ec3e1f0c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - Fixed wrong request filtering [#1468](https://github.com/phalcon/phalcon-devtools/issues/1468) - Fixed empty namespace generation [#1467](https://github.com/phalcon/phalcon-devtools/issues/1467) - Removed `model->getSource()` method generation due to its becoming final in `Phalcon\Mvc\Model` [#1297](https://github.com/phalcon/phalcon-devtools/issues/1297) +- Fixed model `--force` creation bugs [#1317](https://github.com/phalcon/phalcon-devtools/issues/1317) +- Fixed mapping of PascalCase table fields [#1463](https://github.com/phalcon/phalcon-devtools/issues/1463) # [4.0.5](https://github.com/phalcon/cphalcon/releases/tag/v4.0.5) (2021-03-14) diff --git a/src/Builder/Component/AllModels.php b/src/Builder/Component/AllModels.php index e8915263a..2588b30ad 100644 --- a/src/Builder/Component/AllModels.php +++ b/src/Builder/Component/AllModels.php @@ -85,14 +85,9 @@ public function build(): void $genSettersGetters = $this->options->get('genSettersGetters', false); $mapColumn = $this->options->get('mapColumn', false); - $adapter = $config->database->adapter; + $adapter = $config->database->adapter ?? 'Mysql'; $this->isSupportedAdapter($adapter); - $adapter = 'Mysql'; - if (isset($config->database->adapter)) { - $adapter = $config->database->adapter; - } - if (is_object($config->database)) { $configArray = $config->database->toArray(); } else { diff --git a/src/Builder/Component/Model.php b/src/Builder/Component/Model.php index 80c6bc362..e8c6b2f79 100644 --- a/src/Builder/Component/Model.php +++ b/src/Builder/Component/Model.php @@ -27,6 +27,8 @@ use Phalcon\Validation; use Phalcon\Validation\Validator\Email as EmailValidator; use ReflectionClass; +use ReflectionClassConstant; +use ReflectionProperty; /** * Builder to generate models @@ -121,14 +123,9 @@ public function build(): void $genDocMethods = $this->modelOptions->getValidOptionOrDefault('genDocMethods', false); $useSettersGetters = $this->modelOptions->getValidOptionOrDefault('genSettersGetters', false); - $adapter = $config->database->adapter; + $adapter = $config->database->adapter ?? 'Mysql'; $this->isSupportedAdapter($adapter); - $adapter = 'Mysql'; - if (isset($config->database->adapter)) { - $adapter = $config->database->adapter; - } - if (is_object($config->database)) { $configArray = $config->database->toArray(); } else { @@ -168,40 +165,36 @@ public function build(): void foreach ($referenceList as $tableName => $references) { foreach ($references as $reference) { - if ($reference->getReferencedTable() != $this->modelOptions->getOption('name')) { + if ($reference->getReferencedTable() !== $this->modelOptions->getOption('name')) { continue; } - $entityNamespace = ''; - if ($this->modelOptions->hasOption('namespace')) { - $entityNamespace = $this->modelOptions->getOption('namespace')."\\"; - } + $entityNamespace = $this->modelOptions->hasOption('namespace') + ? $this->modelOptions->getOption('namespace')."\\" : ''; $refColumns = $reference->getReferencedColumns(); $columns = $reference->getColumns(); $initialize[] = $snippet->getRelation( 'hasMany', - $this->modelOptions->getOption('camelize') ? Utils::lowerCamelize($refColumns[0]) : $refColumns[0], + $this->getFieldName($refColumns[0]), $entityNamespace . Text::camelize($tableName, '_-'), - $this->modelOptions->getOption('camelize') ? Utils::lowerCamelize($columns[0]) : $columns[0], + $this->getFieldName($columns[0]), "['alias' => '" . Text::camelize($tableName, '_-') . "']" ); } } foreach ($db->describeReferences($this->modelOptions->getOption('name'), $schema) as $reference) { - $entityNamespace = ''; - if ($this->modelOptions->hasOption('namespace')) { - $entityNamespace = $this->modelOptions->getOption('namespace'); - } + $entityNamespace = $this->modelOptions->hasOption('namespace') + ? $this->modelOptions->getOption('namespace') : ''; $refColumns = $reference->getReferencedColumns(); $columns = $reference->getColumns(); $initialize[] = $snippet->getRelation( 'belongsTo', - $this->modelOptions->getOption('camelize') ? Utils::lowerCamelize($columns[0]) : $columns[0], + $this->getFieldName($columns[0]), $this->getEntityClassName($reference, $entityNamespace), - $this->modelOptions->getOption('camelize') ? Utils::lowerCamelize($refColumns[0]) : $refColumns[0], + $this->getFieldName($refColumns[0]), "['alias' => '" . Text::camelize($reference->getReferencedTable(), '_-') . "']" ); } @@ -236,7 +229,7 @@ public function build(): void } $reflection = new ReflectionClass($fullClassName); foreach ($reflection->getMethods() as $method) { - if ($method->getDeclaringClass()->getName() != $fullClassName) { + if ($method->getDeclaringClass()->getName() !== $fullClassName) { continue; } @@ -284,117 +277,60 @@ public function build(): void } } - $possibleFields = $possibleFieldsTransformed = []; + $possibleFieldsTransformed = []; foreach ($fields as $field) { - $possibleFields[$field->getName()] = true; - if ($this->modelOptions->getOption('camelize')) { - $fieldName = Utils::lowerCamelize(Utils::camelize($field->getName(), '_-')); - } else { - $fieldName = Utils::lowerCamelize(Utils::camelize($field->getName(), '-')); - } + $fieldName = $this->getFieldName($field->getName()); $possibleFieldsTransformed[$fieldName] = true; } if (method_exists($reflection, 'getReflectionConstants')) { foreach ($reflection->getReflectionConstants() as $constant) { - if ($constant->getDeclaringClass()->getName() != $fullClassName) { + if ($constant->getDeclaringClass()->getName() !== $fullClassName) { continue; } - $constantsPreg = '/^(\s*)const(\s+)'.$constant->getName().'([\s=;]+)/'; - $endLine = $startLine = 0; - foreach ($linesCode as $line => $code) { - if (preg_match($constantsPreg, $code)) { - $startLine = $line; - break; - } - } - if (!empty($startLine)) { - $countLines = count($linesCode); - for ($i = $startLine; $i < $countLines; $i++) { - if (preg_match('/;(\s*)$/', $linesCode[$i])) { - $endLine = $i; - break; - } - } - } - if (!empty($startLine) && !empty($endLine)) { - $constantDeclaration = join( - '', - array_slice( - $linesCode, - $startLine, - $endLine - $startLine + 1 - ) - ); - $attributes[] = PHP_EOL . " " . $constant->getDocComment() . - PHP_EOL . $constantDeclaration; + $constantsPreg = '/const(\s+)' . $constant->getName() . '([\s=;]+)/'; + $attribute = $this->getAttribute($linesCode, $constantsPreg, $constant); + if (!empty($attribute)) { + $attributes[] = $attribute; } } } foreach ($reflection->getProperties() as $property) { $propertyName = $property->getName(); - /** @var null|string $possibleFieldsValue */ - $possibleFieldsValue = $possibleFieldsTransformed[$propertyName]; - - if ($property->getDeclaringClass()->getName() != $fullClassName || - !empty($possibleFieldsValue)) { + if (!empty($possibleFieldsTransformed[$propertyName]) + || $property->getDeclaringClass()->getName() !== $fullClassName + ) { continue; } $modifiersPreg = ''; switch ($property->getModifiers()) { - case \ReflectionProperty::IS_PUBLIC: + case ReflectionProperty::IS_PUBLIC: $modifiersPreg = '^(\s*)public(\s+)'; break; - case \ReflectionProperty::IS_PRIVATE: + case ReflectionProperty::IS_PRIVATE: $modifiersPreg = '^(\s*)private(\s+)'; break; - case \ReflectionProperty::IS_PROTECTED: + case ReflectionProperty::IS_PROTECTED: $modifiersPreg = '^(\s*)protected(\s+)'; break; - case \ReflectionProperty::IS_STATIC + \ReflectionProperty::IS_PUBLIC: + case ReflectionProperty::IS_STATIC + ReflectionProperty::IS_PUBLIC: $modifiersPreg = '^(\s*)(public?)(\s+)static(\s+)'; break; - case \ReflectionProperty::IS_STATIC + \ReflectionProperty::IS_PROTECTED: + case ReflectionProperty::IS_STATIC + ReflectionProperty::IS_PROTECTED: $modifiersPreg = '^(\s*)protected(\s+)static(\s+)'; break; - case \ReflectionProperty::IS_STATIC + \ReflectionProperty::IS_PRIVATE: + case ReflectionProperty::IS_STATIC + ReflectionProperty::IS_PRIVATE: $modifiersPreg = '^(\s*)private(\s+)static(\s+)'; break; } $modifiersPreg = '/' . $modifiersPreg . '\$' . $propertyName . '([\s=;]+)/'; - $endLine = $startLine = 0; - foreach ($linesCode as $line => $code) { - if (preg_match($modifiersPreg, $code)) { - $startLine = $line; - break; - } - } - - if (!empty($startLine)) { - $countLines = count($linesCode); - for ($i = $startLine; $i < $countLines; $i++) { - if (preg_match('/;(\s*)$/', $linesCode[$i])) { - $endLine = $i; - break; - } - } - } - - if (!empty($startLine) && !empty($endLine)) { - $propertyDeclaration = join( - '', - array_slice( - $linesCode, - $startLine, - $endLine - $startLine + 1 - ) - ); - $attributes[] = PHP_EOL . " " . $property->getDocComment() . PHP_EOL . - $propertyDeclaration; + $attribute = $this->getAttribute($linesCode, $modifiersPreg, $property); + if (!empty($attribute)) { + $attributes[] = $attribute; } } } catch (\Exception $e) { @@ -410,12 +346,9 @@ public function build(): void $validations = []; foreach ($fields as $field) { + $fieldName = $this->getFieldName($field->getName()); + if ($field->getType() === Column::TYPE_CHAR) { - if ($this->modelOptions->getOption('camelize')) { - $fieldName = Utils::lowerCamelize(Utils::camelize($field->getName(), '_-')); - } else { - $fieldName = Utils::lowerCamelize(Utils::camelize($field->getName(), '-')); - } $domain = []; if (preg_match('/\((.*)\)/', (string)$field->getType(), $matches)) { foreach (explode(',', $matches[1]) as $item) { @@ -428,12 +361,7 @@ public function build(): void } } - if ($field->getName() == 'email') { - if ($this->modelOptions->getOption('camelize')) { - $fieldName = Utils::lowerCamelize(Utils::camelize($field->getName(), '_-')); - } else { - $fieldName = Utils::lowerCamelize(Utils::camelize($field->getName(), '-')); - } + if ($field->getName() === 'email') { $validations[] = $snippet->getValidateEmail($fieldName); $uses[] = $snippet->getUseAs(EmailValidator::class, 'EmailValidator'); } @@ -465,8 +393,7 @@ public function build(): void } $type = $this->getPHPType($field->getType()); - $fieldName = Utils::lowerCamelizeWithDelimiter($field->getName(), '-', true); - $fieldName = $this->modelOptions->getOption('camelize') ? Utils::lowerCamelize($fieldName) : $fieldName; + $fieldName = $this->getFieldName($field->getName()); $attributes[] = $snippet->getAttributes( $type, $useSettersGetters ? 'protected' : 'public', @@ -571,6 +498,65 @@ public function build(): void } } + /** + * @param array $linesCode + * @param string $pattern + * @param ReflectionProperty|ReflectionClassConstant $attribute + * + * @return null|string + */ + protected function getAttribute(array $linesCode, string $pattern, $attribute): ?string + { + $endLine = $startLine = 0; + foreach ($linesCode as $line => $code) { + if (preg_match($pattern, $code)) { + $startLine = $line; + break; + } + } + if (!empty($startLine)) { + $countLines = count($linesCode); + for ($i = $startLine; $i < $countLines; $i++) { + if (preg_match('/;(\s*)$/', $linesCode[$i])) { + $endLine = $i; + break; + } + } + } + + if (!empty($startLine) && !empty($endLine)) { + $attributeDeclaration = join( + '', + array_slice( + $linesCode, + $startLine, + $endLine - $startLine + 1 + ) + ); + $attributeFormatted = $attributeDeclaration; + if (!empty($attribute->getDocComment())) { + $attributeFormatted = " " . $attribute->getDocComment() . PHP_EOL . $attribute; + } + return $attributeFormatted; + } + + return null; + } + + /** + * @param string $fieldName + * + * @return string + */ + protected function getFieldName(string $fieldName): string + { + if ($this->modelOptions->getOption('camelize')) { + return Utils::lowerCamelize(Utils::camelize($fieldName, '_-')); + } + + return Utils::lowerCamelizeWithDelimiter($fieldName, '-', true); + } + /** * Set path to model * From b89d1d25a890188ae121df3559f583359c655274 Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Mon, 22 Mar 2021 22:00:29 +0000 Subject: [PATCH 8/8] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec3e1f0c7..97a1dcd5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# [4.0.6](https://github.com/phalcon/cphalcon/releases/tag/v4.0.6) +# [4.0.6](https://github.com/phalcon/cphalcon/releases/tag/v4.0.6) (2021-03-22) ## Fixed - Fixed model findFirst return type error [#1478](https://github.com/phalcon/phalcon-devtools/issues/1478) - Added trailing semicolon to scaffolding crud views getters [#1477](https://github.com/phalcon/phalcon-devtools/issues/1477)