Skip to content

Commit

Permalink
Merge pull request #6 from mybuilder/symfony-4-upgrade-fix
Browse files Browse the repository at this point in the history
Add Symfony 4 support, fix Symfony 3/4 deprecation notices and drop support for Symfony 2 (and PHP < 7.3)
  • Loading branch information
Gavin Love authored May 5, 2020
2 parents e76a7c0 + 2bdd04d commit a2d3cd1
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 152 deletions.
38 changes: 26 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
language: php

php:
- 7.0
- 7.3
cache:
directories:
- $HOME/.composer/cache/files
- .phpunit

env:
- SYMFONY_VERSION=2.7.*
- SYMFONY_VERSION=3.0.*
- SYMFONY_VERSION=3.4.*
- SYMFONY_VERSION=4.0.*
- SYMFONY_VERSION=4.2.*
global:
- SYMFONY_PHPUNIT_DIR=.phpunit

before_script:
- composer update symfony/console:${SYMFONY_VERSION}
- composer update symfony/framework-bundle:${SYMFONY_VERSION}
matrix:
fast_finish: true
include:
- php: 7.3
env: SYMFONY_VERSION="3.4.*"
- php: 7.3
env: SYMFONY_VERSION="4.4.*"
- php: 7.4
env: SYMFONY_VERSION="3.4.*"
- php: 7.4
env: SYMFONY_VERSION="4.4.*"

script: bin/phpunit
before_install:
- composer self-update
- if [ "$SYMFONY_VERSION" != "" ]; then composer require --dev --no-update symfony/framework-bundle:"${SYMFONY_VERSION}"; fi
- if [ "$SYMFONY_VERSION" != "" ]; then composer require --dev --no-update symfony/console:"${SYMFONY_VERSION}"; fi

install:
- composer update $COMPOSER_FLAGS

script: bin/phpunit -v
16 changes: 4 additions & 12 deletions Annotation/Supervisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,15 @@
*/
class Supervisor
{
/**
* @var integer
*/
/** @var integer */
public $processes;

/**
* @var string
*/
/** @var string */
public $params;

/**
* @var string
*/
/** @var string */
public $executor;

/**
* @var string
*/
/** @var string */
public $server;
}
9 changes: 5 additions & 4 deletions Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MyBuilder\Bundle\SupervisorBundle\Command;

use MyBuilder\Bundle\SupervisorBundle\Exporter\AnnotationSupervisorExporter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -27,12 +28,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$commands = $this->getApplication()->all();

/** @var AnnotationSupervisorExporter $exporter */
$exporter = $this->getContainer()->get('mybuilder.supervisor_bundle.annotation_supervisor_exporter');

$output->write($exporter->export($commands, $this->parseOptions($input)));
}

private function parseOptions(InputInterface $input)
private function parseOptions(InputInterface $input): array
{
$options = [];

Expand All @@ -51,13 +53,12 @@ private function parseOptions(InputInterface $input)
return $options;
}

/**
* @throws \LogicException
*/
/** @throws \LogicException */
protected function getContainer(): ContainerInterface
{
if (null === $this->container) {
$application = $this->getApplication();

if (null === $application) {
throw new \LogicException('The container cannot be retrieved as the application instance is not yet set.');
}
Expand Down
15 changes: 11 additions & 4 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('my_builder_supervisor');
if (method_exists(TreeBuilder::class, 'getRootNode')) {
// Symfony 4
$treeBuilder = new TreeBuilder('my_builder_supervisor');
$rootNode = $treeBuilder->getRootNode();
} else {
// Symfony 3
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('my_builder_supervisor');
}

$rootNode
->children()
->arrayNode('exporter')
->children()
->variableNode('program')->end()
->scalarNode('executor')->example('php')->end()
->scalarNode('console')->example('app/console (bin/console)')->end()
->scalarNode('console')->example('bin/console')->end()
->end()
->end();

Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/MyBuilderSupervisorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');

$exporterConfig = isset($config['exporter']) ? $config['exporter'] : array();
$exporterConfig = $config['exporter'] ?? [];
$container->setParameter('mybuilder.supervisor_bundle.exporter_config', $exporterConfig);
}
}
93 changes: 51 additions & 42 deletions Exporter/AnnotationSupervisorExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

class AnnotationSupervisorExporter
{
/** @var Reader */
private $reader;

/** @var array */
private $config;

public function __construct(Reader $reader, array $config)
Expand All @@ -25,7 +28,7 @@ public function __construct(Reader $reader, array $config)
*
* @return string The supervisor configuration
*/
public function export(array $commands, array $options)
public function export(array $commands, array $options): string
{
$programs = $this->toParsedPrograms($commands, $options);

Expand All @@ -41,46 +44,49 @@ public function export(array $commands, array $options)
private function toParsedPrograms(array $commands, array $options)
{
$config = isset($this->config['program']) ? (array) $this->config['program'] : [];
list($env, $server, $options) = $this->takeEnvironmentAndServerFrom($options);
[$env, $server, $options] = $this->takeEnvironmentAndServerFrom($options);
$config += $options;

return array_reduce($commands, function (array $programs, Command $command) use ($env, $server, $config)
{
foreach ($this->toSupervisorAnnotations($command, $server) as $instance => $annotation) {
$programs[] = $config + [
'name' => $this->buildProgramName($command->getName(), $instance),
'command' => $this->buildCommand($command->getName(), $env, $annotation),
'numprocs' => $annotation->processes,
];
}

return $programs;
}, []);
return array_reduce(
$commands,
function (array $programs, Command $command) use ($env, $server, $config) {
foreach ($this->toSupervisorAnnotations($command, $server) as $instance => $annotation) {
$programs[] = $config + [
'name' => $this->buildProgramName($command->getName(), $instance),
'command' => $this->buildCommand($command->getName(), $env, $annotation),
'numprocs' => $annotation->processes,
];
}

return $programs;
}, []
);
}

private function takeEnvironmentAndServerFrom(array $options)
private function takeEnvironmentAndServerFrom(array $options): array
{
$env = isset($options['environment']) ? $options['environment'] : null;
$server = isset($options['server']) ? $options['server'] : null;
$env = $options['environment'] ?? null;
$server = $options['server'] ?? null;

unset($options['environment']);
unset($options['server']);
unset($options['environment'], $options['server']);

return [ $env, $server, $options ];
return [$env, $server, $options];
}

private function toSupervisorAnnotations(Command $command, $server)
private function toSupervisorAnnotations(Command $command, $server): array
{
$annotations = $this->reader->getClassAnnotations(new \ReflectionClass($command));

$filtered = array_filter($annotations, function ($annotation) use ($server)
{
if ($annotation instanceof SupervisorAnnotation) {
return null === $server || $server === $annotation->server;
}
$filtered = array_filter(
$annotations,
static function ($annotation) use ($server) {
if ($annotation instanceof SupervisorAnnotation) {
return null === $server || $server === $annotation->server;
}

return false;
});
return false;
}
);

if (empty($filtered)) {
return [];
Expand All @@ -89,21 +95,21 @@ private function toSupervisorAnnotations(Command $command, $server)
return array_combine(range(1, count($filtered)), array_values($filtered));
}

private function buildProgramName($commandName, $instance)
private function buildProgramName(string $commandName, int $instance): string
{
$name = str_replace(':', '_', $commandName);

return (1 === $instance) ? $name : "{$name}_{$instance}";
}

private function buildCommand($commandName, $environment, SupervisorAnnotation $annotation)
private function buildCommand(string $commandName, string $environment, SupervisorAnnotation $annotation): string
{
$executor = '';

if ($annotation->executor) {
$executor = $annotation->executor;
} else if (isset($this->config['executor'])) {
} elseif (isset($this->config['executor'])) {
$executor = $this->config['executor'];
} else {
$executor = '';
}

$console = isset($this->config['console']) ? " {$this->config['console']}" : '';
Expand All @@ -113,21 +119,24 @@ private function buildCommand($commandName, $environment, SupervisorAnnotation $
return $executor . $console . ' ' . $commandName . $params . $env;
}

private function buildConfiguration(array $programs)
private function buildConfiguration(array $programs): Configuration
{
$config = new Configuration;
$config->registerProcessor(new CommandConfigurationProcessor);

return array_reduce($programs, function (Configuration $config, $program) {
$command = new \Francodacosta\Supervisord\Command;
return array_reduce(
$programs,
static function (Configuration $config, $program) {
$command = new \Francodacosta\Supervisord\Command;

foreach ($program as $k => $v) {
$command->set($k, $v);
}
foreach ($program as $k => $v) {
$command->set($k, $v);
}

$config->add($command);
$config->add($command);

return $config;
}, $config);
return $config;
}, $config
);
}
}
4 changes: 3 additions & 1 deletion MyBuilderSupervisorBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

use Symfony\Component\HttpKernel\Bundle\Bundle;

class MyBuilderSupervisorBundle extends Bundle {}
class MyBuilderSupervisorBundle extends Bundle
{
}
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://travis-ci.org/mybuilder/supervisor-bundle.svg?branch=master)](https://travis-ci.org/mybuilder/supervisor-bundle)

A bundle for Symfony 2/3/4 which allows you to use `@Supervisor` annotations to configure how [Supervisor](http://supervisord.org/) runs your console commands.
A bundle for Symfony 3/4 which allows you to use `@Supervisor` annotations to configure how [Supervisor](http://supervisord.org/) runs your console commands.

## Installation

Expand All @@ -16,19 +16,28 @@ $ php composer.phar require mybuilder/supervisor-bundle

### Enable the bundle

Enable the bundle in the `app/AppKernel.php`:
Enable the bundle in the `app/AppKernel.php` for Symfony 3:

``` php
public function registerBundles() {
$bundles = [
public function registerBundles(): array
{
return [
new MyBuilder\Bundle\SupervisorBundle\MyBuilderSupervisorBundle(),
];
}
```

Enable the bundle in the `config/bundles.php` for Symfony 4:

```php
return [
MyBuilder\Bundle\SupervisorBundle\MyBuilderSupervisorBundle::class => ['all' => true],
];
```

### Configure the bundle

You can add the following to your `config.yml` to define your global export configuration.
You can add the following to your `config.yml` (Symfony 3) / `packages/my_builder_supervisor.yaml` (Symfony 4) to define your global export configuration.

```yaml
my_builder_supervisor:
Expand All @@ -41,7 +50,7 @@ my_builder_supervisor:
executor: php

# allows you to specify the console that all commands should be passed to
console: app/console
console: bin/console
```
## Usage
Expand All @@ -67,11 +76,11 @@ class SendQueuedEmailsCommand extends Command {}

## Exporting the Supervisor configuration

You should run `app/console supervisor:dump` and review what the Supervisor configuration will look like based on the current specified definition.
You should run `bin/console supervisor:dump` and review what the Supervisor configuration will look like based on the current specified definition.
If you are happy with this you can write out the configuration to a `conf` file:

```
$ app/console supervisor:dump --user=mybuilder --server=web > "/etc/supervisor.d/symfony.conf"
$ bin/console supervisor:dump --user=mybuilder --server=web > "/etc/supervisor.d/symfony.conf"
```

And then reload Supervisor:
Expand All @@ -85,7 +94,7 @@ $ kill -SIGHUP $(supervisorctl pid)
You can choose which environment you want to run the commands in Supervisor under like this:

```
$ app/console supervisor:dump --server=web --env=prod
$ bin/console supervisor:dump --server=web --env=prod
```

---
Expand Down
1 change: 0 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
services:

mybuilder.supervisor_bundle.annotation_supervisor_exporter:
class: MyBuilder\Bundle\SupervisorBundle\Exporter\AnnotationSupervisorExporter
public: true
Expand Down
Loading

0 comments on commit a2d3cd1

Please sign in to comment.