Skip to content

Commit 39b2912

Browse files
authored
add zdt feature toggle on/off (#4)
Allows a user to quickly enable/disable a feature from the ZDT
1 parent efe5e96 commit 39b2912

File tree

15 files changed

+363
-55
lines changed

15 files changed

+363
-55
lines changed

README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ return [
4747

4848
To retrieve the rollout service from a zend controller:
4949

50-
```
50+
```php
5151
<?php
5252

5353
$rollout = $this->getServiceLocator()->get('zf2_rollout');
@@ -57,6 +57,38 @@ Refer to the documentation of [opensoft/rollout](https://github.com/opensoft/rol
5757

5858
## Zend Developer Toolbar
5959

60-
The module comes with support for the zend developer toolbar. Currently the toolbar only shows the list of features and enable status for a given user.
60+
The module comes with support for the zend developer toolbar.
6161

6262
![zf2-adlogix-rollout zend developer tools](docs/rollout-zdt.png)
63+
64+
:warning: The ZDT rollout comes with a quick toggling action, allowing the user to quickly enable/disable a feature by clicking on one of the listed feature elements in the toolbar. Make sure to only authorise these actions in development mode. :warning:
65+
66+
An example of enabling the end points with BjyAuthorize:
67+
68+
```php
69+
<?php
70+
71+
// config/autoload/authorization.development.php
72+
73+
use Adlogix\Zf2Rollout\Service\Controller\RolloutController;
74+
75+
return [
76+
'bjyauthorize' => [
77+
78+
'guards' => [
79+
80+
// Add this if you are adding guards on controllers
81+
'BjyAuthorize\Guard\Controller' => [
82+
['controller' => RolloutController::class, 'roles' => ['guest','user']],
83+
],
84+
85+
// Add this if you are adding guards on routes
86+
'BjyAuthorize\Guard\Route' => [
87+
['route' => 'rollout_feature_toggle', 'roles' => ['guest','user']],
88+
],
89+
],
90+
91+
],
92+
];
93+
94+
```

composer.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,19 @@
2929
"zendframework/zend-mvc": "~2.7",
3030
"zendframework/zend-serializer": "~2.9",
3131
"zendframework/zend-servicemanager": "~2.7",
32+
"zendframework/zend-stdlib": "~2.7",
33+
"zendframework/zend-test": "~2.6",
3234
"doctrine/orm": "~2.5",
3335
"phpunit/phpunit": "~5.0"
3436
},
3537
"autoload": {
3638
"psr-4": {
3739
"Adlogix\\Zf2Rollout\\": "src/"
38-
},
39-
"classmap": [
40-
"./"
41-
]
40+
}
4241
},
4342
"autoload-dev": {
4443
"psr-4": {
4544
"Adlogix\\Zf2Rollout\\Test\\": "tests/"
46-
},
47-
"classmap": [
48-
"./"
49-
]
45+
}
5046
}
5147
}

config/module.config.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
12+
use Adlogix\Zf2Rollout\Service\Controller\RolloutController;
13+
1114
return [
1215

1316
'rollout' => [
@@ -25,6 +28,21 @@
2528
]
2629
],
2730

31+
'router' => array(
32+
'routes' => array(
33+
'rollout_feature_toggle' => array(
34+
'type' => \Zend\Mvc\Router\Http\Segment::class,
35+
'options' => array(
36+
'route' => '/_rollout_toggle/:feature[/]',
37+
'defaults' => array(
38+
'controller' => RolloutController::class,
39+
'action' => 'toggleFeature',
40+
),
41+
),
42+
),
43+
),
44+
),
45+
2846
'view_manager' => [
2947
'template_path_stack' => [
3048
__DIR__ . '/../view',
@@ -34,6 +52,15 @@
3452
],
3553
],
3654

55+
'controllers' => [
56+
57+
'factories' => [
58+
59+
RolloutController::class => \Adlogix\Zf2Rollout\Service\Factory\RolloutControllerFactory::class
60+
]
61+
62+
],
63+
3764
'service_manager' => [
3865

3966
'invokables' => [
@@ -50,6 +77,7 @@
5077
'zf2_rollout_storage_doctrine' => Adlogix\Zf2Rollout\Service\Factory\DoctrineORMStorageFactory::class,
5178

5279
'zf2_rollout' => Adlogix\Zf2Rollout\Service\Factory\RolloutFactory::class,
80+
'zf2_rollout_user' => \Adlogix\Zf2Rollout\Service\Factory\RolloutUserFactory::class,
5381

5482
'zf2_rollout.toolbar.collector' => \Adlogix\Zf2Rollout\Service\Factory\RolloutCollectorFactory::class
5583

docs/rollout-zdt.png

2.77 KB
Loading

src/Collector/RolloutCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Collector;
12+
namespace Adlogix\Zf2Rollout\Collector;
1313

1414

1515
use Opensoft\Rollout\Rollout;

Module.php renamed to src/Module.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
use Zend\ModuleManager\Feature\ViewHelperProviderInterface;
1919
use Zend\ServiceManager\AbstractPluginManager;
2020

21-
class Module implements ConfigProviderInterface, ViewHelperProviderInterface
21+
final class Module implements ConfigProviderInterface, ViewHelperProviderInterface
2222
{
2323
/**
2424
* {@inheritdoc}
2525
*/
2626
public function getConfig()
2727
{
28-
return include __DIR__ . '/config/module.config.php';
28+
return include __DIR__ . '/../config/module.config.php';
2929
}
3030

3131
/**
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/*
3+
* This file is part of the Adlogix package.
4+
*
5+
* (c) Allan Segebarth <[email protected]>
6+
* (c) Jean-Jacques Courtens <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Adlogix\Zf2Rollout\Service\Controller;
13+
14+
use Opensoft\Rollout\Rollout;
15+
use Opensoft\Rollout\RolloutUserInterface;
16+
use Zend\Http\Header\Referer;
17+
use Zend\Http\Request;
18+
use Zend\Mvc\Controller\AbstractActionController;
19+
20+
/**
21+
* @author Toni Van de Voorde <[email protected]>
22+
*/
23+
final class RolloutController extends AbstractActionController
24+
{
25+
/**
26+
* @var Rollout
27+
*/
28+
private $rollout;
29+
30+
/**
31+
* @var RolloutUserInterface
32+
*/
33+
private $rolloutUser;
34+
35+
/**
36+
* @param Rollout $rollout
37+
* @param RolloutUserInterface $rolloutUser
38+
*/
39+
public function __construct(
40+
Rollout $rollout,
41+
RolloutUserInterface $rolloutUser
42+
) {
43+
$this->rollout = $rollout;
44+
$this->rolloutUser = $rolloutUser;
45+
}
46+
47+
/**
48+
* @return \Zend\Http\Response
49+
*/
50+
public function toggleFeatureAction()
51+
{
52+
$feature = $this->params()->fromRoute('feature');
53+
54+
if ($this->rollout->isActive($feature, $this->rolloutUser)) {
55+
$this->rollout->deactivateUser($feature, $this->rolloutUser);
56+
} else {
57+
$this->rollout->activateUser($feature, $this->rolloutUser);
58+
}
59+
60+
return $this->redirect()->toUrl($this->getRefererUri());
61+
}
62+
63+
/**
64+
* @return string The referer URI (if not available returns '/' )
65+
*/
66+
protected function getRefererUri()
67+
{
68+
/** @var Request $request */
69+
$request = $this->getRequest();
70+
71+
$headers = $request->getHeader('Referer', '/');
72+
73+
return $headers instanceof Referer ? $headers->getUri() : (string)$headers;
74+
}
75+
}

src/Service/Factory/RolloutCollectorFactory.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111

1212
namespace Adlogix\Zf2Rollout\Service\Factory;
1313

14-
use Collector\RolloutCollector;
14+
use Adlogix\Zf2Rollout\Collector\RolloutCollector;
1515
use Interop\Container\ContainerInterface;
1616
use Opensoft\Rollout\Rollout;
1717
use Opensoft\Rollout\RolloutUserInterface;
18-
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
1918
use Zend\ServiceManager\FactoryInterface;
2019
use Zend\ServiceManager\ServiceLocatorInterface;
2120

@@ -38,26 +37,12 @@ public function createService(ServiceLocatorInterface $serviceLocator)
3837
*/
3938
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
4039
{
41-
/** @var array $config */
42-
$config = $container->get('zf2_rollout_config');
43-
4440

4541
/** @var Rollout $rollout */
4642
$rollout = $container->get('zf2_rollout');
4743

48-
$rolloutUserServiceId = $config['user_service'];
49-
50-
if (!isset($rolloutUserServiceId)) {
51-
throw new ServiceNotCreatedException(sprintf('You must define a service for rollout user_service.'));
52-
}
53-
54-
if (!$container->has($rolloutUserServiceId)) {
55-
throw new ServiceNotCreatedException(sprintf('Defined rollout user_service \'%s\' is not configured!',
56-
$rolloutUserServiceId));
57-
}
58-
5944
/** @var RolloutUserInterface $rolloutUser */
60-
$rolloutUser = $container->get($rolloutUserServiceId);
45+
$rolloutUser = $container->get('zf2_rollout_user');
6146

6247
return new RolloutCollector($rollout, $rolloutUser);
6348
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/*
3+
* This file is part of the Adlogix package.
4+
*
5+
* (c) Allan Segebarth <[email protected]>
6+
* (c) Jean-Jacques Courtens <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Adlogix\Zf2Rollout\Service\Factory;
13+
14+
15+
use Adlogix\Zf2Rollout\Service\Controller\RolloutController;
16+
use Interop\Container\ContainerInterface;
17+
use Opensoft\Rollout\Rollout;
18+
use Opensoft\Rollout\RolloutUserInterface;
19+
use Zend\Mvc\Controller\ControllerManager;
20+
use Zend\ServiceManager\FactoryInterface;
21+
use Zend\ServiceManager\ServiceLocatorInterface;
22+
23+
/**
24+
* @author Toni Van de Voorde <[email protected]>
25+
*/
26+
final class RolloutControllerFactory implements FactoryInterface
27+
{
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function createService(ServiceLocatorInterface $serviceLocator)
33+
{
34+
return $this($serviceLocator, null);
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
41+
{
42+
if ($container instanceof ControllerManager) {
43+
$container = $container->getServiceLocator();
44+
}
45+
46+
/** @var Rollout $rollout */
47+
$rollout = $container->get('zf2_rollout');
48+
49+
/** @var RolloutUserInterface $rolloutUser */
50+
$rolloutUser = $container->get('zf2_rollout_user');
51+
52+
return new RolloutController($rollout, $rolloutUser);
53+
}
54+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/*
3+
* This file is part of the Adlogix package.
4+
*
5+
* (c) Allan Segebarth <[email protected]>
6+
* (c) Jean-Jacques Courtens <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Adlogix\Zf2Rollout\Service\Factory;
13+
14+
15+
use Interop\Container\ContainerInterface;
16+
use Opensoft\Rollout\RolloutUserInterface;
17+
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
18+
use Zend\ServiceManager\FactoryInterface;
19+
use Zend\ServiceManager\ServiceLocatorInterface;
20+
21+
/**
22+
* @author Toni Van de Voorde <[email protected]>
23+
*/
24+
final class RolloutUserFactory implements FactoryInterface
25+
{
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function createService(ServiceLocatorInterface $serviceLocator)
31+
{
32+
return $this($serviceLocator, null);
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
39+
{
40+
/** @var array $config */
41+
$config = $container->get('zf2_rollout_config');
42+
43+
$rolloutUserServiceId = $config['user_service'];
44+
45+
if (!isset($rolloutUserServiceId)) {
46+
throw new ServiceNotCreatedException(sprintf('You must define a service for rollout user_service.'));
47+
}
48+
49+
if (!$container->has($rolloutUserServiceId)) {
50+
throw new ServiceNotCreatedException(sprintf('Defined rollout user_service \'%s\' is not configured!',
51+
$rolloutUserServiceId));
52+
}
53+
54+
/** @var RolloutUserInterface $rolloutUser */
55+
return $container->get($rolloutUserServiceId);
56+
}
57+
}

0 commit comments

Comments
 (0)