source: sandbox/newExpressoMail/newExpressoMail/Assetic/Factory/Worker/CacheBustingWorker.php @ 7265

Revision 7265, 1.8 KB checked in by gustavo, 12 years ago (diff)

Ticket #0000 - Criado novo modulo para o desenvolvimento do novo ExpressoMail?

  • Property svn:executable set to *
Line 
1<?php
2
3/*
4 * This file is part of the Assetic package, an OpenSky project.
5 *
6 * (c) 2010-2012 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
12namespace Assetic\Factory\Worker;
13
14use Assetic\Asset\AssetInterface;
15
16/**
17 * Adds cache busting code
18 *
19 * @author Kris Wallsmith <kris.wallsmith@gmail.com>
20 */
21class CacheBustingWorker implements WorkerInterface
22{
23    const STRATEGY_CONTENT = 1;
24    const STRATEGY_MODIFICATION = 2;
25
26    private $strategy;
27
28    public function __construct($strategy = self::STRATEGY_CONTENT)
29    {
30        $this->strategy = $strategy;
31    }
32
33    public function process(AssetInterface $asset)
34    {
35        $hash = hash_init('sha1');
36
37        switch($this->strategy) {
38            case self::STRATEGY_MODIFICATION:
39                hash_update($hash, $asset->getLastModified());
40                break;
41            case self::STRATEGY_CONTENT:
42                hash_update($hash, $asset->dump());
43                break;
44        }
45
46        foreach ($asset as $i => $leaf) {
47            if ($sourcePath = $leaf->getSourcePath()) {
48                hash_update($hash, $sourcePath);
49            } else {
50                hash_update($hash, $i);
51            }
52        }
53
54        $hash = substr(hash_final($hash), 0, 7);
55        $url = $asset->getTargetPath();
56
57        $oldExt = pathinfo($url, PATHINFO_EXTENSION);
58        $newExt = '-'.$hash.'.'.$oldExt;
59
60        if (!$oldExt || 0 < preg_match('/'.preg_quote($newExt, '/').'$/', $url)) {
61            return;
62        }
63
64        $asset->setTargetPath(substr($url, 0, (strlen($oldExt) + 1) * -1).$newExt);
65    }
66
67    public function getStrategy()
68    {
69        return $this->strategy;
70    }
71
72    public function setStrategy($strategy)
73    {
74        $this->strategy = $strategy;
75
76        return $this;
77    }
78}
Note: See TracBrowser for help on using the repository browser.