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\Configuration; |
14
|
|
|
use allejo\stakx\Document\StaticPageView; |
15
|
|
|
use allejo\stakx\Event\CompilerPostRenderStaticPageView; |
16
|
|
|
use allejo\stakx\Event\ConfigurationParseComplete; |
17
|
|
|
use allejo\stakx\Event\PageManagerPostProcess; |
18
|
|
|
use allejo\stakx\Filesystem\FileExplorer; |
19
|
|
|
use allejo\stakx\Filesystem\FilesystemLoader as fs; |
20
|
|
|
use allejo\stakx\Filesystem\FilesystemPath; |
21
|
|
|
use allejo\stakx\Filesystem\Folder; |
22
|
|
|
use allejo\stakx\RuntimeStatus; |
23
|
|
|
use allejo\stakx\Service; |
24
|
|
|
use Psr\Log\LoggerInterface; |
25
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
26
|
7 |
|
|
27
|
|
|
class AssetEngineSubscriber implements EventSubscriberInterface |
28
|
7 |
|
{ |
29
|
7 |
|
private $assetEngineManager; |
30
|
7 |
|
private $assetPageViews; |
31
|
|
|
private $logger; |
32
|
7 |
|
|
33
|
|
|
public function __construct(AssetEngineManager $assetEngineManager, LoggerInterface $logger) |
34
|
7 |
|
{ |
35
|
|
|
$this->assetEngineManager = $assetEngineManager; |
36
|
|
|
$this->assetPageViews = []; |
37
|
7 |
|
$this->logger = $logger; |
38
|
|
|
} |
39
|
7 |
|
|
40
|
7 |
|
public function processConfigurationSettings(ConfigurationParseComplete $event) |
41
|
|
|
{ |
42
|
7 |
|
$configuration = $event->getConfiguration()->getConfiguration(); |
43
|
|
|
|
44
|
7 |
|
/** @var AssetEngineInterface $engine */ |
45
|
|
|
foreach ($this->assetEngineManager->getEngines() as $engine) |
46
|
1 |
|
{ |
47
|
|
|
$defaults = __::get($configuration, $engine->getConfigurationNamespace(), []); |
48
|
|
|
$options = array_merge($engine->getDefaultConfiguration(), $defaults); |
49
|
|
|
|
50
|
|
|
$engine->setOptions($options); |
51
|
|
|
} |
52
|
1 |
|
} |
53
|
|
|
|
54
|
1 |
|
public function processAssetEnginePageView(PageManagerPostProcess $event) |
55
|
|
|
{ |
56
|
1 |
|
/** |
57
|
|
|
* @var string $folder |
58
|
|
|
* @var AssetEngineInterface $engine |
59
|
|
|
*/ |
60
|
|
|
foreach ($this->assetEngineManager->getFoldersToWatch() as $folder => $engine) |
61
|
1 |
|
{ |
62
|
1 |
|
$assetFolder = fs::absolutePath($folder); |
|
|
|
|
63
|
|
|
|
64
|
1 |
|
if (!fs::exists($assetFolder)) |
65
|
|
|
{ |
66
|
1 |
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$engine->setPageManager($event->getPageManager()); |
70
|
|
|
$extensions = []; |
71
|
1 |
|
|
72
|
|
|
foreach ($engine->getExtensions() as $extension) |
73
|
1 |
|
{ |
74
|
|
|
$extensions[] = "/.{$extension}.twig$/"; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
$explorer = FileExplorer::create($assetFolder, [], $extensions, FileExplorer::INCLUDE_ONLY_FILES | FileExplorer::IGNORE_DIRECTORIES); |
78
|
|
|
|
79
|
1 |
|
foreach ($explorer as $file) |
80
|
1 |
|
{ |
81
|
|
|
$assetPageView = new StaticPageView($file); |
82
|
1 |
|
|
83
|
|
|
try |
84
|
|
|
{ |
85
|
|
|
$event->getPageManager()->trackNewPageView($assetPageView); |
86
|
|
|
$this->assetPageViews[$assetPageView->getRelativeFilePath()] = [ |
87
|
|
|
'engine' => $engine, |
88
|
1 |
|
]; |
89
|
|
|
} |
90
|
|
|
catch (\Exception $e) |
91
|
|
|
{ |
92
|
|
|
$this->logger->error('An exception occurred while creating a Static PageView for an AssetEngine'); |
93
|
1 |
|
$this->logger->error(' {message}', [ |
94
|
|
|
'message' => $e->getMessage(), |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function compileAssetEnginePageViews(CompilerPostRenderStaticPageView $event) |
102
|
|
|
{ |
103
|
|
|
$pageView = $event->getPageView(); |
104
|
|
|
$filePath = $pageView->getRelativeFilePath(); |
105
|
|
|
|
106
|
|
|
if (isset($this->assetPageViews[$filePath])) |
107
|
|
|
{ |
108
|
|
|
/** @var AssetEngineInterface $engine */ |
109
|
|
|
$engine = $this->assetPageViews[$filePath]['engine']; |
110
|
|
|
$cacheDir = $this->buildCacheFolder($engine); |
111
|
|
|
|
112
|
|
|
if (Service::hasRunTimeFlag(RuntimeStatus::USING_CACHE)) |
113
|
|
|
{ |
114
|
|
|
$engine->loadCache($cacheDir); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$output = $engine->parse($event->getCompiledOutput(), [ |
118
|
|
|
'pageview' => $pageView, |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
$engine->saveCache($cacheDir); |
122
|
|
|
$event->setCompiledOutput($output); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public static function getSubscribedEvents() |
127
|
|
|
{ |
128
|
|
|
return [ |
129
|
|
|
ConfigurationParseComplete::NAME => 'processConfigurationSettings', |
130
|
|
|
PageManagerPostProcess::NAME => 'processAssetEnginePageView', |
131
|
|
|
CompilerPostRenderStaticPageView::NAME => 'compileAssetEnginePageViews', |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function buildCacheFolder(AssetEngineInterface $engine) |
136
|
|
|
{ |
137
|
|
|
$cacheDirPath = new FilesystemPath(Service::getWorkingDirectory() . '/'); |
138
|
|
|
$cacheDirPath |
139
|
|
|
->appendToPath(Configuration::CACHE_FOLDER . '/') |
140
|
|
|
->appendToPath(__::slug(strtolower($engine->getName())) . '/') |
141
|
|
|
; |
142
|
|
|
|
143
|
|
|
fs::mkdir($cacheDirPath); |
144
|
|
|
|
145
|
|
|
$cacheDir = new Folder($cacheDirPath); |
146
|
|
|
$cacheDir->freeze(); |
147
|
|
|
|
148
|
|
|
return $cacheDir; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
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: