|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright 2018 Vladimir Jimenez |
|
5
|
|
|
* @license https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace allejo\stakx\EventSubscriber; |
|
9
|
|
|
|
|
10
|
|
|
use __\__; |
|
11
|
|
|
use allejo\stakx\AssetEngine\AssetEngineInterface; |
|
12
|
|
|
use allejo\stakx\AssetEngine\AssetEngineManager; |
|
13
|
|
|
use allejo\stakx\Document\StaticPageView; |
|
14
|
|
|
use allejo\stakx\Event\CompilerPostRenderStaticPageView; |
|
15
|
|
|
use allejo\stakx\Event\ConfigurationParseComplete; |
|
16
|
|
|
use allejo\stakx\Event\PageManagerPostProcess; |
|
17
|
|
|
use allejo\stakx\Filesystem\FileExplorer; |
|
18
|
|
|
use allejo\stakx\Filesystem\FilesystemLoader as fs; |
|
19
|
|
|
use Psr\Log\LoggerInterface; |
|
20
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
21
|
|
|
|
|
22
|
|
|
class AssetEngineSubscriber implements EventSubscriberInterface |
|
23
|
|
|
{ |
|
24
|
|
|
private $assetEngineManager; |
|
25
|
|
|
private $assetPageViews; |
|
26
|
7 |
|
private $logger; |
|
27
|
|
|
|
|
28
|
7 |
|
public function __construct(AssetEngineManager $assetEngineManager, LoggerInterface $logger) |
|
29
|
7 |
|
{ |
|
30
|
7 |
|
$this->assetEngineManager = $assetEngineManager; |
|
31
|
|
|
$this->assetPageViews = []; |
|
32
|
7 |
|
$this->logger = $logger; |
|
33
|
|
|
} |
|
34
|
7 |
|
|
|
35
|
|
|
public function processConfigurationSettings(ConfigurationParseComplete $event) |
|
36
|
|
|
{ |
|
37
|
7 |
|
$configuration = $event->getConfiguration()->getConfiguration(); |
|
38
|
|
|
|
|
39
|
7 |
|
/** @var AssetEngineInterface $engine */ |
|
40
|
7 |
|
foreach ($this->assetEngineManager->getEngines() as $engine) |
|
41
|
|
|
{ |
|
42
|
7 |
|
$defaults = __::get($configuration, $engine->getConfigurationNamespace(), []); |
|
43
|
|
|
$options = array_merge($engine->getDefaultConfiguration(), $defaults); |
|
44
|
7 |
|
|
|
45
|
|
|
$engine->setOptions($options); |
|
46
|
1 |
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function processAssetEnginePageView(PageManagerPostProcess $event) |
|
50
|
|
|
{ |
|
51
|
|
|
/** |
|
52
|
1 |
|
* @var string $folder |
|
53
|
|
|
* @var AssetEngineInterface $engine |
|
54
|
1 |
|
*/ |
|
55
|
|
|
foreach ($this->assetEngineManager->getFoldersToWatch() as $folder => $engine) |
|
56
|
1 |
|
{ |
|
57
|
|
|
$assetFolder = fs::absolutePath($folder); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
if (!fs::exists($assetFolder)) |
|
60
|
|
|
{ |
|
61
|
1 |
|
continue; |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
$engine->setPageManager($event->getPageManager()); |
|
65
|
|
|
$extensions = []; |
|
66
|
1 |
|
|
|
67
|
|
|
foreach ($engine->getExtensions() as $extension) |
|
68
|
|
|
{ |
|
69
|
1 |
|
$extensions[] = "/.{$extension}.twig$/"; |
|
70
|
|
|
} |
|
71
|
1 |
|
|
|
72
|
|
|
$explorer = FileExplorer::create($assetFolder, [], $extensions, FileExplorer::INCLUDE_ONLY_FILES | FileExplorer::IGNORE_DIRECTORIES); |
|
73
|
1 |
|
|
|
74
|
|
|
foreach ($explorer as $file) |
|
75
|
|
|
{ |
|
76
|
|
|
$assetPageView = new StaticPageView($file); |
|
77
|
1 |
|
|
|
78
|
|
|
try |
|
79
|
1 |
|
{ |
|
80
|
1 |
|
$event->getPageManager()->trackNewPageView($assetPageView); |
|
81
|
|
|
$this->assetPageViews[$assetPageView->getRelativeFilePath()] = [ |
|
82
|
1 |
|
'engine' => $engine, |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
catch (\Exception $e) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->logger->error('An exception occurred while creating a Static PageView for an AssetEngine'); |
|
88
|
1 |
|
$this->logger->error(' {message}', [ |
|
89
|
|
|
'message' => $e->getMessage(), |
|
90
|
|
|
]); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
1 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function compileAssetEnginePageViews(CompilerPostRenderStaticPageView $event) |
|
97
|
|
|
{ |
|
98
|
|
|
$pageView = $event->getPageView(); |
|
99
|
|
|
$filePath = $pageView->getRelativeFilePath(); |
|
100
|
|
|
|
|
101
|
|
|
if (isset($this->assetPageViews[$filePath])) |
|
102
|
|
|
{ |
|
103
|
|
|
/** @var AssetEngineInterface $engine */ |
|
104
|
|
|
$engine = $this->assetPageViews[$filePath]['engine']; |
|
105
|
|
|
|
|
106
|
|
|
$output = $engine->parse($event->getCompiledOutput(), [ |
|
107
|
|
|
'pageview' => $pageView, |
|
108
|
|
|
]); |
|
109
|
|
|
|
|
110
|
|
|
$event->setCompiledOutput($output); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public static function getSubscribedEvents() |
|
115
|
|
|
{ |
|
116
|
|
|
return [ |
|
117
|
|
|
ConfigurationParseComplete::NAME => 'processConfigurationSettings', |
|
118
|
|
|
PageManagerPostProcess::NAME => 'processAssetEnginePageView', |
|
119
|
|
|
CompilerPostRenderStaticPageView::NAME => 'compileAssetEnginePageViews', |
|
120
|
|
|
]; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: