Completed
Branch master (8f7741)
by
unknown
02:56
created

Loader::getCollectionArray()   A

↳ Parent: Loader

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 4
Code Lines 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Redaxscript\Asset;
3
4
use Redaxscript\Cache;
5
use Redaxscript\Registry;
6
7
/**
8
 * parent class to load and concat assets
9
 *
10
 * @since 3.0.0
11
 *
12
 * @package Redaxscript
13
 * @category Asset
14
 * @author Henry Ruhs
15
 */
16
17
class Loader
18
{
19
	/**
20
	 * instance of the registry class
21
	 *
22
	 * @var object
23
	 */
24
25
	protected $_registry;
26
27
	/**
28
	 * collection of the loader
29
	 *
30
	 * @var array
31
	 */
32
33
	protected $_collectionArray = [];
34
35
	/**
36
	 * constructor of the class
37
	 *
38
	 * @since 3.0.0
39
	 *
40
	 * @param Registry $registry instance of the registry class
41
	 */
42
43
	public function __construct(Registry $registry)
44
	{
45
		$this->_registry = $registry;
46
	}
47
48
	/**
49
	 * init the class
50
	 *
51
	 * @since 3.0.0
52
	 *
53
	 * @param array $collectionArray
54
	 *
55
	 * @return Loader
56
	 */
57
58
	public function init($collectionArray = [])
59
	{
60
		if (is_array($collectionArray))
61
		{
62
			$this->_collectionArray = $collectionArray;
63
		}
64
		return $this;
65
	}
66
67
	/**
68
	 * get the collection array
69
	 *
70
	 * @since 3.0.0
71
	 *
72
	 * @return array
73
	 */
74
75
	public function getCollectionArray()
76
	{
77
		return $this->_collectionArray;
78
	}
79
80
	/**
81
	 * concat the collection
82
	 *
83
	 * @since 3.0.0
84
	 *
85
	 * @param array $optionArray
86
	 * @param array $rewriteArray
87
	 *
88
	 * @return Loader
89
	 */
90
91
	public function concat($optionArray = [], $rewriteArray = [])
92
	{
93
		$bundleArray = [];
94
		$restArray = [];
95
96
		/* prevent as needed */
97
98
		if ($this->_registry->get('noCache'))
99
		{
100
			return $this;
101
		}
102
103
		/* process collection */
104
105
		foreach ($this->_collectionArray as $collectionKey => $attributeArray)
106
		{
107
			$path = $attributeArray[$optionArray['attribute']];
108
			$fileArray = pathinfo($path);
109
			if (is_file($path) && $fileArray['extension'] === $optionArray['extension'])
110
			{
111
				$bundleArray[] = $attributeArray[$optionArray['attribute']];
112
			}
113
			else
114
			{
115
				$restArray[] = $attributeArray;
116
			}
117
		}
118
119
		/* cache as needed */
120
121
		$cache = new Cache();
122
		$cache->init($optionArray['directory'], $optionArray['extension']);
123
124
		/* load from cache */
125
126
		if ($cache->validate($bundleArray, $optionArray['lifetime']))
127
		{
128
			$this->_collectionArray = $restArray;
129
			$this->_collectionArray['bundle'] =
130
			[
131
				$optionArray['attribute'] => $cache->getPath($bundleArray)
132
			];
133
			if ($optionArray['extension'] === 'css')
134
			{
135
				$this->_collectionArray['bundle']['rel'] = 'stylesheet';
136
			}
137
		}
138
139
		/* else store to cache */
140
141
		else
142
		{
143
			$content = $this->_getContent($bundleArray, $rewriteArray);
144
			$cache->store($bundleArray, $content);
145
		}
146
		return $this;
147
	}
148
149
	/**
150
	 * get the content
151
	 *
152
	 * @since 3.0.0
153
	 *
154
	 * @param array $bundleArray
155
	 * @param array $rewriteArray
156
	 *
157
	 * @return string
158
	 */
159
160
	protected function _getContent($bundleArray = [], $rewriteArray = [])
161
	{
162
		$output = null;
163
164
		/* process bundle */
165
166
		foreach ($bundleArray as $value)
167
		{
168
			$output .= file_get_contents($value);
169
		}
170
171
		/* process rewrite */
172
173
		foreach ($rewriteArray as $key => $value)
174
		{
175
			$output = str_replace($key, $value, $output);
176
		}
177
		return $output;
178
	}
179
}