/** * Add a command to the terminus list of commands * * @param [string] $name The name of the command that will be used in the CLI * @param [string] $class The command implementation * @return [void] */ static function addCommand($name, $class) { $path = preg_split('/\\s+/', $name); $leaf_name = array_pop($path); $full_path = $path; $command = self::getRootCommand(); while (!empty($path)) { $subcommand_name = $path[0]; $subcommand = $command->findSubcommand($path); // Create an empty container if (!$subcommand) { $subcommand = new Dispatcher\CompositeCommand($command, $subcommand_name, new DocParser('')); $command->addSubcommand($subcommand_name, $subcommand); } $command = $subcommand; } $leaf_command = Dispatcher\CommandFactory::create($leaf_name, $class, $command); if (!$command->canHaveSubcommands()) { throw new TerminusException(sprintf("'%s' can't have subcommands.", implode(' ', Dispatcher\getPath($command)))); } $command->addSubcommand($leaf_name, $leaf_command); }
/** * Includes every command file in the commands directory * * @param CompositeCommand $parent The parent command to add the new commands to * * @return void */ private static function loadAllCommands(CompositeCommand $parent) { // Create a list of directories where commands might live. $directories = array(); // Add the directory of core commands first. $directories[] = TERMINUS_ROOT . '/php/Terminus/Commands'; // Find the command directories from the third party plugins directory. foreach (self::getUserPlugins() as $dir) { $directories[] = "{$dir}/Commands/"; } // Include all class files in the command directories. foreach ($directories as $cmd_dir) { if ($cmd_dir && file_exists($cmd_dir)) { $iterator = new \DirectoryIterator($cmd_dir); foreach ($iterator as $file) { if ($file->isFile() && $file->isReadable() && $file->getExtension() == 'php') { include_once $file->getPathname(); } } } } // Find the defined command classes and add them to the given base command. $classes = get_declared_classes(); $options = ['cache' => self::getCache(), 'logger' => self::getLogger(), 'outputter' => self::getOutputter(), 'session' => Session::instance()]; foreach ($classes as $class) { $reflection = new \ReflectionClass($class); if ($reflection->isSubclassOf('Terminus\\Commands\\TerminusCommand')) { Dispatcher\CommandFactory::create($reflection->getName(), $parent, $options); } } }