@@ -35,6 +35,7 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa
35
35
parent ::install ($ repo , $ package );
36
36
37
37
$ this ->_installAutoloader ($ package );
38
+ $ this ->_copyAssets ($ package );
38
39
}
39
40
40
41
/**
@@ -47,8 +48,8 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini
47
48
parent ::update ($ repo , $ initial , $ target );
48
49
49
50
$ this ->_installAutoloader ($ target );
51
+ $ this ->_copyAssets ($ target );
50
52
}
51
-
52
53
/**
53
54
* Installs the default autoloader if no autoloader is supplied.
54
55
*
@@ -57,14 +58,7 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini
57
58
protected function _installAutoloader (PackageInterface $ package )
58
59
{
59
60
$ path = $ this ->_getAutoloaderPath ($ package );
60
- $ manifest = $ this ->_getKoowaManifest ($ this ->getInstallPath ($ package ));
61
-
62
- if (!($ manifest instanceof \SimpleXMLElement))
63
- {
64
- throw new \InvalidArgumentException (
65
- 'Failed to load `koowa-component.xml` manifest for package ` ' .$ package ->getPrettyName ().'`. '
66
- );
67
- }
61
+ $ manifest = $ this ->_getKoowaManifest ($ package );
68
62
69
63
if (!file_exists ($ path ))
70
64
{
@@ -79,11 +73,13 @@ protected function _installAutoloader(PackageInterface $package)
79
73
* You can override this autoloader by supplying an autoload.php file in the root of the relevant component.
80
74
**/
81
75
76
+ KoowaAutoloader::bootstrap();
77
+
82
78
$ classname::getInstance()
83
79
->getObject('lib:object.bootstrapper')
84
80
->registerComponent(
85
81
' $ component',
86
- dirname(__FILE__) ,
82
+ __DIR__ ,
87
83
' $ vendor'
88
84
);
89
85
EOL ;
@@ -116,9 +112,11 @@ protected function _removeAutoloader(PackageInterface $package)
116
112
*
117
113
* @param PackageInterface $package
118
114
* @return bool|\SimpleXMLElement Instance of SimpleXMLElement or false on failure
115
+ * @throws `InvalidArgumentException` on failure to load the XML manifest
119
116
*/
120
- protected function _getKoowaManifest ($ path )
117
+ protected function _getKoowaManifest (PackageInterface $ package )
121
118
{
119
+ $ path = $ this ->getInstallPath ($ package );
122
120
$ directory = new \RecursiveDirectoryIterator ($ path , \RecursiveDirectoryIterator::KEY_AS_PATHNAME );
123
121
$ iterator = new \RecursiveIteratorIterator ($ directory );
124
122
$ regex = new \RegexIterator ($ iterator , '/koowa-component\.xml/ ' , \RegexIterator::GET_MATCH );
@@ -131,6 +129,13 @@ protected function _getKoowaManifest($path)
131
129
$ manifests = array_keys ($ files );
132
130
$ manifest = simplexml_load_file ($ manifests [0 ]);
133
131
132
+ if (!($ manifest instanceof \SimpleXMLElement))
133
+ {
134
+ throw new \InvalidArgumentException (
135
+ 'Failed to load `koowa-component.xml` manifest for package ` ' .$ package ->getPrettyName ().'`. '
136
+ );
137
+ }
138
+
134
139
return $ manifest ;
135
140
}
136
141
@@ -170,4 +175,84 @@ protected function _getObjectManagerClassName()
170
175
171
176
return $ platform ? 'Nooku\Library\ObjectManager ' : 'KObjectManager ' ;
172
177
}
178
+
179
+ /**
180
+ * Copy assets into the media folder if the installation is running in a Joomla context
181
+ *
182
+ * @param PackageInterface $package
183
+ */
184
+ protected function _copyAssets (PackageInterface $ package )
185
+ {
186
+ $ path = rtrim ($ this ->getInstallPath ($ package ), '/ ' );
187
+ $ asset_path = $ path .'/resources/assets ' ;
188
+ $ vendor_dir = dirname (dirname ($ path ));
189
+
190
+ // Check for libraries/joomla. vendor directory sits in libraries/ folder in Joomla 3.4+
191
+ $ is_joomla = is_dir (dirname ($ vendor_dir ).'/joomla ' ) || is_dir (dirname ($ vendor_dir ).'/libraries/joomla ' );
192
+
193
+ if ($ is_joomla && is_dir ($ asset_path ))
194
+ {
195
+ $ manifest = $ this ->_getKoowaManifest ($ package );
196
+
197
+ $ root = is_dir (dirname ($ vendor_dir ).'/joomla ' ) ? dirname (dirname ($ vendor_dir )) : dirname ($ vendor_dir );
198
+ $ destination = $ root .'/media/koowa/com_ ' .$ manifest ->name ;
199
+
200
+ $ this ->_copyDirectory ($ asset_path , $ destination );
201
+ }
202
+ }
203
+
204
+ /**
205
+ * Copy source folder into target. Clears the target folder first.
206
+ *
207
+ * @param $source
208
+ * @param $target
209
+ * @return bool
210
+ */
211
+ protected function _copyDirectory ($ source , $ target )
212
+ {
213
+ $ result = false ;
214
+
215
+ if (!is_dir ($ target )) {
216
+ $ result = mkdir ($ target , 0755 , true );
217
+ }
218
+ else
219
+ {
220
+ // Clear directory
221
+ $ iter = new \RecursiveDirectoryIterator ($ target );
222
+ foreach (new \RecursiveIteratorIterator ($ iter , \RecursiveIteratorIterator::CHILD_FIRST ) as $ f )
223
+ {
224
+ if ($ f ->isDir ())
225
+ {
226
+ if (!in_array ($ f ->getFilename (), array ('. ' , '.. ' ))) {
227
+ rmdir ($ f ->getPathname ());
228
+ }
229
+ } else {
230
+ unlink ($ f ->getPathname ());
231
+ }
232
+ }
233
+ }
234
+
235
+ if (is_dir ($ target ))
236
+ {
237
+ $ result = true ; // needed for empty directories
238
+ $ iterator = new \RecursiveIteratorIterator (new \RecursiveDirectoryIterator ($ source ), \RecursiveIteratorIterator::SELF_FIRST );
239
+ foreach ($ iterator as $ f )
240
+ {
241
+ if ($ f ->isDir ()) {
242
+ $ path = $ target .'/ ' .$ iterator ->getSubPathName ();
243
+ if (!is_dir ($ path )) {
244
+ $ result = mkdir ($ path );
245
+ }
246
+ } else {
247
+ $ result = copy ($ f , $ target .'/ ' .$ iterator ->getSubPathName ());
248
+ }
249
+
250
+ if ($ result === false ) {
251
+ break ;
252
+ }
253
+ }
254
+ }
255
+
256
+ return $ result ;
257
+ }
173
258
}
0 commit comments