Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ env:

cache:
directories:
- $HOME/.composer/cache/files
- $HOME/.composer/cache

before_script:
- mkdir -p build/logs
Expand Down
48 changes: 44 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,50 @@ Execute oxrun inside your OXID eShop base directory (or subdirectory) if you wan

# Defining your own command

It is possible to adding new console command via `services.yaml` file. For this you have to deposit in your repo a `/services.yaml` file
There are three methods to add your own commands.

- 1st method in the OXRUN directory.
- 2nd method in the OXID module directory.
- 3rd method over Composer packages.

Each own command is added to a namespace group 'own:'.

### Method: OXRUN directory

The commands are stored in your repo under `oxrun_config/commands/`.

Need the specifications:

- File name must end with `*Command.php`.
- Classname and filename must match.
- Class must be in namespace: `Oxrun\CustomCommand`
- Class must extend from `Symfony\Component\Console\Command\Command`

With the interface `Oxrun\Command\EnableInterface` and the two traits: `\Oxrun\Traits\NeedDatabase` and
`\Oxrun\Traits\NoNeedDatabase`. You can enable a command if has a working database connection.

[Example: OwnOxrunCommand.php](example/OwnOxrunCommand.php)

### Method: OXID module directory

The commands are in the module of a OXID eShop: `oxid-esale/source/modules/*/*/`.

Need the specifications:

- Is in a subfolder that reads: `Commands`, `commands` or `Command`
- File name must end with `*Command.php` or `*command.php`.
- Classname and filename must match.
- Namespace does not matter
- Class must extend from `Symfony\Component\Console\Command\Command`

Recommended path would be: `oxid-esale/source/modules/my/Module/Commands/ExportThingCommand.php`

### Method: Composer packages

It is possible to get command from other composer packages. Via `services.yaml` file. For this you have to deposit in your repo a `/services.yaml` file
and install it with composer.

That's how she looks
That's how looks

```yaml
services:
Expand All @@ -63,12 +103,12 @@ That's how she looks
- { name: 'console.command' }
```

[Template for your command](https://github.com/OXIDprojects/oxrun/blob/composer-command-collector/tests/Oxrun/CommandCollection/testData/HelloWorldCommand.php)
[Template for your command](example/HelloWorldCommand.php)

Example: https://github.com/OXIDprojects/oxid-module-internals/blob/master/services.yaml

Available commands
------------------
==================

##### cache
- [cache:clear](#cacheclear) Clear OXID cache
Expand Down
12 changes: 11 additions & 1 deletion bin/oxrun
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Oxrun;

use Oxrun\CommandCollection\Aggregator;
use Oxrun\CommandCollection\CommandFinder;
use Oxrun\CommandCollection\ContainerCollection;
use Symfony\Component\Console\Output\ConsoleOutput;

Expand All @@ -21,7 +23,15 @@ $autoloader = require_once $OXRUN_VENDOR_PATH . 'autoload.php';
$application = new \Oxrun\Application($autoloader, 'oxrun', '@package_version@');

try {
(new ContainerCollection())->addCommandTo($application);
$commandFinder = (new CommandFinder())
->addRegister(new Aggregator\CommunityPass(), true)
->addRegister(new Aggregator\ModulePass(),true)
->addRegister(new Aggregator\CustomPass(),true)
->addRegister(new Aggregator\OxrunPass());

(new ContainerCollection($commandFinder))
->addCommandTo($application);

} catch (\Exception $e) {
$consoleOutput = new ConsoleOutput();
$consoleOutput->writeln('<error>Command Collection: '.$e->getMessage().'</error>');
Expand Down
Binary file added doc/img/oxruncmds.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions example/OwnOxrunCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Created by PhpStorm.
* User: tobi
* Date: 2018-11-27
* Time: 19:36
*/

namespace Oxrun\CustomCommand;

use Oxrun\Command\EnableInterface;
use Oxrun\Traits\NeedDatabase;
use Oxrun\Traits\NoNeedDatabase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class OwnOxrunCommand
*
* @package Oxrun\CustomCommand
*/
class OwnOxrunCommand extends Command implements EnableInterface
{
/**
* This command does not need a database
*/
use NoNeedDatabase;

/**
* or need database
*/
// use NeedDatabase;

protected function configure()
{
$this
->setName('demo-component:say-hello')
->setDescription('Hello World Command');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Hello World');
}
}
1 change: 0 additions & 1 deletion src/Oxrun/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ public function checkBootstrapOxidInclude($oxBootstrap)
return false;
}


/**
* @return mixed|string
* @throws \Exception
Expand Down
2 changes: 1 addition & 1 deletion src/Oxrun/Command/Misc/GenerateDocumentationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected function writeToc(OutputInterface $output)
$commandOutput = $commandTester->getDisplay();
$commandOutput = json_decode($commandOutput);
$output->writeLn("Available commands");
$output->writeLn("------------------" . PHP_EOL);
$output->writeLn("==================" . PHP_EOL);

$description = [];
array_walk($commandOutput->commands, function ($item) use (&$description) {
Expand Down
187 changes: 187 additions & 0 deletions src/Oxrun/CommandCollection/Aggregator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php
/**
* Created by PhpStorm.
* User: tobi
* Date: 2019-03-07
* Time: 07:05
*/

namespace Oxrun\CommandCollection;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

/**
* Class Aggregator
* @package Oxrun\CommandCollection
*/
abstract class Aggregator implements CompilerPassInterface
{
/**
* @var Definition
*/
private $definition;

/**
* @var ContainerBuilder
*/
private $container;

/**
* @var string
*/
protected $shopDir = '';

/**
* @var string
*/
protected $oxrunConfigDir = '';

/**
* @var OutputInterface
*/
protected $consoleOutput = null;

/**
* Algorithmus to find the Commands
*
* @return void
*/
abstract protected function searchCommands();

/**
* @return string
*/
abstract protected function getPassName();

/**
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
// always first check if the primary service is defined
if (!$container->has('command_container')) {
return;
}

$this->definition = $container->findDefinition('command_container');
$this->container = $container;

$this->searchCommands();
}

/**
* Add the command class to ContainerBuilder
*
* @param string $commandClass
* @param null $filepath
*
* @return void
* @throws \Exception
*/
protected function add($commandClass, $filepath = null)
{
if ($filepath) {
CacheCheck::addFile($filepath);
}

$id = $commandClass;

$definitionCmd = new Definition($commandClass);
$this->container->setDefinition($commandClass, $definitionCmd);

$this->addDefinition($id, $definitionCmd);
}

/**
* @param Definition $definitionCmd
* @throws \Exception
*/
protected function addDefinition($id, Definition $definitionCmd)
{
$commandClass = $definitionCmd->getClass();

if (! $this->isCommandCompatibleClass($commandClass)) {
if ($this->consoleOutput) {
$this->consoleOutput->writeln("<comment>$commandClass is not a compatible command</comment>");
}
return;
}

$definitionCmd->setPublic(false);

$this->definition->addMethodCall('addFromDi', [new Reference($id), $this->getPassName()]);
}

/**
* @return ContainerBuilder
*/
protected function getContainer()
{
return $this->container;
}

/**
* Filter out classes with predefined criteria to be accepted as valid `Command` classes.
*
* A given class should match the following criteria:
* a) Extends `Symfony\Component\Console\Command\Command`;
* b) Is not `Symfony\Component\Console\Command\Command` itself.
*
* @param string $class
*
* @return boolean
* @throws \Exception
*/
private function isCommandCompatibleClass($commandClass)
{
try {
new $commandClass;
} catch (\Error $ex) {
throw new \Exception("Error loading class '$commandClass'. Trace: " . static::class );
}

return is_subclass_of($commandClass, Command::class) && $commandClass !== Command::class;
}

/**
* Check is right configured
*
* @throws \Exception
*/
public function valid()
{
}

/**
* @param string $shopDir
*/
public function setShopDir(string $shopDir)
{
$this->shopDir = $shopDir;
}

/**
* @param string $oxrunConfigDir
*/
public function setOxrunConfigDir(string $oxrunConfigDir)
{
$this->oxrunConfigDir = $oxrunConfigDir;
}

/**
* @param OutputInterface $output
* @return Aggregator
*/
public function setConsoleOutput(OutputInterface $output)
{
$this->consoleOutput = $output;

return $this;
}
}

Loading