Skip to content

Commit

Permalink
Use CommandFileDiscovery class from Annotation Commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson committed Apr 9, 2016
1 parent d931ac3 commit 9410e40
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
21 changes: 11 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 17 additions & 14 deletions includes/preflight.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -359,7 +363,6 @@ function drush_init_annotation_commands($container, $commandFileClassNames) {
$application->add($command);
}
}
drush_init_global_options($application);
}

/**
Expand Down
10 changes: 9 additions & 1 deletion lib/Drush/Boot/DrupalBoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace Drush\Command;
namespace Drush\CommandFiles;

/**
* @file
* Evaluate PHP code.
*/

class EvalCommand
class EvalCommandFile
{
/**
* Evaluate arbitrary php code after bootstrapping Drupal (if available).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Drush\Command;
namespace Drush\CommandFiles;

/**
* @file
Expand All @@ -8,7 +8,7 @@

use Drush\Log\LogLevel;

class InitCommand extends \Robo\Tasks
class InitCommandFile extends \Robo\Tasks
{
/**
* Initialize local Drush configuration
Expand Down

0 comments on commit 9410e40

Please sign in to comment.