26
26
*/
27
27
class DumpCommand extends Command
28
28
{
29
+ private $ basePath ;
30
+ private $ am ;
31
+
29
32
protected function configure ()
30
33
{
31
34
$ this
@@ -36,21 +39,29 @@ protected function configure()
36
39
;
37
40
}
38
41
39
- protected function execute (InputInterface $ input , OutputInterface $ output )
42
+ protected function initialize (InputInterface $ input , OutputInterface $ output )
40
43
{
41
- if (!$ basePath = $ input ->getArgument ('write_to ' )) {
42
- $ basePath = $ this ->container ->getParameter ('assetic.write_to ' );
43
- }
44
+ parent ::initialize ($ input , $ output );
44
45
45
- $ am = $ this ->container ->get ('assetic.asset_manager ' );
46
+ $ this ->basePath = $ input ->getArgument ('write_to ' ) ?: $ this ->container ->getParameter ('assetic.write_to ' );
47
+ $ this ->am = $ this ->container ->get ('assetic.asset_manager ' );
48
+ }
49
+
50
+ protected function execute (InputInterface $ input , OutputInterface $ output )
51
+ {
52
+ if (!$ input ->getOption ('watch ' )) {
53
+ foreach ($ this ->am ->getNames () as $ name ) {
54
+ $ this ->dumpAsset ($ name , $ output );
55
+ }
46
56
47
- if ($ input ->getOption ('watch ' )) {
48
- return $ this ->watch ($ am , $ basePath , $ output , $ this ->container ->getParameter ('kernel.debug ' ));
57
+ return ;
49
58
}
50
59
51
- foreach ( $ am ->getNames () as $ name ) {
52
- $ this -> dumpAsset ( $ am -> get ( $ name ), $ basePath , $ output );
60
+ if (! $ this -> am ->isDebug () ) {
61
+ throw new \ RuntimeException ( ' The --watch option is only available in debug mode. ' );
53
62
}
63
+
64
+ $ this ->watch ($ output );
54
65
}
55
66
56
67
/**
@@ -59,22 +70,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
59
70
* This method includes an infinite loop the continuously polls the asset
60
71
* manager for changes.
61
72
*
62
- * @param LazyAssetManager $am The asset manager
63
- * @param string $basePath The base directory to write to
64
- * @param OutputInterface $output The command output
65
- * @param Boolean $debug Debug mode
73
+ * @param OutputInterface $output The command output
66
74
*/
67
- protected function watch (LazyAssetManager $ am , $ basePath , OutputInterface $ output, $ debug = false )
75
+ private function watch (OutputInterface $ output )
68
76
{
69
- if (!$ debug ) {
70
- throw new \RuntimeException ('The --watch option is only available in debug mode. ' );
71
- }
72
-
73
77
$ refl = new \ReflectionClass ('Assetic \\AssetManager ' );
74
78
$ prop = $ refl ->getProperty ('assets ' );
75
79
$ prop ->setAccessible (true );
76
80
77
- $ cache = sys_get_temp_dir ().'/assetic_watch_ ' .substr (sha1 ($ basePath ), 0 , 7 );
81
+ $ cache = sys_get_temp_dir ().'/assetic_watch_ ' .substr (sha1 ($ this -> basePath ), 0 , 7 );
78
82
if (file_exists ($ cache )) {
79
83
$ previously = unserialize (file_get_contents ($ cache ));
80
84
} else {
@@ -84,15 +88,15 @@ protected function watch(LazyAssetManager $am, $basePath, OutputInterface $outpu
84
88
$ error = '' ;
85
89
while (true ) {
86
90
try {
87
- foreach ($ am ->getNames () as $ name ) {
88
- if ($ asset = $ this ->checkAsset ($ am , $ name , $ previously )) {
89
- $ this ->dumpAsset ($ asset , $ basePath , $ output );
91
+ foreach ($ this -> am ->getNames () as $ name ) {
92
+ if ($ asset = $ this ->checkAsset ($ name , $ previously )) {
93
+ $ this ->dumpAsset ($ asset , $ output );
90
94
}
91
95
}
92
96
93
97
// reset the asset manager
94
- $ prop ->setValue ($ am , array ());
95
- $ am ->load ();
98
+ $ prop ->setValue ($ this -> am , array ());
99
+ $ this -> am ->load ();
96
100
97
101
file_put_contents ($ cache , serialize ($ previously ));
98
102
$ error = '' ;
@@ -110,16 +114,15 @@ protected function watch(LazyAssetManager $am, $basePath, OutputInterface $outpu
110
114
/**
111
115
* Checks if an asset should be dumped.
112
116
*
113
- * @param LazyAssetManager $am The asset manager
114
- * @param string $name The asset name
115
- * @param array &$previously An array of previous visits
117
+ * @param string $name The asset name
118
+ * @param array &$previously An array of previous visits
116
119
*
117
120
* @return AssetInterface|Boolean The asset if it should be dumped
118
121
*/
119
- protected function checkAsset (LazyAssetManager $ am , $ name , array &$ previously )
122
+ private function checkAsset ($ name , array &$ previously )
120
123
{
121
- $ formula = $ am ->hasFormula ($ name ) ? serialize ($ am ->getFormula ($ name )) : null ;
122
- $ asset = $ am ->get ($ name );
124
+ $ formula = $ this -> am ->hasFormula ($ name ) ? serialize ($ this -> am ->getFormula ($ name )) : null ;
125
+ $ asset = $ this -> am ->get ($ name );
123
126
$ mtime = $ asset ->getLastModified ();
124
127
125
128
if (isset ($ previously [$ name ])) {
@@ -136,15 +139,39 @@ protected function checkAsset(LazyAssetManager $am, $name, array &$previously)
136
139
/**
137
140
* Writes an asset.
138
141
*
139
- * @param AssetInterface $asset An asset
140
- * @param string $basePath The base directory to write to
141
- * @param OutputInterface $output The command output
142
+ * If the application or asset is in debug mode, each leaf asset will be
143
+ * dumped as well.
144
+ *
145
+ * @param string $name An asset name
146
+ * @param OutputInterface $output The command output
147
+ */
148
+ private function dumpAsset ($ name , OutputInterface $ output )
149
+ {
150
+ $ asset = $ this ->am ->get ($ name );
151
+ $ formula = $ this ->am ->getFormula ($ name );
152
+
153
+ // start by dumping the main asset
154
+ $ this ->doDump ($ asset , $ output );
155
+
156
+ // dump each leaf if debug
157
+ if (isset ($ formula [2 ]['debug ' ]) ? $ formula [2 ]['debug ' ] : $ this ->am ->isDebug ()) {
158
+ foreach ($ asset as $ leaf ) {
159
+ $ this ->doDump ($ leaf , $ output );
160
+ }
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Performs the asset dump.
166
+ *
167
+ * @param AssetInterface $asset An asset
168
+ * @param OutputInterface $output The command output
142
169
*
143
170
* @throws RuntimeException If there is a problem writing the asset
144
171
*/
145
- protected function dumpAsset (AssetInterface $ asset, $ basePath , OutputInterface $ output )
172
+ private function doDump (AssetInterface $ asset , OutputInterface $ output )
146
173
{
147
- $ target = rtrim ($ basePath , '/ ' ).'/ ' .str_replace ('_controller/ ' , '' , $ asset ->getTargetUrl ());
174
+ $ target = rtrim ($ this -> basePath , '/ ' ).'/ ' .str_replace ('_controller/ ' , '' , $ asset ->getTargetUrl ());
148
175
if (!is_dir ($ dir = dirname ($ target ))) {
149
176
$ output ->writeln ('<info>[dir+]</info> ' .$ dir );
150
177
if (false === @mkdir ($ dir , 0777 , true )) {
0 commit comments