|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Majora\Bundle\FrameworkExtraBundle\Event; |
|
4
|
|
|
|
|
5
|
|
|
use Majora\Framework\Date\Clock; |
|
6
|
|
|
use Symfony\Component\Console\ConsoleEvents; |
|
7
|
|
|
use Symfony\Component\Console\Event\ConsoleCommandEvent; |
|
8
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
10
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Framework event subscriber, looking for a mocked date |
|
14
|
|
|
* If guessed, define it as current one for Clock service |
|
15
|
|
|
* |
|
16
|
|
|
* @example |
|
17
|
|
|
* /app_dev.php/article/1?_date_mock=2015-01-01 |
|
18
|
|
|
*/ |
|
19
|
|
|
class ClockSubscriber extends Clock implements EventSubscriberInterface |
|
20
|
|
|
{ |
|
21
|
|
|
protected $mockParamName; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* construct |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $mockParamName |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($mockParamName) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->mockParamName = $mockParamName; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @see EventSubscriberInterface::getSubscribedEvents() |
|
35
|
|
|
*/ |
|
36
|
|
|
static public function getSubscribedEvents() |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
return array( |
|
39
|
|
|
KernelEvents::REQUEST => array('onKernelRequest', 100), |
|
40
|
|
|
ConsoleEvents::COMMAND => array('onConsoleCommand', 100), |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* kernel request event handler |
|
46
|
|
|
*/ |
|
47
|
|
|
public function onKernelRequest(GetResponseEvent $event) |
|
48
|
|
|
{ |
|
49
|
|
|
$request = $event->getRequest(); |
|
50
|
|
|
if (!$strMockedDate = $request->query->get($this->mockParamName)) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->mock($strMockedDate); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* console command event handler |
|
59
|
|
|
*/ |
|
60
|
|
|
public function onConsoleCommand(ConsoleCommandEvent $event) |
|
61
|
|
|
{ |
|
62
|
|
|
$input = $event->getInput(); |
|
63
|
|
|
if (!$input->hasOption($this->mockParamName)) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->mock($input->getOption($this->mockParamName)); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|