Skip to content

Commit

Permalink
Symfony 4/5 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
artgris committed Jul 20, 2021
1 parent a0a3612 commit 6538f7f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 41 deletions.
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class Configuration implements ConfigurationInterface
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('artgris_maintenance');
$treeBuilder = new TreeBuilder("artgris_maintenance");
$rootNode = $treeBuilder->getRootNode();

$rootNode
->children()
Expand Down
17 changes: 8 additions & 9 deletions EventListener/MaintenanceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@


use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelInterface;
use Twig_Environment;
use Twig\Environment;

/**
* Maintenance Listener
Expand All @@ -29,7 +29,7 @@ class MaintenanceListener
*/
private $kernel;
/**
* @var Twig_Environment
* @var Environment
*/
private $twig_Environment;
/**
Expand All @@ -42,26 +42,25 @@ class MaintenanceListener
*
* @param array $maintenance
* @param KernelInterface $kernel
* @param Twig_Environment $twig_Environment
* @param Environment $twig_Environment
*/
public function __construct(array $maintenance, KernelInterface $kernel, Twig_Environment $twig_Environment)
public function __construct(array $maintenance, KernelInterface $kernel, Environment $twig_Environment)
{
$this->enable = $maintenance['enable'] ? $maintenance['enable'] : false;
$this->ips = $maintenance['ips'] ? $maintenance['ips'] : [];
$this->enable = $maintenance['enable'] ?: false;
$this->ips = $maintenance['ips'] ?: [];
$this->response = $maintenance['response'];
$this->kernel = $kernel;
$this->twig_Environment = $twig_Environment;
}

public function onKernelRequest(GetResponseEvent $event)
public function onKernelRequest(RequestEvent $event)
{
/**
* Conditions : Maintenance enable, not in dev/test mode, not in enable Ips
*/
if ($this->enable && !in_array($this->kernel->getEnvironment(), ['dev']) && !in_array(@$_SERVER['REMOTE_ADDR'], $this->ips)) {
$content = $this->twig_Environment->render('@ArtgrisMaintenance/maintenance.html.twig');
$event->setResponse(new Response($content, $this->response));
$event->stopPropagation();
}
}
}
34 changes: 7 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,22 @@ Installation

`composer require artgris/maintenance-bundle`

### 2) Enable Bundle

// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
// ...
public function registerBundles()
{
$bundles = array(
// ...
new Artgris\MaintenanceBundle\ArtgrisMaintenanceBundle(),
);
// ...
}
}
### 3) Configure the Bundle
### 2) Configure the Bundle

Adds following configurations

to ` app/config/config.yml` :
to ` config/packages/artgris_maintenance.yaml` :

```yml
artgris_maintenance:
enable: true # Enable|Disable maintenance
ips: [127.0.0.1, ...] # IPs allow (prod)
ips: ["127.0.0.1","::1",...] # IPs allow (prod)
response: 503 # Maintenance Page HTTP Status Code
```
### 4) Override maintenance.html.twig (optional)
Create your own twig in `app/Resources/ArtgrisMaintenanceBundle/views/maintenance.html.twig`.
or in (S4 project) `template/bundles/ArtgrisMaintenanceBundle/maintenance.html.twig`
in `template/bundles/ArtgrisMaintenanceBundle/maintenance.html.twig`

ex:
```twig
Expand All @@ -61,11 +41,11 @@ Usage
The `dev` environment was not affected by maintenance.

- Enable|Disable maintenance : `enable: true|false`
- Add authorized IPs to prod : `ips: [127.0.0.1, ...]`
- Add authorized IPs to prod : `ips: ["127.0.0.1","::1",...]`
- Maintenance Page HTTP Status Code : `response: 503`


Don't forget to clear and warm the `prod` cache :
Don't forget to clear the `prod` cache :

php bin/console cache:clear --env=prod
php bin/console cache:clear

7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
}
],
"require": {
"php": ">=5.3.3",
"twig/twig": "~1.11|~2.0|~3.0"
"php": ">=7.1.3",
"twig/twig": "^2.12|^3.0",
"symfony/config": "^4.4|^5.0"
},
"require-dev": {
"phpunit/phpunit": "~3.7"
},
"autoload": {
"psr-0": {
"Artgris\\MaintenanceBundle": ""
"Artgris\\MaintenanceBundle": "",
}
},
"target-dir": "Artgris/MaintenanceBundle"
Expand Down

0 comments on commit 6538f7f

Please sign in to comment.