From 9410e406f2254881319858cc36c4f6a80de486b8 Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Sat, 9 Apr 2016 09:27:54 -0700 Subject: [PATCH] Use CommandFileDiscovery class from Annotation Commands. --- composer.lock | 21 +++++++------ includes/preflight.inc | 31 ++++++++++--------- lib/Drush/Boot/DrupalBoot.php | 10 +++++- .../EvalCommandFile.php} | 4 +-- .../InitCommandFile.php} | 4 +-- 5 files changed, 41 insertions(+), 29 deletions(-) rename lib/Drush/{Command/EvalCommand.php => CommandFiles/EvalCommandFile.php} (94%) rename lib/Drush/{Command/InitCommand.php => CommandFiles/InitCommandFile.php} (98%) diff --git a/composer.lock b/composer.lock index 137c09be28..225cd984bc 100644 --- a/composer.lock +++ b/composer.lock @@ -13,12 +13,12 @@ "source": { "type": "git", "url": "https://github.com/Codegyre/Robo.git", - "reference": "55bddda6a87a2f09456cda3f5a6f401816bf7c9c" + "reference": "1fdb60308faabcdbc1f930fdeccf2a2865ee8931" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Codegyre/Robo/zipball/55bddda6a87a2f09456cda3f5a6f401816bf7c9c", - "reference": "55bddda6a87a2f09456cda3f5a6f401816bf7c9c", + "url": "https://api.github.com/repos/Codegyre/Robo/zipball/1fdb60308faabcdbc1f930fdeccf2a2865ee8931", + "reference": "1fdb60308faabcdbc1f930fdeccf2a2865ee8931", "shasum": "" }, "require": { @@ -68,27 +68,28 @@ } ], "description": "Modern task runner", - "time": "2016-04-07 01:27:02" + "time": "2016-04-08 01:08:14" }, { "name": "consolidation/annotation-command", - "version": "0.5.0", + "version": "0.6.1", "source": { "type": "git", "url": "https://github.com/consolidation-org/annotation-command.git", - "reference": "96a43474a3a73ac877f5bf1f93ce03f609a992ac" + "reference": "712975a56880ee569e3a6ceecc3c4ebc25a9fea0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/consolidation-org/annotation-command/zipball/96a43474a3a73ac877f5bf1f93ce03f609a992ac", - "reference": "96a43474a3a73ac877f5bf1f93ce03f609a992ac", + "url": "https://api.github.com/repos/consolidation-org/annotation-command/zipball/712975a56880ee569e3a6ceecc3c4ebc25a9fea0", + "reference": "712975a56880ee569e3a6ceecc3c4ebc25a9fea0", "shasum": "" }, "require": { "php": ">=5.5.0", "phpdocumentor/reflection-docblock": "~2", "psr/log": "~1.0", - "symfony/console": "~2.5|~3.0" + "symfony/console": "~2.5|~3.0", + "symfony/finder": "~2.5|~3.0" }, "require-dev": { "phpunit/phpunit": "4.*", @@ -116,7 +117,7 @@ } ], "description": "Initialize Symfony Console commands from annotated command class methods.", - "time": "2016-04-06 17:16:46" + "time": "2016-04-09 05:24:19" }, { "name": "consolidation/bootstrap", diff --git a/includes/preflight.inc b/includes/preflight.inc index 7473e7fbe6..9b34d8ff17 100644 --- a/includes/preflight.inc +++ b/includes/preflight.inc @@ -186,16 +186,9 @@ function drush_preflight_prepare() { // or do much else until after this is done. $container = drush_init_dependency_injection_container(); - // TODO: Where will we get our list of built-in annotation commands? - // Maybe list the contents of lib/Drush/Commands? We probably want - // to support placing the commands in subfolders. - $commandFileClassNames = [ - \Drush\Command\InitCommand::class, - \Drush\Command\EvalCommand::class, - ]; - - // Add our annotation commands to the application. - drush_init_annotation_commands($container, $commandFileClassNames); + // Add our core annotation command files to the application. + drush_init_annotation_commands($container); + drush_init_application_global_options($container); // Terminate immediately unless invoked as a command line script if (!drush_verify_cli()) { @@ -282,6 +275,9 @@ function drush_init_dependency_injection_container($input = null, $output = null ->withArgument(\Drush::getVersion()) ->withMethodCall('setDispatcher', ['eventDispatcher']) ->withMethodCall('setAutoExit', [false]); + $container->share('commandDiscovery', 'Consolidation\AnnotationCommand\CommandFileDiscovery') + ->withMethodCall('addSearchLocation', ['CommandFiles']) + ->withMethodCall('setSearchPattern', ['#.*(Commands|CommandFile).php$#']); $container->share('formatterManager', 'Consolidation\OutputFormatters\FormatterManager'); $container->share('hookManager', 'Consolidation\AnnotationCommand\HookManager'); $container->share('commandProcessor', 'Consolidation\AnnotationCommand\CommandProcessor') @@ -300,7 +296,8 @@ function drush_init_dependency_injection_container($input = null, $output = null } // TODO: Where should this go? -function drush_init_global_options($application) { +function drush_init_application_global_options($container) { + $application = $container->get('application'); $definition = $application->getDefinition(); // TODO: We should make a better way to manage global options @@ -345,10 +342,17 @@ function drush_init_global_options($application) { } } -function drush_init_annotation_commands($container, $commandFileClassNames) { +function drush_init_annotation_commands($container) { + $application = $container->get('application'); + $discovery = $container->get('commandDiscovery'); + $commandFiles = $discovery->discover(DRUSH_BASE_PATH . '/lib/Drush', '\Drush'); + drush_init_register_command_files($container, $commandFiles); +} + +function drush_init_register_command_files($container, $commandFiles) { $application = $container->get('application'); $commandFactory = $container->get('commandFactory'); - foreach ($commandFileClassNames as $className) { + foreach ($commandFiles as $sourcePath => $className) { $classAlias = str_replace('\\', '', $className); // Add and fetch our class from the container to apply the inductors @@ -359,7 +363,6 @@ function drush_init_annotation_commands($container, $commandFileClassNames) { $application->add($command); } } - drush_init_global_options($application); } /** diff --git a/lib/Drush/Boot/DrupalBoot.php b/lib/Drush/Boot/DrupalBoot.php index 8cacb9338a..6baffd4a13 100644 --- a/lib/Drush/Boot/DrupalBoot.php +++ b/lib/Drush/Boot/DrupalBoot.php @@ -106,13 +106,17 @@ function commandfile_searchpaths($phase, $phase_max = FALSE) { $phase_max = $phase; } - $searchpath = array(); + $container = \Drush::getContainer(); + $discovery = $container->get('commandDiscovery'); + $commandFiles = []; + $searchpath = []; switch ($phase) { case DRUSH_BOOTSTRAP_DRUPAL_ROOT: $drupal_root = \Drush::bootstrapManager()->getRoot(); $searchpath[] = $drupal_root . '/../drush'; $searchpath[] = $drupal_root . '/drush'; $searchpath[] = $drupal_root . '/sites/all/drush'; + $commandFiles = $discovery->discover($searchpath, '\Drupal'); break; case DRUSH_BOOTSTRAP_DRUPAL_SITE: // If we are going to stop bootstrapping at the site, then @@ -141,6 +145,7 @@ function commandfile_searchpaths($phase, $phase_max = FALSE) { } $searchpath = array_merge($searchpath, $this->contrib_themes_paths()); + $commandFiles = $discovery->discoverNamespaced($searchpath, '\Drupal'); } break; case DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION: @@ -165,8 +170,11 @@ function commandfile_searchpaths($phase, $phase_max = FALSE) { foreach (drush_theme_list() as $key => $value) { $searchpath[] = drupal_get_path('theme', $key); } + $commandFiles = $discovery->discoverNamespaced($searchpath, '\Drupal'); break; } + // A little inelegant, but will do for now. + drush_init_register_command_files($container, $commandFiles); return $searchpath; } diff --git a/lib/Drush/Command/EvalCommand.php b/lib/Drush/CommandFiles/EvalCommandFile.php similarity index 94% rename from lib/Drush/Command/EvalCommand.php rename to lib/Drush/CommandFiles/EvalCommandFile.php index b930db309e..faa6368be9 100644 --- a/lib/Drush/Command/EvalCommand.php +++ b/lib/Drush/CommandFiles/EvalCommandFile.php @@ -1,12 +1,12 @@