-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
StyleAndJavascriptInclusionService.php
106 lines (90 loc) · 3.08 KB
/
StyleAndJavascriptInclusionService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace Neos\Neos\Ui\Domain\Service;
/*
* This file is part of the Neos.Neos.Ui package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Eel\CompilingEvaluator;
use Neos\Eel\Utility;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Utility\PositionalArraySorter;
/**
* @Flow\Scope("singleton")
*/
class StyleAndJavascriptInclusionService
{
/**
* @Flow\Inject
* @var ResourceManager
*/
protected $resourceManager;
/**
* @Flow\Inject(lazy=false)
* @var CompilingEvaluator
*/
protected $eelEvaluator;
/**
* @Flow\InjectConfiguration(package="Neos.Fusion", path="defaultContext")
* @var array
*/
protected $fusionDefaultEelContext;
/**
* @Flow\InjectConfiguration(path="configurationDefaultEelContext")
* @var array
*/
protected $additionalEelDefaultContext;
/**
* @Flow\InjectConfiguration(path="resources.javascript")
* @var array
*/
protected $javascriptResources;
/**
* @Flow\InjectConfiguration(path="resources.stylesheets")
* @var array
*/
protected $stylesheetResources;
public function getHeadScripts()
{
return $this->build($this->javascriptResources, function ($uri, $defer) {
return '<script src="' . $uri . '" ' . $defer . '></script>';
});
}
public function getHeadStylesheets()
{
return $this->build($this->stylesheetResources, function ($uri, $defer) {
return '<link rel="stylesheet" href="' . $uri . '" ' . $defer . '/>';
});
}
protected function build(array $resourceArrayToSort, \Closure $builderForLine)
{
$sortedResources = (new PositionalArraySorter($resourceArrayToSort))->toArray();
$result = '';
foreach ($sortedResources as $element) {
$resourceExpression = $element['resource'];
if (substr($resourceExpression, 0, 2) === '${' && substr($resourceExpression, -1) === '}') {
$resourceExpression = Utility::evaluateEelExpression(
$resourceExpression,
$this->eelEvaluator,
[],
array_merge($this->fusionDefaultEelContext, $this->additionalEelDefaultContext)
);
}
$hash = null;
if (strpos($resourceExpression, 'resource://') === 0) {
// Cache breaker
$hash = substr(md5_file($resourceExpression), 0, 8);
$resourceExpression = $this->resourceManager->getPublicPackageResourceUriByPath($resourceExpression);
}
$finalUri = $hash ? $resourceExpression . '?' . $hash : $resourceExpression;
$defer = key_exists('defer', $element) && $element['defer'] ? 'defer ' : '';
$result .= $builderForLine($finalUri, $defer);
}
return $result;
}
}