11<?php
22
3+ declare (strict_types=1 );
34
45namespace Doctrine \Bundle \MigrationsBundle \Command ;
56
67use Doctrine \Bundle \DoctrineBundle \Command \DoctrineCommand as BaseCommand ;
7- use Doctrine \DBAL \Migrations \Configuration \AbstractFileConfiguration ;
8- use Doctrine \DBAL \Migrations \Configuration \Configuration ;
8+ use Doctrine \Migrations \Configuration \AbstractFileConfiguration ;
9+ use Doctrine \Migrations \Configuration \Configuration ;
10+ use Doctrine \Migrations \Version \Version ;
11+ use ErrorException ;
912use Symfony \Component \DependencyInjection \ContainerAwareInterface ;
1013use Symfony \Component \DependencyInjection \ContainerInterface ;
1114use Symfony \Component \DependencyInjection \Exception \InvalidArgumentException ;
15+ use function error_get_last ;
16+ use function is_dir ;
17+ use function method_exists ;
18+ use function mkdir ;
19+ use function preg_match ;
20+ use function str_replace ;
1221
1322/**
1423 * Base class for Doctrine console commands to extend from.
15- *
16- * @author Fabien Potencier <fabien@symfony.com>
1724 */
1825abstract class DoctrineCommand extends BaseCommand
1926{
20- public static function configureMigrations (ContainerInterface $ container , Configuration $ configuration )
27+ public static function configureMigrations (ContainerInterface $ container , Configuration $ configuration ) : void
2128 {
22- if (!$ configuration ->getMigrationsDirectory ()) {
29+ $ dir = $ configuration ->getMigrationsDirectory ();
30+
31+ if ($ dir === null ) {
2332 $ dir = $ container ->getParameter ('doctrine_migrations.dir_name ' );
24- if (!is_dir ($ dir ) && !@mkdir ($ dir , 0777 , true ) && !is_dir ($ dir )) {
33+
34+ if (! is_dir ($ dir ) && ! @mkdir ($ dir , 0777 , true ) && ! is_dir ($ dir )) {
2535 $ error = error_get_last ();
26- throw new \ErrorException ($ error ['message ' ]);
36+
37+ throw new ErrorException (sprintf (
38+ 'Failed to create directory %s with message %s ' , $ dir , $ error ['message ' ]
39+ ));
2740 }
41+
2842 $ configuration ->setMigrationsDirectory ($ dir );
2943 } else {
30- $ dir = $ configuration ->getMigrationsDirectory ();
3144 // class Kernel has method getKernelParameters with some of the important path parameters
32- $ pathPlaceholderArray = array ('kernel.root_dir ' , 'kernel.cache_dir ' , 'kernel.logs_dir ' );
45+ $ pathPlaceholderArray = ['kernel.root_dir ' , 'kernel.cache_dir ' , 'kernel.logs_dir ' ];
46+
3347 foreach ($ pathPlaceholderArray as $ pathPlaceholder ) {
34- if ($ container ->hasParameter ($ pathPlaceholder ) && preg_match ('/\% ' . $ pathPlaceholder. '\%/ ' , $ dir )) {
35- $ dir = str_replace ( ' % ' . $ pathPlaceholder . ' % ' , $ container -> getParameter ( $ pathPlaceholder ), $ dir ) ;
48+ if (! $ container ->hasParameter ($ pathPlaceholder ) || ! preg_match ('/\% ' . $ pathPlaceholder . '\%/ ' , $ dir )) {
49+ continue ;
3650 }
51+
52+ $ dir = str_replace ('% ' . $ pathPlaceholder . '% ' , $ container ->getParameter ($ pathPlaceholder ), $ dir );
3753 }
38- if (!is_dir ($ dir ) && !@mkdir ($ dir , 0777 , true ) && !is_dir ($ dir )) {
54+
55+ if (! is_dir ($ dir ) && ! @mkdir ($ dir , 0777 , true ) && ! is_dir ($ dir )) {
3956 $ error = error_get_last ();
40- throw new \ErrorException ($ error ['message ' ]);
57+
58+ throw new ErrorException (sprintf (
59+ 'Failed to create directory %s with message %s ' , $ dir , $ error ['message ' ]
60+ ));
4161 }
62+
4263 $ configuration ->setMigrationsDirectory ($ dir );
4364 }
44- if (!$ configuration ->getMigrationsNamespace ()) {
65+
66+ if ($ configuration ->getMigrationsNamespace () === null ) {
4567 $ configuration ->setMigrationsNamespace ($ container ->getParameter ('doctrine_migrations.namespace ' ));
4668 }
47- if (!$ configuration ->getName ()) {
69+
70+ if ($ configuration ->getName () === null ) {
4871 $ configuration ->setName ($ container ->getParameter ('doctrine_migrations.name ' ));
4972 }
73+
5074 // For backward compatibility, need use a table from parameters for overwrite the default configuration
51- if (!($ configuration instanceof AbstractFileConfiguration) || ! $ configuration ->getMigrationsTableName ()) {
75+ if (! ($ configuration instanceof AbstractFileConfiguration) || $ configuration ->getMigrationsTableName () === '' ) {
5276 $ configuration ->setMigrationsTableName ($ container ->getParameter ('doctrine_migrations.table_name ' ));
5377 }
78+
5479 // Migrations is not register from configuration loader
55- if (!($ configuration instanceof AbstractFileConfiguration)) {
56- $ configuration ->registerMigrationsFromDirectory ($ configuration ->getMigrationsDirectory ());
80+ if (! ($ configuration instanceof AbstractFileConfiguration)) {
81+ $ migrationsDirectory = $ configuration ->getMigrationsDirectory ();
82+
83+ if ($ migrationsDirectory !== null ) {
84+ $ configuration ->registerMigrationsFromDirectory ($ migrationsDirectory );
85+ }
5786 }
5887
59- if (method_exists ($ configuration , 'getCustomTemplate ' ) && ! $ configuration ->getCustomTemplate ()) {
88+ if (method_exists ($ configuration , 'getCustomTemplate ' ) && $ configuration ->getCustomTemplate () === null ) {
6089 $ configuration ->setCustomTemplate ($ container ->getParameter ('doctrine_migrations.custom_template ' ));
6190 }
6291
6392 $ organizeMigrations = $ container ->getParameter ('doctrine_migrations.organize_migrations ' );
93+
6494 switch ($ organizeMigrations ) {
6595 case Configuration::VERSIONS_ORGANIZATION_BY_YEAR :
6696 $ configuration ->setMigrationsAreOrganizedByYear (true );
@@ -81,18 +111,19 @@ public static function configureMigrations(ContainerInterface $container, Config
81111 }
82112
83113 /**
84- * @param ContainerInterface $container
85- * @param array $versions
114+ * @param Version[] $versions
86115 *
87116 * Injects the container to migrations aware of it
88117 */
89- private static function injectContainerToMigrations (ContainerInterface $ container , array $ versions )
118+ private static function injectContainerToMigrations (ContainerInterface $ container , array $ versions ) : void
90119 {
91120 foreach ($ versions as $ version ) {
92121 $ migration = $ version ->getMigration ();
93- if ($ migration instanceof ContainerAwareInterface) {
94- $ migration -> setContainer ( $ container ) ;
122+ if (! ( $ migration instanceof ContainerAwareInterface) ) {
123+ continue ;
95124 }
125+
126+ $ migration ->setContainer ($ container );
96127 }
97128 }
98129}
0 commit comments