Skip to content

Commit 820297d

Browse files
authored
Merge pull request #153 from joomlatools/feature/152-commands
Load extra commands from home directory
2 parents 5707c46 + c2ea5db commit 820297d

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/Joomlatools/Console/Application.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public function run(Input\InputInterface $input = null, Output\OutputInterface $
7979

8080
$this->_loadPlugins();
8181

82+
$this->_loadExtraCommands();
83+
8284
parent::run($this->_input, $this->_output);
8385
}
8486

@@ -204,6 +206,51 @@ public function getPlugins()
204206
return $this->_plugins;
205207
}
206208

209+
/**
210+
* Loads extra commands from the ~/.joomlatools/console/commands/ folder
211+
*
212+
* Each PHP file in the folder is included and if the class in the file extends the base Symfony command
213+
* it's instantiated and added to the app.
214+
*
215+
* @return void
216+
*/
217+
protected function _loadExtraCommands()
218+
{
219+
$path = $this->getConsoleHome().'/commands';
220+
221+
if (\is_dir($path))
222+
{
223+
$iterator = new \DirectoryIterator($path);
224+
225+
foreach ($iterator as $file)
226+
{
227+
if ($file->getExtension() == 'php')
228+
{
229+
require $file->getPathname();
230+
231+
$className = $file->getBasename('.php');
232+
233+
if (\class_exists($className))
234+
{
235+
$reflection = new \ReflectionClass($className);
236+
237+
if (!$reflection->isSubclassOf('\Symfony\Component\Console\Command\Command')) {
238+
continue;
239+
}
240+
241+
$command = new $className();
242+
243+
if (!$command instanceof \Symfony\Component\Console\Command\Command) {
244+
continue;
245+
}
246+
247+
$this->add($command);
248+
}
249+
}
250+
}
251+
}
252+
}
253+
207254
/**
208255
* Set up environment
209256
*/
@@ -227,7 +274,7 @@ protected function _setup()
227274

228275
if (file_exists($old))
229276
{
230-
$this->_output->writeln('<comment>Moving legacy plugin directory to ~/.joomlatools-console/plugins.</comment>');
277+
$this->_output->writeln('<comment>Moving legacy plugin directory to ~/.joomlatools/console/plugins.</comment>');
231278

232279
$cmd = sprintf('mv %s %s', escapeshellarg($old), escapeshellarg($this->getPluginPath()));
233280
exec($cmd);

src/Joomlatools/Console/Command/Site/Create.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,26 @@ protected function execute(InputInterface $input, OutputInterface $output)
206206
$command->run(new ArrayInput($arguments), $output);
207207
}
208208

209+
/*
210+
* Run all site:create:* commands after site creation
211+
*/
212+
try {
213+
$commands = $this->getApplication()->all('site:create');
214+
215+
foreach ($commands as $command) {
216+
$arguments = array(
217+
$command->getName(),
218+
'site' => $this->site,
219+
'--www' => $this->www
220+
);
221+
$command->setApplication($this->getApplication());
222+
$command->run(new ArrayInput($arguments), $output);
223+
}
224+
}
225+
catch (\Symfony\Component\Console\Exception\NamespaceNotFoundException $e) {}
226+
catch (\Symfony\Component\Console\Exception\CommandNotFoundException $e) {}
227+
228+
209229
return 0;
210230
}
211231

0 commit comments

Comments
 (0)