Skip to content

Commit

Permalink
Merge pull request #11 from OXIDprojects/service.yaml-support
Browse files Browse the repository at this point in the history
added support for adding commands from services.yaml
  • Loading branch information
keywan-ghadami-oxid authored Nov 26, 2018
2 parents 4c77796 + 4dd84bc commit d1c0dab
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog for OXID Console

## [v5.2.0]
### Added
- Support for commands registered via services.yaml of other composer packages

## [v5.1.0]
### Added
- Support for commands registered via composer.json of other composer packages
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ vendor/bin/oxid list

## Defining your own command
* Class must extend `Symfony\Component\Console\Command\Command` class
* in the composer json of your module
```json
"extra": {
"oxideshop": {
"console-commands" : ["OxidCommunity\\ModuleInternals\\Command\\ModuleFixCommand"]
}
},
* in the service.yaml json of your module (composer package)
```yaml
services:
oxid_community.moduleinternals.module.fix.command:
class: OxidCommunity\ModuleInternals\Command\ModuleFixCommand
tags:
- { name: 'console.command' }
```
### Template for your command:
Expand Down
21 changes: 9 additions & 12 deletions bin/oxid
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,19 @@ try {

$commandCollector = new CommandCollector();
$application = new Application('OXID Console', $version);
$application->addCommands($commandCollector->getAllCommands());
$commands = $commandCollector->getAllCommands();
foreach ($commands as $command) {
try {
$application->add($command);
} catch (Throwable $e) {
print get_class($command) . " not loadad " . $e->getMessage() . "\n" . $e->getTraceAsString();
}
}
$application->run();
} catch (Throwable $ex) {
//console should print errors always
print $ex->getMessage();
print $ex->getTraceAsString();
/*
$i = 0;
foreach ($ex->getTrace() as $frame) {
echo sprintf("#%d %s(%d): %s(%s)\n",
$i++, $frame["file"], $frame["line"],
$frame["function"],
implode(", ", array_map(
function ($e) { return var_export($e, true); }, $frame["args"])));
}
*/
//shop exception hanlder may tke care about error code and logging
//shop exception handler may take care about error code and logging
throw $ex;
}
24 changes: 21 additions & 3 deletions src/Core/CommandCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Module\ModuleList;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

use OxidProfessionalServices\OxidConsole\Command\CacheClearCommand;
use OxidProfessionalServices\OxidConsole\Command\DatabaseUpdateCommand;
Expand Down Expand Up @@ -82,23 +85,38 @@ private function getCommandsFromCore()

private function getCommandsFromComposer(){

$nio = new \Composer\IO\NullIO();
$factory = new \Composer\Factory();
$oConfig = Registry::getConfig();

$localRepository = new InstalledFilesystemRepository(new JsonFile(VENDOR_PATH.'/composer/installed.json'));

$commandsClasses = [];

$packages = $localRepository->getPackages();

$symfonyContainer = new ContainerBuilder();
$loader = new YamlFileLoader($symfonyContainer, new FileLocator());

foreach ($packages as $package) {
//deprecated syntax to be removed
$extra = $package->getExtra();
$oxideshop = isset($extra['oxideshop']) ? $extra['oxideshop'] : [];
$consoleCommands = isset($oxideshop['console-commands']) && is_array($oxideshop['console-commands']) ?
$oxideshop['console-commands'] : [];
foreach ($consoleCommands as $commandClass) {
$commandsClasses[] = new $commandClass;
}
//end of deprecated code

$serviceFile = VENDOR_PATH . $package->getName() . '/services.yaml';
if (file_exists($serviceFile)) {
$loader->load($serviceFile);
}
}
foreach ($symfonyContainer->findTaggedServiceIds('console.command') as $id => $tags) {
$definition = $symfonyContainer->getDefinition($id);
$class = $definition->getClass();
$commandsClasses[] = new $class;
}

return $commandsClasses;
}

Expand Down

0 comments on commit d1c0dab

Please sign in to comment.