From 3d44ea59d879f91f3c3ebc28da7d85165e42ebbb Mon Sep 17 00:00:00 2001 From: "j.guyon" Date: Thu, 6 Dec 2018 16:57:18 +0100 Subject: [PATCH 1/3] Remove deprecated ContainerAware usage in command and controller --- Command/ExecuteCommand.php | 37 ++++++++++++++-------- Command/MonitorCommand.php | 59 +++++++++++++++++++++-------------- Command/UnlockCommand.php | 30 +++++++++++++----- Controller/BaseController.php | 4 +-- Resources/config/services.yml | 15 +++++++++ 5 files changed, 99 insertions(+), 46 deletions(-) diff --git a/Command/ExecuteCommand.php b/Command/ExecuteCommand.php index 7c82e7fb..021943f0 100644 --- a/Command/ExecuteCommand.php +++ b/Command/ExecuteCommand.php @@ -3,7 +3,8 @@ namespace JMose\CommandSchedulerBundle\Command; use Cron\CronExpression; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Bridge\Doctrine\ManagerRegistry; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\StringInput; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -18,7 +19,7 @@ * @author Julien Guyon * @package JMose\CommandSchedulerBundle\Command */ -class ExecuteCommand extends ContainerAwareCommand +class ExecuteCommand extends Command { /** @@ -41,6 +42,25 @@ class ExecuteCommand extends ContainerAwareCommand */ private $commandsVerbosity; + /** + * ExecuteCommand constructor. + * @param ManagerRegistry $managerRegistry + * @param $managerName + * @param $logPath + */ + public function __construct(ManagerRegistry $managerRegistry, $managerName, $logPath) + { + $this->em = $managerRegistry->getManager($managerName); + $this->logPath = $logPath; + + // If logpath is not set to false, append the directory separator to it + if (false !== $this->logPath) { + $this->logPath = rtrim($this->logPath, '/\\').DIRECTORY_SEPARATOR; + } + + parent::__construct(); + } + /** * @inheritdoc */ @@ -63,12 +83,6 @@ protected function configure() protected function initialize(InputInterface $input, OutputInterface $output) { $this->dumpMode = $input->getOption('dump'); - $this->logPath = $this->getContainer()->getParameter('jmose_command_scheduler.log_path'); - - // If logpath is not set to false, append the directory separator to it - if (false !== $this->logPath) { - $this->logPath = rtrim($this->logPath, '/\\').DIRECTORY_SEPARATOR; - } // Store the original verbosity before apply the quiet parameter $this->commandsVerbosity = $output->getVerbosity(); @@ -76,10 +90,6 @@ protected function initialize(InputInterface $input, OutputInterface $output) if (true === $input->getOption('no-output')) { $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); } - - $this->em = $this->getContainer()->get('doctrine')->getManager( - $this->getContainer()->getParameter('jmose_command_scheduler.doctrine_manager') - ); } /** @@ -94,7 +104,8 @@ protected function execute(InputInterface $input, OutputInterface $output) // Before continue, we check that the output file is valid and writable (except for gaufrette) if (false !== $this->logPath && strpos($this->logPath, 'gaufrette:') !== 0 && false === is_writable( $this->logPath - )) { + ) + ) { $output->writeln( ''.$this->logPath. ' not found or not writable. You should override `log_path` in your config.yml'.'' diff --git a/Command/MonitorCommand.php b/Command/MonitorCommand.php index 42771cfe..85084185 100644 --- a/Command/MonitorCommand.php +++ b/Command/MonitorCommand.php @@ -2,7 +2,8 @@ namespace JMose\CommandSchedulerBundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Bridge\Doctrine\ManagerRegistry; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -13,7 +14,7 @@ * @author Daniel Fischer * @package JMose\CommandSchedulerBundle\Command */ -class MonitorCommand extends ContainerAwareCommand +class MonitorCommand extends Command { /** @@ -46,6 +47,32 @@ class MonitorCommand extends ContainerAwareCommand */ private $sendMailIfNoError; + /** + * MonitorCommand constructor. + * @param ManagerRegistry $managerRegistry + * @param $managerName + * @param $lockTimeout + * @param $receiver + * @param $mailSubject + * @param $sendMailIfNoError + */ + public function __construct( + ManagerRegistry $managerRegistry, + $managerName, + $lockTimeout, + $receiver, + $mailSubject, + $sendMailIfNoError + ) { + $this->em = $managerRegistry->getManager($managerName); + $this->lockTimeout = $lockTimeout; + $this->receiver = $receiver; + $this->mailSubject = $mailSubject; + $this->sendMailIfNoError = $sendMailIfNoError; + + parent::__construct(); + } + /** * @inheritdoc */ @@ -58,25 +85,6 @@ protected function configure() ->setHelp('This class is for monitoring all active commands.'); } - /** - * Initialize parameters and services used in execute function - * - * @param InputInterface $input - * @param OutputInterface $output - */ - protected function initialize(InputInterface $input, OutputInterface $output) - { - $this->lockTimeout = $this->getContainer()->getParameter('jmose_command_scheduler.lock_timeout'); - $this->dumpMode = $input->getOption('dump'); - $this->receiver = $this->getContainer()->getParameter('jmose_command_scheduler.monitor_mail'); - $this->mailSubject = $this->getContainer()->getParameter('jmose_command_scheduler.monitor_mail_subject'); - $this->sendMailIfNoError = $this->getContainer()->getParameter('jmose_command_scheduler.send_ok'); - - $this->em = $this->getContainer()->get('doctrine')->getManager( - $this->getContainer()->getParameter('jmose_command_scheduler.doctrine_manager') - ); - } - /** * @param InputInterface $input * @param OutputInterface $output @@ -85,6 +93,7 @@ protected function initialize(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output) { // If not in dump mode and none receiver is set, exit. + $this->dumpMode = $input->getOption('dump'); if (!$this->dumpMode && count($this->receiver) === 0) { $output->writeln('Please add receiver in configuration'); @@ -100,7 +109,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $message = ""; foreach ($failedCommands as $command) { - $message .= sprintf("%s: returncode %s, locked: %s, last execution: %s\n", + $message .= sprintf( + "%s: returncode %s, locked: %s, last execution: %s\n", $command->getName(), $command->getLastReturnCode(), $command->getLocked(), @@ -134,8 +144,8 @@ private function sendMails($message) // prepare email constants $hostname = gethostname(); $subject = $this->getMailSubject(); - $headers = 'From: cron-monitor@' . $hostname . "\r\n" . - 'X-Mailer: PHP/' . phpversion(); + $headers = 'From: cron-monitor@'.$hostname."\r\n". + 'X-Mailer: PHP/'.phpversion(); foreach ($this->receiver as $rcv) { mail(trim($rcv), $subject, $message, $headers); @@ -150,6 +160,7 @@ private function sendMails($message) private function getMailSubject() { $hostname = gethostname(); + return sprintf($this->mailSubject, $hostname, date('Y-m-d H:i:s')); } diff --git a/Command/UnlockCommand.php b/Command/UnlockCommand.php index 50d8f1d9..f679e2f8 100644 --- a/Command/UnlockCommand.php +++ b/Command/UnlockCommand.php @@ -3,7 +3,8 @@ namespace JMose\CommandSchedulerBundle\Command; use JMose\CommandSchedulerBundle\Entity\ScheduledCommand; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Bridge\Doctrine\ManagerRegistry; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -15,7 +16,7 @@ * @author Marcel Pfeiffer * @package JMose\CommandSchedulerBundle\Command */ -class UnlockCommand extends ContainerAwareCommand +class UnlockCommand extends Command { /** @@ -23,6 +24,11 @@ class UnlockCommand extends ContainerAwareCommand */ private $em; + /** + * @var integer + */ + private $defaultLockTimeout; + /** * @var integer|boolean Number of seconds after a command is considered as timeout */ @@ -38,6 +44,20 @@ class UnlockCommand extends ContainerAwareCommand */ private $scheduledCommandName = []; + /** + * UnlockCommand constructor. + * @param ManagerRegistry $managerRegistry + * @param $managerName + * @param $lockTimeout + */ + public function __construct(ManagerRegistry $managerRegistry, $managerName, $lockTimeout) + { + $this->em = $managerRegistry->getManager($managerName); + $this->defaultLockTimeout = $lockTimeout; + + parent::__construct(); + } + /** * @inheritdoc */ @@ -69,16 +89,12 @@ protected function initialize(InputInterface $input, OutputInterface $output) $this->lockTimeout = $input->getOption('lock-timeout', null); if ($this->lockTimeout === null) { - $this->lockTimeout = $this->getContainer()->getParameter('jmose_command_scheduler.lock_timeout'); + $this->lockTimeout = $this->defaultLockTimeout; } else { if ($this->lockTimeout === 'false') { $this->lockTimeout = false; } } - - $this->em = $this->getContainer()->get('doctrine')->getManager( - $this->getContainer()->getParameter('jmose_command_scheduler.doctrine_manager') - ); } /** diff --git a/Controller/BaseController.php b/Controller/BaseController.php index a15a2436..331f6c59 100644 --- a/Controller/BaseController.php +++ b/Controller/BaseController.php @@ -2,7 +2,7 @@ namespace JMose\CommandSchedulerBundle\Controller; -use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; /** * Class BaseController @@ -10,7 +10,7 @@ * @author Julien Guyon * @package JMose\CommandSchedulerBundle\Controller */ -abstract class BaseController extends Controller +abstract class BaseController extends AbstractController { /** * @return \Doctrine\Common\Persistence\ObjectManager|object diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 05ff670f..bb1b3fad 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -10,10 +10,25 @@ services: - { name: form.type, alias: command_choice } JMose\CommandSchedulerBundle\Command\ExecuteCommand: + arguments: + - "@doctrine" + - "%jmose_command_scheduler.doctrine_manager%" + - "%jmose_command_scheduler.log_path%" tags: [console.command] JMose\CommandSchedulerBundle\Command\MonitorCommand: + arguments: + - "@doctrine" + - "%jmose_command_scheduler.doctrine_manager%" + - "%jmose_command_scheduler.lock_timeout%" + - "%jmose_command_scheduler.monitor_mail%" + - "%jmose_command_scheduler.monitor_mail_subject%" + - "%jmose_command_scheduler.send_ok%" tags: [console.command] JMose\CommandSchedulerBundle\Command\UnlockCommand: + arguments: + - "@doctrine" + - "%jmose_command_scheduler.doctrine_manager%" + - "%jmose_command_scheduler.lock_timeout%" tags: [console.command] From adf4b4d9f3b8bd781cc47e1d7c11296b41e8024c Mon Sep 17 00:00:00 2001 From: "j.guyon" Date: Thu, 6 Dec 2018 17:03:13 +0100 Subject: [PATCH 2/3] Add Symfony 4.2 in travis build --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 604579ed..80192b7f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,8 @@ matrix: env: SYMFONY_VERSION=3.4.* - php: 7.2 env: SYMFONY_VERSION=4.0.* + - php: 7.2 + env: SYMFONY_VERSION=4.2.* - php: nightly env: SYMFONY_VERSION=4.0.* From 87dab5f605c7ade0dc92a3dcbc082a5edd7dfd01 Mon Sep 17 00:00:00 2001 From: "j.guyon" Date: Thu, 6 Dec 2018 17:28:42 +0100 Subject: [PATCH 3/3] Fix Deprecated TreeBuilder constructor without root node information --- DependencyInjection/Configuration.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ed3d8626..7cf94ed1 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -17,8 +17,14 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('jmose_command_scheduler'); + $treeBuilder = new TreeBuilder('jmose_command_scheduler'); + if (method_exists($treeBuilder, 'getRootNode')) { + $rootNode = $treeBuilder->getRootNode(); + } else { + // BC layer for symfony/config 4.1 and older + $rootNode = $treeBuilder->root('jmose_command_scheduler'); + } + $rootNode ->children() ->scalarNode('doctrine_manager')->defaultValue('default')->end()