This repository has been archived by the owner on Nov 8, 2020. It is now read-only.
/
Dumper.php
121 lines (104 loc) · 2.96 KB
/
Dumper.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace SilexAssetic\Assetic;
use Symfony\Component\Finder\Finder;
use Assetic\Factory\LazyAssetManager;
use Assetic\AssetWriter;
use Assetic\AssetManager;
use Assetic\Extension\Twig\TwigResource;
class Dumper
{
/**
* @var AssetManager
*/
protected $am;
/**
* @var AssetWriter
*/
protected $writer;
/**
* @var LazyAssetManager
*/
protected $lam;
/**
* @var \Twig_Environment
*/
protected $twig;
/**
* @var \Twig_Loader_Filesystem
*/
protected $loaders;
/**
* Ctor
*
* @param AssetManager $am
* @param LazyAssetManager $lam
* @param AssetWriter $writer
*/
public function __construct(AssetManager $am, LazyAssetManager $lam, AssetWriter $writer)
{
$this->am = $am;
$this->lam = $lam;
$this->writer = $writer;
}
/**
* @param \Twig_Environment $twig
* @param \Twig_Loader_Filesystem $loader
*/
public function setTwig(\Twig_Environment $twig, \Twig_Loader_Filesystem $loader)
{
$this->twig = $twig;
$this->loader = $loader;
}
/**
* Locates twig templates and adds their defined assets to the lazy asset manager
*/
public function addTwigAssets()
{
if (!$this->twig instanceof \Twig_Environment) {
throw new \LogicException('Twig environment not set');
}
$finder = new Finder();
$iterator = $finder->files()->name('/\.twig$/')->in($this->loader->getPaths());
foreach ($iterator as $file) {
$resource = new TwigResource($this->loader, $file->getRelativePathname());
$this->lam->addResource($resource, 'twig');
}
}
/**
* Dumps all the assets
*/
public function dumpAssets()
{
$this->dumpManagerAssets($this->am);
$this->dumpManagerAssets($this->lam);
}
/**
* Dumps the assets of given manager
*
* Doesn't use AssetWriter::writeManagerAssets since we also want to dump non-combined assets
* (for example, when using twig extension in debug mode).
*
* @param AssetManager $am
* @param AssetWriter $writer
*/
protected function dumpManagerAssets(AssetManager $am)
{
foreach ($am->getNames() as $name) {
$asset = $am->get($name);
if ($am instanceof LazyAssetManager) {
$formula = $am->getFormula($name);
}
$this->writer->writeAsset($asset);
if (!isset($formula[2])) {
continue;
}
$debug = isset($formula[2]['debug']) ? $formula[2]['debug'] : $am->isDebug();
$combine = isset($formula[2]['combine']) ? $formula[2]['combine'] : null;
if (null !== $combine ? !$combine : $debug) {
foreach ($asset as $leaf) {
$this->writer->writeAsset($leaf);
}
}
}
}
}