Skip to content

Commit

Permalink
Add Symfony 4 support, fix Symfony 3/4 deprecation notices and drop s…
Browse files Browse the repository at this point in the history
…upport for Symfony 2 (and PHP < 7.3) (#34)

* Only use current in-life versions of PHP and Symfony

* Fix unit tests

* Fix symfony 4 deprecations

* Update code to match current standards

* Update to PHPUnit 9

* Ensure use static asserts

* Downgrade to PHPunit 8 to support php 7.2

* Drop php 7.2 support

* Check PHP 7.4 support

* Update composer json to reflect version changes

* Specify reason for TreeBuilder change

* Add Symfony 4 instructions

* Ensure test cleans up after itself
  • Loading branch information
angelsk authored May 5, 2020
1 parent dbb6a29 commit b3871f7
Show file tree
Hide file tree
Showing 21 changed files with 223 additions and 240 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ composer.phar
bin/
vendor/
/Tests/Fixtures/app/cache/
var/
25 changes: 6 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,14 @@ env:
matrix:
fast_finish: true
include:
- php: 7.1
env: SYMFONY_VERSION="2.7.*"
- php: 7.1
env: SYMFONY_VERSION="2.8.*"
- php: 7.1
- php: 7.3
env: SYMFONY_VERSION="3.4.*"
- php: 7.1
env: SYMFONY_VERSION="4.0.*"
- php: 7.1
env: SYMFONY_VERSION="4.1.*"
# Test latest PHP against all symfony versions
- php: 7.2
env: SYMFONY_VERSION="2.7.*"
- php: 7.2
env: SYMFONY_VERSION="2.8.*"
- php: 7.2
- php: 7.3
env: SYMFONY_VERSION="4.4.*"
- php: 7.4
env: SYMFONY_VERSION="3.4.*"
- php: 7.2
env: SYMFONY_VERSION="4.0.*"
- php: 7.2
env: SYMFONY_VERSION="4.1.*"
- php: 7.4
env: SYMFONY_VERSION="4.4.*"

before_install:
- composer self-update
Expand Down
40 changes: 10 additions & 30 deletions Annotation/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,28 @@
*/
class Cron extends Annotation
{
/**
* @var string
*/
/** @var string */
public $minute;

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

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

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

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

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

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

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

/**
Expand All @@ -66,13 +50,9 @@ class Cron extends Annotation
*/
public $server;

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

/**
* @var string
*/
/** @var string */
public $executor;
}
52 changes: 35 additions & 17 deletions Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,62 @@

use MyBuilder\Bundle\CronosBundle\Exporter\AnnotationCronExporter;
use MyBuilder\Cronos\Formatter\Cron;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;

class CommandBase extends ContainerAwareCommand
class CommandBase extends Command implements ContainerAwareInterface
{
protected function addServerOption()
use ContainerAwareTrait;

protected function addServerOption(): void
{
$this
->addOption('server', null, InputOption::VALUE_REQUIRED, 'Only include cron jobs for the specified server', AnnotationCronExporter::ALL_SERVERS);
}

/**
* Configure cron export
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return Cron
*/
protected function configureCronExport(InputInterface $input, OutputInterface $output)
protected function configureCronExport(InputInterface $input, OutputInterface $output): Cron
{
$options = array(
$options = [
'serverName' => $input->getOption('server'),
'environment' => $input->getOption('env')
);
'environment' => $input->getOption('env'),
];

$output->writeln(sprintf('Server <comment>%s</comment>', $options['serverName']));
$cron = $this->exportCron($options);
$output->writeln(sprintf('<Comment>Found %d lines<comment>', $cron->countLines()));
$output->writeln(sprintf('<comment>Found %d lines</comment>', $cron->countLines()));

return $cron;
}

private function exportCron($options)
/** @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.');
}

$this->container = $application->getKernel()->getContainer();

if (null === $this->container) {
throw new \LogicException('The container cannot be retrieved as the kernel has shut down.');
}
}

return $this->container;
}

private function exportCron($options): Cron
{
$commands = $this->getApplication()->all();
/** @var AnnotationCronExporter $exporter */
$exporter = $this->getContainer()->get('mybuilder.cronos_bundle.annotation_cron_exporter');

return $exporter->export($commands, $options);
Expand Down
2 changes: 1 addition & 1 deletion Command/ReplaceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->getContainer()->get('mybuilder.cronos_bundle.cron_process_updater')->updateWith($cron, $key);
$output->writeln(sprintf('<info>Cron successfully updated with key </info><comment>%s</comment>', $key));
} catch (\RuntimeException $e) {
$output->writeln(sprintf('<Comment>Cron cannot be updated - %s<comment>', $e->getMessage()));
$output->writeln(sprintf('<comment>Cron cannot be updated - %s</comment>', $e->getMessage()));
}
}

Expand Down
13 changes: 10 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@

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

$rootNode
->children()
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/MyBuilderCronosExtension.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.cronos_bundle.exporter_config', $exporterConfig);
}
}
70 changes: 30 additions & 40 deletions Exporter/AnnotationCronExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,39 @@

namespace MyBuilder\Bundle\CronosBundle\Exporter;

use Doctrine\Common\Annotations\Annotation;
use Doctrine\Common\Annotations\Reader;
use MyBuilder\Bundle\CronosBundle\Annotation\Cron as CronAnnotation;
use MyBuilder\Cronos\Formatter\Cron as CronFormatter;
use Symfony\Component\Console\Command\Command;

class AnnotationCronExporter
{
const ALL_SERVERS = 'all';
public const ALL_SERVERS = 'all';

/** @var Reader */
private $annotationsReader;
private $config = array();

public function __construct($annotationsReader)
/** @var array */
private $config = [];

public function __construct(Reader $annotationsReader)
{
$this->annotationsReader = $annotationsReader;
}

/**
* Set the config
*
* @param array $config
*/
public function setConfig(array $config)
public function setConfig(array $config): void
{
$this->config = $config;
}

/**
* Export the cron for the given commands and server
*
* @param array $commands
* @param array $options
*
* @return CronFormatter
*/
public function export(array $commands, array $options)
public function export(array $commands, array $options): CronFormatter
{
$cron = $this->createCronConfiguration();

foreach ($commands as $command) {
if ($command instanceof Command) {
$cron = $this->parseAnnotations($cron, $command, $options);
Expand All @@ -48,20 +44,16 @@ public function export(array $commands, array $options)
return $cron;
}

/**
* Create and configure Cron
*
* @return CronFormatter
*/
private function createCronConfiguration()
private function createCronConfiguration(): CronFormatter
{
$cron = new CronFormatter;
$configurator = new ArrayHeaderConfigurator($cron->header());
$configurator->configureFrom($this->config);

return $cron;
}

private function parseAnnotations($cron, Command $command, array $options)
private function parseAnnotations(CronFormatter $cron, Command $command, array $options): CronFormatter
{
foreach ($this->getAnnotations($command) as $annotation) {
if ($this->annotationBelongsToServer($annotation, $options['serverName'])) {
Expand All @@ -72,57 +64,55 @@ private function parseAnnotations($cron, Command $command, array $options)
return $cron;
}

private function annotationBelongsToServer($annotation, $serverName)
private function annotationBelongsToServer(Annotation $annotation, string $serverName): bool
{
return
$annotation instanceof CronAnnotation &&
($serverName === self::ALL_SERVERS || $annotation->server === $serverName);
$annotation instanceof CronAnnotation
&& ($serverName === self::ALL_SERVERS || $annotation->server === $serverName);
}

private function addLine($command, $annotation, array $options, $cron)
private function addLine(Command $command, CronAnnotation $annotation, array $options, CronFormatter $cron): CronFormatter
{
if ($annotation->comment !== null) {
$cron->comment($annotation->comment);
}

if ($command->getDescription()) {
$cron->comment($command->getDescription());
}

$line = $cron->job($this->buildCommand($command->getName(), $annotation, $options));

$configurator = new AnnotationLineConfigurator($line);
$configurator->configureFrom($annotation);

return $cron;
}

private function getAnnotations(Command $command)
private function getAnnotations(Command $command): array
{
$reflectedClass = new \ReflectionClass($command);

return $this->annotationsReader->getClassAnnotations($reflectedClass);
}

/**
* build the Command to execute with parameters and environment.
*
* @param string $commandName Name of command to execute
* @param $annotation
* @param array $options
*
* @return string
* Build the Command to execute with parameters and environment.
*/
private function buildCommand($commandName, $annotation, array $options)
private function buildCommand(string $commandName, CronAnnotation $annotation, array $options): string
{
$executor = '';

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

$console = isset($this->config['console']) ? ' ' . $this->config['console'] : '';
$environment = isset($options['environment']) ? ' --env=' . $options['environment'] : '';
$params = $annotation->params ? ' ' . $annotation->params : '';

return $executor . $console . ' ' . $commandName . $params . $environment;
}
}
}
Loading

0 comments on commit b3871f7

Please sign in to comment.