Skip to content

Commit

Permalink
#266 - Add warning that displayes every 5 minutes about dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverde8 committed Feb 18, 2018
1 parent 2001ccd commit 37d5450
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-2.0.0.md
Expand Up @@ -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)

Expand Down
Expand Up @@ -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');
}
}
}
81 changes: 81 additions & 0 deletions src/eXpansion/Framework/Core/Plugins/DevModeNotifier.php
@@ -0,0 +1,81 @@
<?php


namespace eXpansion\Framework\Core\Plugins;
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer;
use eXpansion\Framework\Core\Helpers\ChatNotification;
use eXpansion\Framework\Core\Services\Console;


/**
* Class DevModeNotifier
*
* @package eXpansion\Framework\Core\Plugins;
* @author oliver de Cramer <oliverde8@gmail.com>
*/
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.");
}
}
}
14 changes: 14 additions & 0 deletions 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}

20 changes: 20 additions & 0 deletions src/eXpansion/Framework/Core/Services/Console.php
Expand Up @@ -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.
Expand Down Expand Up @@ -50,6 +52,9 @@ class Console
/** @var OutputInterface */
protected $consoleOutput;

/** @var SymfonyStyle */
protected $sfStyleOutput;

/** @var boolean Color console enabled */
protected $colorEnabled;
/**
Expand Down Expand Up @@ -78,6 +83,8 @@ public function init(OutputInterface $consoleOutput, $dispatcher)
{
$this->consoleOutput = $consoleOutput;
$this->dispatcher = $dispatcher;

$this->sfStyleOutput = new SymfonyStyle(new StringInput(''), $consoleOutput);
}

/**
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 37d5450

Please sign in to comment.