Completed
Push — master ( a52438...bf8826 )
by Henry
06:30
created

includes/Asset/Loader.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Asset;
3
4
use Redaxscript\Filesystem;
5
use Redaxscript\Registry;
6
use function file_get_contents;
7
use function is_file;
8
use function pathinfo;
9
use function str_replace;
10
11
/**
12
 * parent class to load and concat assets
13
 *
14
 * @since 3.0.0
15
 *
16
 * @package Redaxscript
17
 * @category Asset
18
 * @author Henry Ruhs
19
 */
20
21
class Loader
22
{
23
	/**
24
	 * collection of the loader
25
	 *
26
	 * @var array
27
	 */
28
29
	protected $_collectionArray = [];
30
31
	/**
32
	 * constructor of the class
33
	 *
34
	 * @since 3.0.0
35
	 *
36
	 * @param Registry $_registry instance of the registry class
37
	 */
38
39
	public function __construct(protected Registry $_registry)
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_PROTECTED, expecting T_VARIABLE
Loading history...
40
	{
41
	}
42
43
	/**
44
	 * init the class
45
	 *
46
	 * @since 3.0.0
47 5
	 *
48
	 * @param array $collectionArray
49 5
	 *
50 5
	 * @return self
51
	 */
52
53
	public function init(array $collectionArray = []) : self
54
	{
55
		$this->_collectionArray = $collectionArray;
56
		return $this;
57
	}
58
59
	/**
60
	 * get the collection array
61
	 *
62 5
	 * @since 3.0.0
63
	 *
64 5
	 * @return array
65 5
	 */
66
67
	public function getCollectionArray() : array
68
	{
69
		return $this->_collectionArray;
70
	}
71
72
	/**
73
	 * set the collection array
74
	 *
75
	 * @since 3.0.0
76 5
	 *
77
	 * @param array $collectionArray
78 5
	 */
79
80
	public function setCollectionArray(array $collectionArray = []) : void
81
	{
82
		$this->_collectionArray = $collectionArray;
83
	}
84
85
	/**
86
	 * concat the collection
87
	 *
88
	 * @since 3.0.0
89 4
	 *
90
	 * @param array $optionArray
91 4
	 * @param array $rewriteArray
92 4
	 *
93
	 * @return self
94
	 */
95
96
	public function concat(array $optionArray = [], array $rewriteArray = []) : self
97
	{
98
		$collectionArray = $this->getCollectionArray();
99
		$bundleArray = [];
100
		$restArray = [];
101
102
		/* prevent as needed */
103
104
		if ($this->_registry->get('noAssetCache'))
105 5
		{
106
			return $this;
107 5
		}
108 5
109 5
		/* process collection */
110
111
		foreach ($collectionArray as $attributeArray)
112
		{
113 5
			$path = $attributeArray[$optionArray['attribute']];
114
			$fileArray = pathinfo($path);
115 1
			if (is_file($path) && $fileArray['extension'] === $optionArray['extension'])
116
			{
117
				$bundleArray[] = $attributeArray[$optionArray['attribute']];
118
			}
119
			else
120 4
			{
121
				$restArray[] = $attributeArray;
122 4
			}
123 4
		}
124 4
125
		/* cache as needed */
126 4
127
		$this->_handleCache($rewriteArray, $bundleArray, $restArray, $optionArray);
128
		return $this;
129
	}
130 1
131
	/**
132
	 * handle the cache
133
	 *
134
	 * @since 4.0.0
135
	 *
136 4
	 * @param array $rewriteArray
137 4
	 * @param array $bundleArray
138
	 * @param array $restArray
139
	 * @param array $optionArray
140
	 */
141
142
	protected function _handleCache(array $rewriteArray = [], array $bundleArray = [], array $restArray = [], array $optionArray = []) : void
143
	{
144
		$cacheFilesystem = new Filesystem\Cache();
145
		$cacheFilesystem->init($optionArray['directory'], $optionArray['extension']);
146
147
		/* load from cache */
148
149
		if ($cacheFilesystem->validate($bundleArray, $optionArray['lifetime']))
150
		{
151 4
			$collectionArray = $restArray;
152
			$collectionArray['bundle'] =
153 4
			[
154 4
				$optionArray['attribute'] => $cacheFilesystem->getPath($bundleArray, '/')
155
			];
156
			if ($optionArray['extension'] === 'css')
157
			{
158 4
				$collectionArray['bundle']['rel'] = 'stylesheet';
159
			}
160 4
			$this->setCollectionArray($collectionArray);
161 4
		}
162
163 4
		/* else store to cache */
164
165 4
		else
166
		{
167 4
			$content = $this->_getContent($bundleArray, $rewriteArray);
168
			$cacheFilesystem->store($bundleArray, $content);
169 4
		}
170
	}
171
172
	/**
173
	 * get the content
174
	 *
175
	 * @since 3.0.0
176 4
	 *
177 4
	 * @param array $bundleArray
178
	 * @param array $rewriteArray
179 4
	 *
180
	 * @return string|null
181
	 */
182
183
	protected function _getContent(array $bundleArray = [], array $rewriteArray = []) : ?string
184
	{
185
		$output = null;
186
187
		/* process bundle */
188
189
		foreach ($bundleArray as $value)
190
		{
191
			$output .= file_get_contents($value);
192 4
		}
193
194 4
		/* process rewrite */
195
196
		foreach ($rewriteArray as $key => $value)
197
		{
198 4
			$output = str_replace($key, $value, $output);
199
		}
200 4
		return $output;
201
	}
202
}
203