|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Assetic package, an OpenSky project. |
|
|
5 |
* |
|
|
6 |
* (c) 2010-2011 OpenSky Project Inc |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Assetic\Factory\Loader; |
|
|
13 |
|
|
|
14 |
use Assetic\Cache\ConfigCache; |
|
|
15 |
use Assetic\Factory\Resource\IteratorResourceInterface; |
|
|
16 |
use Assetic\Factory\Resource\ResourceInterface; |
|
|
17 |
|
|
|
18 |
/** |
|
|
19 |
* Adds a caching layer to a loader. |
|
|
20 |
* |
|
|
21 |
* A cached formula loader is a composition of a formula loader and a cache. |
|
|
22 |
* |
|
|
23 |
* @author Kris Wallsmith <kris.wallsmith@gmail.com> |
|
|
24 |
*/ |
|
|
25 |
class CachedFormulaLoader implements FormulaLoaderInterface |
|
|
26 |
{ |
|
|
27 |
private $loader; |
|
|
28 |
private $configCache; |
|
|
29 |
private $debug; |
|
|
30 |
|
|
|
31 |
/** |
|
|
32 |
* Constructor. |
|
|
33 |
* |
|
|
34 |
* When the loader is in debug mode it will ensure the cached formulae |
|
|
35 |
* are fresh before returning them. |
|
|
36 |
* |
|
|
37 |
* @param FormulaLoaderInterface $loader A formula loader |
|
|
38 |
* @param ConfigCache $configCache A config cache |
|
|
39 |
* @param Boolean $debug The debug mode |
|
|
40 |
*/ |
|
|
41 |
public function __construct(FormulaLoaderInterface $loader, ConfigCache $configCache, $debug = false) |
|
|
42 |
{ |
|
|
43 |
$this->loader = $loader; |
|
|
44 |
$this->configCache = $configCache; |
|
|
45 |
$this->debug = $debug; |
|
|
46 |
} |
|
|
47 |
|
|
|
48 |
public function load(ResourceInterface $resources) |
|
|
49 |
{ |
|
|
50 |
if (!$resources instanceof IteratorResourceInterface) { |
|
|
51 |
$resources = array($resources); |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
$formulae = array(); |
|
|
55 |
|
|
|
56 |
foreach ($resources as $resource) { |
|
|
57 |
$id = (string) $resource; |
|
|
58 |
if (!$this->configCache->has($id) || ($this->debug && !$resource->isFresh($this->configCache->getTimestamp($id)))) { |
|
|
59 |
$formulae += $this->loader->load($resource); |
|
|
60 |
$this->configCache->set($id, $formulae); |
|
|
61 |
} else { |
|
|
62 |
$formulae += $this->configCache->get($id); |
|
|
63 |
} |
|
|
64 |
} |
|
|
65 |
|
|
|
66 |
return $formulae; |
|
|
67 |
} |
|
|
68 |
} |