Skip to content

Commit 5ec8a37

Browse files
authored
3.4.1 (#1261)
* bugfix: webtools modelsNamespace typo * added: migration support multiple dir * fix: test file dir error * doc: PHPDoc error * dosc: Optimization description * Make provision to keep constant and properties when generating Views (#1253) * Updated Bootstrap framework to version 4.1.3 (#1257) * set database connection dialect (#1260) * set database connection dialect * consolidate if statement for Mysql
1 parent 4294150 commit 5ec8a37

File tree

19 files changed

+467
-94
lines changed

19 files changed

+467
-94
lines changed

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ parameters:
88
- '#Call to an undefined method object::toArray().#'
99
- '#supplied for foreach, only iterables are supported#'
1010
- '#Call to an undefined method [a-zA-Z0-9\\_]+::addMinor().#'
11+
- '#Call to an undefined method ReflectionClass::getReflectionConstants\(\)\.#'
12+
reportUnmatchedIgnoredErrors: false

scripts/Phalcon/Builder/Model.php

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public function build()
219219
$alreadyFindFirst = false;
220220
$alreadyColumnMapped = false;
221221
$alreadyGetSourced = false;
222+
$attributes = [];
222223

223224
if (file_exists($modelPath)) {
224225
try {
@@ -240,7 +241,7 @@ public function build()
240241

241242
$linesCode = file($modelPath);
242243
$fullClassName = $this->modelOptions->getOption('className');
243-
if ($this->modelOptions->getOption('namespace')) {
244+
if ($this->modelOptions->hasOption('namespace')) {
244245
$fullClassName = $this->modelOptions->getOption('namespace').'\\'.$fullClassName;
245246
}
246247
$reflection = new ReflectionClass($fullClassName);
@@ -295,6 +296,107 @@ public function build()
295296
break;
296297
}
297298
}
299+
300+
$possibleFields = [];
301+
foreach ($fields as $field) {
302+
$possibleFields[$field->getName()] = true;
303+
}
304+
if (method_exists($reflection, 'getReflectionConstants')) {
305+
foreach ($reflection->getReflectionConstants() as $constant) {
306+
if ($constant->getDeclaringClass()->getName() != $fullClassName) {
307+
continue;
308+
}
309+
$constantsPreg = '/^(\s*)const(\s+)'.$constant->getName().'([\s=;]+)/';
310+
$endLine = $startLine = 0;
311+
foreach ($linesCode as $line => $code) {
312+
if (preg_match($constantsPreg, $code)) {
313+
$startLine = $line;
314+
break;
315+
}
316+
}
317+
if (!empty($startLine)) {
318+
$countLines = count($linesCode);
319+
for ($i = $startLine; $i < $countLines; $i++) {
320+
if (preg_match('/;(\s*)$/', $linesCode[$i])) {
321+
$endLine = $i;
322+
break;
323+
}
324+
}
325+
}
326+
327+
if (!empty($startLine) && !empty($endLine)) {
328+
$constantDeclaration = join(
329+
'',
330+
array_slice(
331+
$linesCode,
332+
$startLine,
333+
$endLine - $startLine + 1
334+
)
335+
);
336+
$attributes[] = PHP_EOL . " " . $constant->getDocComment() .
337+
PHP_EOL . $constantDeclaration;
338+
}
339+
}
340+
}
341+
342+
foreach ($reflection->getProperties() as $propertie) {
343+
$propertieName = $propertie->getName();
344+
345+
if ($propertie->getDeclaringClass()->getName() != $fullClassName ||
346+
!empty($possibleFields[$propertieName])) {
347+
continue;
348+
}
349+
$modifiersPreg = '';
350+
switch ($propertie->getModifiers()) {
351+
case \ReflectionProperty::IS_PUBLIC:
352+
$modifiersPreg = '^(\s*)public(\s+)';
353+
break;
354+
case \ReflectionProperty::IS_PRIVATE:
355+
$modifiersPreg = '^(\s*)private(\s+)';
356+
break;
357+
case \ReflectionProperty::IS_PROTECTED:
358+
$modifiersPreg = '^(\s*)protected(\s+)';
359+
break;
360+
case \ReflectionProperty::IS_STATIC + \ReflectionProperty::IS_PUBLIC:
361+
$modifiersPreg = '^(\s*)(public?)(\s+)static(\s+)';
362+
break;
363+
case \ReflectionProperty::IS_STATIC + \ReflectionProperty::IS_PROTECTED:
364+
$modifiersPreg = '^(\s*)protected(\s+)static(\s+)';
365+
break;
366+
case \ReflectionProperty::IS_STATIC + \ReflectionProperty::IS_PRIVATE:
367+
$modifiersPreg = '^(\s*)private(\s+)static(\s+)';
368+
break;
369+
}
370+
$modifiersPreg = '/' . $modifiersPreg . '\$' . $propertieName . '([\s=;]+)/';
371+
$endLine = $startLine = 0;
372+
foreach ($linesCode as $line => $code) {
373+
if (preg_match($modifiersPreg, $code)) {
374+
$startLine = $line;
375+
break;
376+
}
377+
}
378+
if (!empty($startLine)) {
379+
$countLines = count($linesCode);
380+
for ($i = $startLine; $i < $countLines; $i++) {
381+
if (preg_match('/;(\s*)$/', $linesCode[$i])) {
382+
$endLine = $i;
383+
break;
384+
}
385+
}
386+
}
387+
if (!empty($startLine) && !empty($endLine)) {
388+
$propertieDeclaration = join(
389+
'',
390+
array_slice(
391+
$linesCode,
392+
$startLine,
393+
$endLine - $startLine + 1
394+
)
395+
);
396+
$attributes[] = PHP_EOL . " " . $propertie->getDocComment() . PHP_EOL .
397+
$propertieDeclaration;
398+
}
399+
}
298400
} catch (\Exception $e) {
299401
throw new RuntimeException(
300402
sprintf(
@@ -353,7 +455,6 @@ public function build()
353455
}
354456
}
355457

356-
$attributes = [];
357458
$setters = [];
358459
$getters = [];
359460
foreach ($fields as $field) {

scripts/Phalcon/Commands/Builtin/Migration.php

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@ class Migration extends Command
4343
public function getPossibleParams()
4444
{
4545
return [
46-
'action=s' => 'Generates a Migration [generate|run]',
47-
'config=s' => 'Configuration file',
48-
'migrations=s' => 'Migrations directory',
49-
'directory=s' => 'Directory where the project was created',
50-
'table=s' => 'Table to migrate. Table name or table prefix with asterisk. Default: all',
51-
'version=s' => 'Version to migrate',
52-
'descr=s' => 'Migration description (used for timestamp based migration)',
53-
'data=s' => 'Export data [always|oncreate] (Import data when run migration)',
54-
'force' => 'Forces to overwrite existing migrations',
55-
'ts-based' => 'Timestamp based migration version',
56-
'log-in-db' => 'Keep migrations log in the database table rather than in file',
57-
'dry' => 'Attempt requested operation without making changes to system (Generating only)',
58-
'verbose' => 'Output of debugging information during operation (Running only)',
46+
'action=s' => 'Generates a Migration [generate|run]',
47+
'config=s' => 'Configuration file',
48+
'migrations=s' => 'Migrations directory. Use comma separated string to specify multiple directories',
49+
'directory=s' => 'Directory where the project was created',
50+
'table=s' => 'Table to migrate. Table name or table prefix with asterisk. Default: all',
51+
'version=s' => 'Version to migrate',
52+
'descr=s' => 'Migration description (used for timestamp based migration)',
53+
'data=s' => 'Export data [always|oncreate] (Import data when run migration)',
54+
'force' => 'Forces to overwrite existing migrations',
55+
'ts-based' => 'Timestamp based migration version',
56+
'log-in-db' => 'Keep migrations log in the database table rather than in file',
57+
'dry' => 'Attempt requested operation without making changes to system (Generating only)',
58+
'verbose' => 'Output of debugging information during operation (Running only)',
5959
'no-auto-increment' => 'Disable auto increment (Generating only)',
60-
'help' => 'Shows this help [optional]',
60+
'help' => 'Shows this help [optional]',
6161
];
6262
}
6363

@@ -79,21 +79,31 @@ public function run(array $parameters)
7979
$config = $this->getConfig($path);
8080
}
8181

82+
83+
//multiple dir
84+
$migrationsDir = [];
8285
if ($this->isReceivedOption('migrations')) {
83-
$migrationsDir = $path . $this->getOption('migrations');
86+
$migrationsDir = explode(',', $this->getOption('migrations'));
8487
} elseif (isset($config['application']['migrationsDir'])) {
85-
$migrationsDir = $config['application']['migrationsDir'];
86-
if (!$this->path->isAbsolutePath($migrationsDir)) {
87-
$migrationsDir = $path . $migrationsDir;
88+
$migrationsDir = explode(',', $config['application']['migrationsDir']);
89+
}
90+
91+
92+
if (!empty($migrationsDir)) {
93+
foreach ($migrationsDir as $id => $dir) {
94+
if (!$this->path->isAbsolutePath($dir)) {
95+
$migrationsDir[$id] = $path . $dir;
96+
}
8897
}
8998
} elseif (file_exists($path . 'app')) {
90-
$migrationsDir = $path . 'app/migrations';
99+
$migrationsDir[] = $path . 'app/migrations';
91100
} elseif (file_exists($path . 'apps')) {
92-
$migrationsDir = $path . 'apps/migrations';
101+
$migrationsDir[] = $path . 'apps/migrations';
93102
} else {
94-
$migrationsDir = $path . 'migrations';
103+
$migrationsDir[] = $path . 'migrations';
95104
}
96105

106+
97107
// keep migrations log in db
98108
// either "log-in-db" option or "logInDb" config variable from "application" block
99109
$migrationsInDb = false;

scripts/Phalcon/Console/OptionParserTrait.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ trait OptionParserTrait
3838
/**
3939
* Get prefix from the option
4040
*
41-
* @param string $prefix
42-
* @param mixed $prefixEnd
41+
* @param string $prefix
42+
* @param mixed $prefixEnd
4343
*
4444
* @return mixed
4545
*/
@@ -74,12 +74,36 @@ public function getVersionNameGeneratingMigration()
7474
} elseif ($this->options['version']) {
7575
VersionCollection::setType(VersionCollection::TYPE_INCREMENTAL);
7676
$versionItem = VersionCollection::createItem($this->options['version']);
77+
//check version is exist
78+
$migrationsDirList = $this->options['migrationsDir'];
79+
if (is_array($migrationsDirList)) {
80+
foreach ($migrationsDirList as $migrationsDir) {
81+
$migrationsDirList = ModelMigration::scanForVersions($migrationsDir);
82+
if (is_array($migrationsDirList)) {
83+
foreach ($migrationsDirList as $item) {
84+
if ($item->getVersion() != $versionItem->getVersion()) {
85+
continue;
86+
}
87+
if (!$this->options['force']) {
88+
throw new \LogicException('Version ' . $item->getVersion() . ' already exists');
89+
} else {
90+
rmdir(rtrim($migrationsDir, '\\/') . DIRECTORY_SEPARATOR . $versionItem->getVersion());
91+
}
92+
}
93+
}
94+
}
95+
}
7796

7897
// The version is guessed automatically
7998
} else {
8099
VersionCollection::setType(VersionCollection::TYPE_INCREMENTAL);
81-
$versionItems = ModelMigration::scanForVersions($this->options['migrationsDir']);
82-
100+
$versionItems = [];
101+
$migrationsDirList = $this->options['migrationsDir'];
102+
if (is_array($migrationsDirList)) {
103+
foreach ($migrationsDirList as $migrationsDir) {
104+
$versionItems = $versionItems + ModelMigration::scanForVersions($migrationsDir);
105+
}
106+
}
83107
if (!isset($versionItems[0])) {
84108
$versionItem = VersionCollection::createItem('1.0.0');
85109
} else {

0 commit comments

Comments
 (0)