Skip to content

Commit

Permalink
3.4.1 (#1261)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sergeyklay authored Dec 25, 2018
1 parent 4294150 commit 5ec8a37
Show file tree
Hide file tree
Showing 19 changed files with 467 additions and 94 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ parameters:
- '#Call to an undefined method object::toArray().#'
- '#supplied for foreach, only iterables are supported#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::addMinor().#'
- '#Call to an undefined method ReflectionClass::getReflectionConstants\(\)\.#'
reportUnmatchedIgnoredErrors: false
105 changes: 103 additions & 2 deletions scripts/Phalcon/Builder/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public function build()
$alreadyFindFirst = false;
$alreadyColumnMapped = false;
$alreadyGetSourced = false;
$attributes = [];

if (file_exists($modelPath)) {
try {
Expand All @@ -240,7 +241,7 @@ public function build()

$linesCode = file($modelPath);
$fullClassName = $this->modelOptions->getOption('className');
if ($this->modelOptions->getOption('namespace')) {
if ($this->modelOptions->hasOption('namespace')) {
$fullClassName = $this->modelOptions->getOption('namespace').'\\'.$fullClassName;
}
$reflection = new ReflectionClass($fullClassName);
Expand Down Expand Up @@ -295,6 +296,107 @@ public function build()
break;
}
}

$possibleFields = [];
foreach ($fields as $field) {
$possibleFields[$field->getName()] = true;
}
if (method_exists($reflection, 'getReflectionConstants')) {
foreach ($reflection->getReflectionConstants() as $constant) {
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;
}
}
}

foreach ($reflection->getProperties() as $propertie) {
$propertieName = $propertie->getName();

if ($propertie->getDeclaringClass()->getName() != $fullClassName ||
!empty($possibleFields[$propertieName])) {
continue;
}
$modifiersPreg = '';
switch ($propertie->getModifiers()) {
case \ReflectionProperty::IS_PUBLIC:
$modifiersPreg = '^(\s*)public(\s+)';
break;
case \ReflectionProperty::IS_PRIVATE:
$modifiersPreg = '^(\s*)private(\s+)';
break;
case \ReflectionProperty::IS_PROTECTED:
$modifiersPreg = '^(\s*)protected(\s+)';
break;
case \ReflectionProperty::IS_STATIC + \ReflectionProperty::IS_PUBLIC:
$modifiersPreg = '^(\s*)(public?)(\s+)static(\s+)';
break;
case \ReflectionProperty::IS_STATIC + \ReflectionProperty::IS_PROTECTED:
$modifiersPreg = '^(\s*)protected(\s+)static(\s+)';
break;
case \ReflectionProperty::IS_STATIC + \ReflectionProperty::IS_PRIVATE:
$modifiersPreg = '^(\s*)private(\s+)static(\s+)';
break;
}
$modifiersPreg = '/' . $modifiersPreg . '\$' . $propertieName . '([\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)) {
$propertieDeclaration = join(
'',
array_slice(
$linesCode,
$startLine,
$endLine - $startLine + 1
)
);
$attributes[] = PHP_EOL . " " . $propertie->getDocComment() . PHP_EOL .
$propertieDeclaration;
}
}
} catch (\Exception $e) {
throw new RuntimeException(
sprintf(
Expand Down Expand Up @@ -353,7 +455,6 @@ public function build()
}
}

$attributes = [];
$setters = [];
$getters = [];
foreach ($fields as $field) {
Expand Down
52 changes: 31 additions & 21 deletions scripts/Phalcon/Commands/Builtin/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ class Migration extends Command
public function getPossibleParams()
{
return [
'action=s' => 'Generates a Migration [generate|run]',
'config=s' => 'Configuration file',
'migrations=s' => 'Migrations directory',
'directory=s' => 'Directory where the project was created',
'table=s' => 'Table to migrate. Table name or table prefix with asterisk. Default: all',
'version=s' => 'Version to migrate',
'descr=s' => 'Migration description (used for timestamp based migration)',
'data=s' => 'Export data [always|oncreate] (Import data when run migration)',
'force' => 'Forces to overwrite existing migrations',
'ts-based' => 'Timestamp based migration version',
'log-in-db' => 'Keep migrations log in the database table rather than in file',
'dry' => 'Attempt requested operation without making changes to system (Generating only)',
'verbose' => 'Output of debugging information during operation (Running only)',
'action=s' => 'Generates a Migration [generate|run]',
'config=s' => 'Configuration file',
'migrations=s' => 'Migrations directory. Use comma separated string to specify multiple directories',
'directory=s' => 'Directory where the project was created',
'table=s' => 'Table to migrate. Table name or table prefix with asterisk. Default: all',
'version=s' => 'Version to migrate',
'descr=s' => 'Migration description (used for timestamp based migration)',
'data=s' => 'Export data [always|oncreate] (Import data when run migration)',
'force' => 'Forces to overwrite existing migrations',
'ts-based' => 'Timestamp based migration version',
'log-in-db' => 'Keep migrations log in the database table rather than in file',
'dry' => 'Attempt requested operation without making changes to system (Generating only)',
'verbose' => 'Output of debugging information during operation (Running only)',
'no-auto-increment' => 'Disable auto increment (Generating only)',
'help' => 'Shows this help [optional]',
'help' => 'Shows this help [optional]',
];
}

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


//multiple dir
$migrationsDir = [];
if ($this->isReceivedOption('migrations')) {
$migrationsDir = $path . $this->getOption('migrations');
$migrationsDir = explode(',', $this->getOption('migrations'));
} elseif (isset($config['application']['migrationsDir'])) {
$migrationsDir = $config['application']['migrationsDir'];
if (!$this->path->isAbsolutePath($migrationsDir)) {
$migrationsDir = $path . $migrationsDir;
$migrationsDir = explode(',', $config['application']['migrationsDir']);
}


if (!empty($migrationsDir)) {
foreach ($migrationsDir as $id => $dir) {
if (!$this->path->isAbsolutePath($dir)) {
$migrationsDir[$id] = $path . $dir;
}
}
} elseif (file_exists($path . 'app')) {
$migrationsDir = $path . 'app/migrations';
$migrationsDir[] = $path . 'app/migrations';
} elseif (file_exists($path . 'apps')) {
$migrationsDir = $path . 'apps/migrations';
$migrationsDir[] = $path . 'apps/migrations';
} else {
$migrationsDir = $path . 'migrations';
$migrationsDir[] = $path . 'migrations';
}


// keep migrations log in db
// either "log-in-db" option or "logInDb" config variable from "application" block
$migrationsInDb = false;
Expand Down
32 changes: 28 additions & 4 deletions scripts/Phalcon/Console/OptionParserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ trait OptionParserTrait
/**
* Get prefix from the option
*
* @param string $prefix
* @param mixed $prefixEnd
* @param string $prefix
* @param mixed $prefixEnd
*
* @return mixed
*/
Expand Down Expand Up @@ -74,12 +74,36 @@ public function getVersionNameGeneratingMigration()
} elseif ($this->options['version']) {
VersionCollection::setType(VersionCollection::TYPE_INCREMENTAL);
$versionItem = VersionCollection::createItem($this->options['version']);
//check version is exist
$migrationsDirList = $this->options['migrationsDir'];
if (is_array($migrationsDirList)) {
foreach ($migrationsDirList as $migrationsDir) {
$migrationsDirList = ModelMigration::scanForVersions($migrationsDir);
if (is_array($migrationsDirList)) {
foreach ($migrationsDirList as $item) {
if ($item->getVersion() != $versionItem->getVersion()) {
continue;
}
if (!$this->options['force']) {
throw new \LogicException('Version ' . $item->getVersion() . ' already exists');
} else {
rmdir(rtrim($migrationsDir, '\\/') . DIRECTORY_SEPARATOR . $versionItem->getVersion());
}
}
}
}
}

// The version is guessed automatically
} else {
VersionCollection::setType(VersionCollection::TYPE_INCREMENTAL);
$versionItems = ModelMigration::scanForVersions($this->options['migrationsDir']);

$versionItems = [];
$migrationsDirList = $this->options['migrationsDir'];
if (is_array($migrationsDirList)) {
foreach ($migrationsDirList as $migrationsDir) {
$versionItems = $versionItems + ModelMigration::scanForVersions($migrationsDir);
}
}
if (!isset($versionItems[0])) {
$versionItem = VersionCollection::createItem('1.0.0');
} else {
Expand Down
Loading

0 comments on commit 5ec8a37

Please sign in to comment.