Skip to content

Commit 7e792eb

Browse files
authored
Develop (#5)
* controller events * refactoring
1 parent bfe7326 commit 7e792eb

21 files changed

Lines changed: 435 additions & 43 deletions

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 0.1.0 - 2017-03-07
2+
3+
Initial tagged release
4+
5+
### Added
6+
* Everything
7+
8+
### Deprecated
9+
* Nothing
10+
11+
### Removed
12+
* Nothing
13+
14+
### Fixed
15+
* Nothing

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^5.6 || ^7.0",
13+
"php": "^7.1",
1414
"psr/http-message": "^1.0",
1515
"container-interop/container-interop": "^1.1",
16-
"zendframework/zend-servicemanager": "^3.0"
16+
"zendframework/zend-servicemanager": "^3.0",
17+
18+
"dotkernel/dot-event": "~0.1"
1719
},
1820
"require-dev": {
1921
"zendframework/zend-expressive-template": "^1.0",
@@ -34,7 +36,7 @@
3436
},
3537
"extra": {
3638
"branch-alias": {
37-
"dev-master": "0.6-dev"
39+
"dev-master": "0.2-dev"
3840
}
3941
}
4042
}

src/AbstractActionController.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,43 @@
77
* Time: 8:24 PM
88
*/
99

10+
declare(strict_types = 1);
11+
1012
namespace Dot\Controller;
1113

14+
use Dot\Controller\Event\ControllerEvent;
1215
use Psr\Http\Message\ResponseInterface;
1316

1417
/**
1518
* Class AbstractActionController
1619
* @package Dot\Controller
1720
*/
18-
class AbstractActionController extends AbstractController
21+
abstract class AbstractActionController extends AbstractController
1922
{
2023
/**
2124
* @return ResponseInterface
2225
*/
23-
public function dispatch()
26+
public function dispatch(): ResponseInterface
2427
{
2528
$request = $this->request;
26-
$action = AbstractController::getMethodFromAction(
27-
strtolower($request->getAttribute('action', 'index'))
28-
);
29+
30+
$action = strtolower(trim($request->getAttribute('action', 'index')));
31+
if (empty($action)) {
32+
$action = 'index';
33+
}
34+
35+
$action = AbstractController::getMethodFromAction($action);
2936

3037
if (method_exists($this, $action)) {
38+
$r = $this->dispatchEvent(ControllerEvent::EVENT_CONTROLLER_DISPATCH, [
39+
'request' => $request,
40+
'next' => $this->getNext(),
41+
'method' => $action
42+
]);
43+
if ($r instanceof ResponseInterface) {
44+
return $r;
45+
}
46+
3147
return $this->$action();
3248
}
3349

src/AbstractController.php

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,28 @@
77
* Time: 8:24 PM
88
*/
99

10+
declare(strict_types = 1);
11+
1012
namespace Dot\Controller;
1113

14+
use Dot\Controller\Event\DispatchControllerEventsTrait;
15+
use Dot\Controller\Plugin\PluginInterface;
1216
use Dot\Controller\Plugin\PluginManager;
1317
use Dot\Controller\Plugin\PluginManagerAwareInterface;
1418
use Psr\Http\Message\ResponseInterface;
1519
use Psr\Http\Message\ServerRequestInterface;
20+
use Zend\EventManager\EventManagerAwareInterface;
1621

1722
/**
1823
* Class AbstractController
1924
* @package Dot\Controller
2025
*/
21-
abstract class AbstractController implements PluginManagerAwareInterface
26+
abstract class AbstractController implements
27+
PluginManagerAwareInterface,
28+
EventManagerAwareInterface
2229
{
30+
use DispatchControllerEventsTrait;
31+
2332
/** @var PluginManager */
2433
protected $pluginManager;
2534

@@ -32,13 +41,16 @@ abstract class AbstractController implements PluginManagerAwareInterface
3241
/** @var callable */
3342
protected $next;
3443

44+
/** @var bool */
45+
protected $debug = false;
46+
3547
/**
3648
* Transform an "action" token into a method name
3749
*
3850
* @param string $action
3951
* @return string
4052
*/
41-
public static function getMethodFromAction($action)
53+
public static function getMethodFromAction(string $action): string
4254
{
4355
$method = str_replace(['.', '-', '_'], ' ', $action);
4456
$method = ucwords($method);
@@ -58,36 +70,36 @@ public function __invoke(
5870
ServerRequestInterface $request,
5971
ResponseInterface $response,
6072
callable $next = null
61-
) {
73+
): ResponseInterface {
6274
$this->request = $request;
6375
$this->response = $response;
6476
$this->next = $next;
6577

6678
return $this->dispatch();
6779
}
6880

69-
abstract public function dispatch();
81+
abstract public function dispatch(): ResponseInterface;
7082

7183
/**
7284
* @return ServerRequestInterface
7385
*/
74-
public function getRequest()
86+
public function getRequest(): ServerRequestInterface
7587
{
7688
return $this->request;
7789
}
7890

7991
/**
8092
* @return ResponseInterface
8193
*/
82-
public function getResponse()
94+
public function getResponse(): ResponseInterface
8395
{
8496
return $this->response;
8597
}
8698

8799
/**
88100
* @return callable
89101
*/
90-
public function getNext()
102+
public function getNext(): callable
91103
{
92104
return $this->next;
93105
}
@@ -102,7 +114,7 @@ public function getNext()
102114
* @param array $params
103115
* @return mixed
104116
*/
105-
public function __call($method, $params)
117+
public function __call(string $method, array $params)
106118
{
107119
$plugin = $this->plugin($method);
108120
if (is_callable($plugin)) {
@@ -115,29 +127,43 @@ public function __call($method, $params)
115127
* Get plugin instance
116128
*
117129
* @param string $name Name of plugin to return
118-
* @param null|array $options Options to pass to plugin constructor (if not already instantiated)
119-
* @return mixed
130+
* @param array $options Options to pass to plugin constructor (if not already instantiated)
131+
* @return PluginInterface|callable
120132
*/
121-
public function plugin($name, array $options = null)
133+
public function plugin(string $name, array $options = []): PluginInterface
122134
{
123135
return $this->getPluginManager()->get($name, $options);
124136
}
125137

126138
/**
127139
* @return PluginManager
128140
*/
129-
public function getPluginManager()
141+
public function getPluginManager(): PluginManager
130142
{
131143
return $this->pluginManager;
132144
}
133145

134146
/**
135147
* @param PluginManager $pluginManager
136-
* @return $this
137148
*/
138149
public function setPluginManager(PluginManager $pluginManager)
139150
{
140151
$this->pluginManager = $pluginManager;
141-
return $this;
152+
}
153+
154+
/**
155+
* @return bool
156+
*/
157+
public function isDebug(): bool
158+
{
159+
return $this->debug;
160+
}
161+
162+
/**
163+
* @param bool $debug
164+
*/
165+
public function setDebug(bool $debug)
166+
{
167+
$this->debug = $debug;
142168
}
143169
}

src/ConfigProvider.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
* Time: 8:24 PM
88
*/
99

10+
declare(strict_types = 1);
11+
1012
namespace Dot\Controller;
1113

14+
use Dot\Controller\Factory\ControllerEventListenersInitializer;
1215
use Dot\Controller\Factory\PluginManagerAwareInitializer;
1316
use Dot\Controller\Factory\PluginManagerFactory;
1417
use Dot\Controller\Plugin\PluginManager;
@@ -19,22 +22,30 @@
1922
*/
2023
class ConfigProvider
2124
{
22-
public function __invoke()
25+
public function __invoke(): array
2326
{
2427
return [
28+
'dependencies' => $this->getDependenciesConfig(),
29+
2530
'dot_controller' => [
26-
'plugin_manager' => []
27-
],
2831

29-
'dependencies' => [
30-
'factories' => [
31-
PluginManager::class => PluginManagerFactory::class,
32-
],
32+
'plugin_manager' => [],
3333

34-
'initializers' => [
35-
PluginManagerAwareInitializer::class,
36-
],
34+
'event_listeners' => [],
35+
],
36+
];
37+
}
38+
39+
public function getDependenciesConfig(): array
40+
{
41+
return [
42+
'factories' => [
43+
PluginManager::class => PluginManagerFactory::class,
3744
],
45+
'initializers' => [
46+
PluginManagerAwareInitializer::class,
47+
ControllerEventListenersInitializer::class,
48+
]
3849
];
3950
}
4051
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* @copyright: DotKernel
4+
* @library: dot-controller
5+
* @author: n3vrax
6+
* Date: 1/27/2017
7+
* Time: 7:12 PM
8+
*/
9+
10+
declare(strict_types = 1);
11+
12+
namespace Dot\Controller\Event;
13+
14+
use Zend\EventManager\AbstractListenerAggregate;
15+
16+
/**
17+
* Class AbstractControllerEventListener
18+
* @package Dot\Controller\Event
19+
*/
20+
abstract class AbstractControllerEventListener extends AbstractListenerAggregate implements
21+
ControllerEventListenerInterface
22+
{
23+
use ControllerEventListenerTrait;
24+
}

src/Event/ControllerEvent.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* @copyright: DotKernel
4+
* @library: dot-controller
5+
* @author: n3vra
6+
* Date: 1/27/2017
7+
* Time: 4:20 PM
8+
*/
9+
10+
declare(strict_types = 1);
11+
12+
namespace Dot\Controller\Event;
13+
14+
use Dot\Event\Event;
15+
16+
/**
17+
* Class ActionControllerEvent
18+
* @package Dot\Controller\Event
19+
*/
20+
class ControllerEvent extends Event
21+
{
22+
const EVENT_CONTROLLER_DISPATCH = 'event.controller.dispatch';
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* @copyright: DotKernel
4+
* @library: dot-controller
5+
* @author: n3vra
6+
* Date: 1/27/2017
7+
* Time: 5:21 PM
8+
*/
9+
10+
declare(strict_types = 1);
11+
12+
namespace Dot\Controller\Event;
13+
14+
use Zend\EventManager\ListenerAggregateInterface;
15+
16+
/**
17+
* Interface ActionControllerEventListenerInterface
18+
* @package Dot\Controller\Event
19+
*/
20+
interface ControllerEventListenerInterface extends ListenerAggregateInterface
21+
{
22+
const LISTEN_ALL = 1;
23+
24+
/**
25+
* @param ControllerEvent $e
26+
*/
27+
public function onDispatch(ControllerEvent $e);
28+
}

0 commit comments

Comments
 (0)