diff --git a/CHANGELOG-2.0.0.md b/CHANGELOG-2.0.0.md index c11ba2ef..187480ba 100644 --- a/CHANGELOG-2.0.0.md +++ b/CHANGELOG-2.0.0.md @@ -8,6 +8,7 @@ ## Features * Feature #262 : Added support to ping eXpansion Analytics. +* Feature #266 : Add warning that displayes every 5 minutes about dev mode. # 2.0.0.0-alpha2 (2018-02-03) diff --git a/src/eXpansion/Framework/Core/DependencyInjection/eXpansionCoreExtension.php b/src/eXpansion/Framework/Core/DependencyInjection/eXpansionCoreExtension.php index 15fb5590..e18808a9 100644 --- a/src/eXpansion/Framework/Core/DependencyInjection/eXpansionCoreExtension.php +++ b/src/eXpansion/Framework/Core/DependencyInjection/eXpansionCoreExtension.php @@ -48,5 +48,9 @@ public function load(array $configs, ContainerBuilder $container) // Temporary for the prototype. $loader->load('plugins.yml'); + + if ($container->getParameter('kernel.environment') == 'dev') { + $loader->load('plugins_dev.yml'); + } } } \ No newline at end of file diff --git a/src/eXpansion/Framework/Core/Plugins/DevModeNotifier.php b/src/eXpansion/Framework/Core/Plugins/DevModeNotifier.php new file mode 100644 index 00000000..55b7e916 --- /dev/null +++ b/src/eXpansion/Framework/Core/Plugins/DevModeNotifier.php @@ -0,0 +1,81 @@ + + */ +class DevModeNotifier implements ListenerInterfaceExpTimer +{ + /** @var ChatNotification */ + protected $chatNotification; + + /** @var Console */ + protected $console; + + /** @var int How often notificaiton needs to be displayed */ + protected $interval = 300; + + /** @var int last time it was displayed. */ + protected $lastDisplayTime; + + /** + * DevModeNotifier constructor. + * + * @param ChatNotification $chatNotification + * @param Console $console + */ + public function __construct(ChatNotification $chatNotification, Console $console) + { + $this->chatNotification = $chatNotification; + $this->console = $console; + + // Display once 30 seconds after start. + $this->lastDisplayTime = time() - $this->interval + 10; + } + + /** + * @inheritdoc + */ + public function onPreLoop() + { + // nothing + } + + /** + * @inheritdoc + */ + public function onPostLoop() + { + // nothing + } + + /** + * @inheritdoc + */ + public function onEverySecond() + { + if (time() > $this->lastDisplayTime + $this->interval) { + $this->lastDisplayTime = time(); + + $this->console->getSfStyleOutput()->warning( + [ + "!! eXpansion is running in dev mode !!", + "In dev mode eXpansion is not stable and will leak memory which will cause crash.", + "This is normal behaviour. Please use prod mode. ", + "", + "If you are currently developing please ignore this message.", + ] + ); + $this->chatNotification->sendMessage("{warning} eXpansion is in dev mode. Memory leaks are normal."); + } + } +} \ No newline at end of file diff --git a/src/eXpansion/Framework/Core/Resources/config/plugins_dev.yml b/src/eXpansion/Framework/Core/Resources/config/plugins_dev.yml new file mode 100644 index 00000000..ad01138d --- /dev/null +++ b/src/eXpansion/Framework/Core/Resources/config/plugins_dev.yml @@ -0,0 +1,14 @@ +services: + _defaults: + autowire: true + autoconfigure: true + public: true + + # + # Plugin to warn agains dev mode being enabled. + # + eXpansion\Framework\Core\Plugins\DevModeNotifier: + class: 'eXpansion\Framework\Core\Plugins\DevModeNotifier' + tags: + - {name: expansion.plugin, data_provider: exp.timer} + diff --git a/src/eXpansion/Framework/Core/Services/Console.php b/src/eXpansion/Framework/Core/Services/Console.php index 414b62a5..2cf957ea 100644 --- a/src/eXpansion/Framework/Core/Services/Console.php +++ b/src/eXpansion/Framework/Core/Services/Console.php @@ -4,8 +4,10 @@ use eXpansion\Framework\Core\Helpers\ColorConversion; use eXpansion\Framework\Core\Services\Application\Dispatcher; +use Symfony\Component\Console\Input\StringInput; use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Class Console to print in the console. @@ -50,6 +52,9 @@ class Console /** @var OutputInterface */ protected $consoleOutput; + /** @var SymfonyStyle */ + protected $sfStyleOutput; + /** @var boolean Color console enabled */ protected $colorEnabled; /** @@ -78,6 +83,8 @@ public function init(OutputInterface $consoleOutput, $dispatcher) { $this->consoleOutput = $consoleOutput; $this->dispatcher = $dispatcher; + + $this->sfStyleOutput = new SymfonyStyle(new StringInput(''), $consoleOutput); } /** @@ -225,4 +232,17 @@ public function getConsoleOutput() return $this->consoleOutput; } + + /** + * Get symfony style output. + * + * @return SymfonyStyle + */ + public function getSfStyleOutput(): SymfonyStyle + { + if (is_null($this->sfStyleOutput)) { + $this->sfStyleOutput = new SymfonyStyle(new StringInput(''), new NullOutput()); + } + return $this->sfStyleOutput; + } }