OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxmodulelist.php
Go to the documentation of this file.
1 <?php
2 
7 class oxModuleList extends oxSuperCfg
8 {
15  protected $_aModules = array();
16 
22  protected $_aSkipFiles = array( 'functions.php', 'vendormetadata.php' );
23 
29  public function getAllModules()
30  {
31  return $this->getConfig()->getAllModules();
32  }
33 
39  public function getActiveModuleInfo()
40  {
41  $aModulePaths = $this->getModulePaths();
42 
43  // Extract module paths from extended classes
44  if ( !is_array($aModulePaths) || count($aModulePaths) < 1 ) {
45  $aModulePaths = $this->extractModulePaths();
46  }
47 
48  $aDisabledModules = $this->getDisabledModules();
49  if ( is_array($aDisabledModules) && count($aDisabledModules) > 0 && count($aModulePaths) > 0 ) {
50  $aModulePaths = array_diff_key($aModulePaths, array_flip($aDisabledModules));
51  }
52  return $aModulePaths;
53  }
54 
60  public function getDisabledModuleInfo()
61  {
62  $aDisabledModules = $this->getDisabledModules();
63  $aModulePaths = array();
64 
65  if ( is_array($aDisabledModules) && count($aDisabledModules) > 0 ) {
66  $aModulePaths = $this->getModulePaths();
67 
68  // Extract module paths from extended classes
69  if ( !is_array($aModulePaths) || count($aModulePaths) < 1 ) {
70  $aModulePaths = $this->extractModulePaths();
71  }
72 
73  if ( is_array($aModulePaths) || count($aModulePaths) > 0 ) {
74  $aModulePaths = array_intersect_key($aModulePaths, array_flip($aDisabledModules));
75  }
76  }
77 
78  return $aModulePaths;
79  }
80 
86  public function getModuleVersions()
87  {
88  return $this->getConfig()->getConfigParam('aModuleVersions');
89  }
90 
96  public function getModules()
97  {
98  return $this->getConfig()->getConfigParam('aModules');
99  }
100 
106  public function getLegacyModules()
107  {
108  return $this->getConfig()->getConfigParam('aLegacyModules');
109  }
110 
116  public function getDisabledModules()
117  {
118  return $this->getConfig()->getConfigParam('aDisabledModules');
119  }
120 
126  public function getModulePaths()
127  {
128  return $this->getConfig()->getConfigParam('aModulePaths');
129  }
130 
136  public function getModuleEvents()
137  {
138  return (array) $this->getConfig()->getConfigParam('aModuleEvents');
139  }
140 
146  public function extractModulePaths()
147  {
148  $aModules = $this->getAllModules();
149  $aModulePaths = array();
150 
151  if (is_array($aModules) && count($aModules) > 0) {
152  foreach ($aModules as $aModuleClasses) {
153  foreach ($aModuleClasses as $sModule) {
154  $sModuleId = substr($sModule, 0, strpos($sModule, "/"));
155  $aModulePaths[$sModuleId] = $sModuleId;
156  }
157  }
158  }
159  return $aModulePaths;
160  }
161 
167  public function getModuleFiles()
168  {
169  return $this->getConfig()->getConfigParam('aModuleFiles');
170  }
171 
177  public function getModuleTemplates()
178  {
179  return $this->getConfig()->getConfigParam('aModuleTemplates');
180  }
181 
190  public function getDisabledModuleClasses()
191  {
192  $aDisabledModules = $this->getDisabledModules();
193  $aModules = $this->getAllModules();
194  $aModulePaths = $this->getModulePaths();
195 
196  $aDisabledModuleClasses = array();
197  if (isset($aDisabledModules) && is_array($aDisabledModules)) {
198  //get all disabled module paths
199  foreach ($aDisabledModules as $sId) {
200  $sPath = $aModulePaths[$sId];
201  if (!isset($sPath)) {
202  $sPath = $sId;
203  }
204  foreach ( $aModules as $sClass => $aModuleClasses ) {
205  foreach ( $aModuleClasses as $sModuleClass ) {
206  if (strpos($sModuleClass, $sPath."/") === 0 ) {
207  $aDisabledModuleClasses[] = $sModuleClass;
208  }
209  }
210  }
211  }
212  }
213 
214  return $aDisabledModuleClasses;
215  }
216 
222  public function cleanup()
223  {
224  $aDeletedExt = $this->getDeletedExtensions();
225 
226  //collecting deleted extension IDs
227  $aDeletedExtIds = $this->getDeletedExtensionIds($aDeletedExt);
228 
229  // removing from aModules config array
230  $this->_removeFromModulesArray( $aDeletedExt );
231 
232  // removing from aDisabledModules array
233  $this->_removeFromDisabledModulesArray( $aDeletedExtIds );
234 
235  // removing from aLegacyModules array
236  $this->_removeFromLegacyModulesArray( $aDeletedExtIds );
237 
238  // removing from aModulePaths array
239  $this->_removeFromModulesPathsArray( $aDeletedExtIds );
240 
241  // removing from aModuleEvents array
242  $this->_removeFromModulesEventsArray( $aDeletedExtIds );
243 
244  // removing from aModuleVersions array
245  $this->_removeFromModulesVersionsArray( $aDeletedExtIds );
246 
247  // removing from aModuleFiles array
248  $this->_removeFromModulesFilesArray( $aDeletedExtIds );
249 
250  // removing from aModuleTemplates array
251  $this->_removeFromModulesTemplatesArray( $aDeletedExtIds );
252 
253  //removing from config tables and templates blocks table
254  $this->_removeFromDatabase( $aDeletedExtIds );
255  }
256 
264  public function getDeletedExtensionIds($aDeletedExt)
265  {
266  $aDeletedExtIds = array();
267  if ( !empty($aDeletedExt) ) {
268  $oModule = oxNew('oxModule');
269  foreach ( $aDeletedExt as $sOxClass => $aDeletedModules ) {
270  foreach ( $aDeletedModules as $sModulePath ) {
271  $aDeletedExtIds[] = $oModule->getIdByPath($sModulePath);
272  }
273  }
274  }
275 
276  if ( !empty( $aDeletedExtIds ) ) {
277  $aDeletedExtIds = array_unique( $aDeletedExtIds );
278  }
279 
280  return $aDeletedExtIds;
281  }
282 
288  public function getDeletedExtensions()
289  {
290  $aModules = $this->getAllModules();
291  $aDeletedExt = array();
292 
293  foreach ( $aModules as $sOxClass => $aModulesList ) {
294  foreach ( $aModulesList as $sModulePath ) {
295  $sExtPath = $this->getConfig()->getModulesDir() . $sModulePath.'.php';
296  if ( !file_exists( $sExtPath ) ) {
297  $aDeletedExt[$sOxClass][] = $sModulePath;
298  }
299  }
300  }
301 
302  return $aDeletedExt;
303  }
304 
314  public function diffModuleArrays($aAllModuleArray, $aRemModuleArray)
315  {
316  if (is_array($aAllModuleArray) && is_array($aRemModuleArray)) {
317  foreach ($aAllModuleArray as $sClass => $aModuleChain) {
318  if (!is_array($aModuleChain)) {
319  $aModuleChain = array($aModuleChain);
320  }
321  if (isset($aRemModuleArray[$sClass])) {
322  if (!is_array($aRemModuleArray[$sClass])) {
323  $aRemModuleArray[$sClass] = array($aRemModuleArray[$sClass]);
324  }
325  $aAllModuleArray[$sClass] = array();
326  foreach ($aModuleChain as $sModule) {
327  if (!in_array($sModule, $aRemModuleArray[$sClass])) {
328  $aAllModuleArray[$sClass][] = $sModule;
329  }
330  }
331  if (!count($aAllModuleArray[$sClass])) {
332  unset ($aAllModuleArray[$sClass]);
333  }
334  } else {
335  $aAllModuleArray[$sClass] = $aModuleChain;
336  }
337  }
338 
339  }
340 
341  return $aAllModuleArray;
342  }
343 
351  public function buildModuleChains($aModuleArray)
352  {
353  $aModules = array();
354  if (is_array($aModuleArray)) {
355  foreach ($aModuleArray as $sClass => $aModuleChain) {
356  $aModules[$sClass] = implode('&', $aModuleChain);
357  }
358  }
359  return $aModules;
360  }
361 
369  protected function _removeFromModulesArray( $aDeletedExt )
370  {
371  $aExt = $this->getAllModules();
372  $aUpdatedExt = $this->diffModuleArrays( $aExt, $aDeletedExt );
373  $aUpdatedExt = $this->buildModuleChains( $aUpdatedExt );
374 
375  $this->getConfig()->saveShopConfVar( 'aarr', 'aModules', $aUpdatedExt );
376  }
377 
385  protected function _removeFromDisabledModulesArray( $aDeletedExtIds )
386  {
387  $oConfig = $this->getConfig();
388  $aDisabledExtensionIds = $this->getDisabledModules();
389  $aDisabledExtensionIds = array_diff($aDisabledExtensionIds, $aDeletedExtIds);
390  $oConfig->saveShopConfVar( 'arr', 'aDisabledModules', $aDisabledExtensionIds );
391  }
392 
400  protected function _removeFromLegacyModulesArray( $aDeletedExtIds )
401  {
402  $aLegacyExt = $this->getLegacyModules();
403 
404  foreach ( $aDeletedExtIds as $sDeletedExtId ) {
405  if ( isset($aLegacyExt[$sDeletedExtId]) ) {
406  unset( $aLegacyExt[$sDeletedExtId] );
407  }
408  }
409 
410  $this->getConfig()->saveShopConfVar( 'aarr', 'aLegacyModules', $aLegacyExt );
411  }
412 
420  protected function _removeFromModulesPathsArray( $aDeletedModule )
421  {
422  $aModulePaths = $this->getModulePaths();
423 
424  foreach ( $aDeletedModule as $sDeletedModuleId ) {
425  if ( isset($aModulePaths[$sDeletedModuleId]) ) {
426  unset( $aModulePaths[$sDeletedModuleId] );
427  }
428  }
429 
430  $this->getConfig()->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
431  }
432 
440  protected function _removeFromModulesVersionsArray( $aDeletedModule )
441  {
442  $aModuleVersions = $this->getModuleVersions();
443 
444  foreach ( $aDeletedModule as $sDeletedModuleId ) {
445  if ( isset($aModuleVersions[$sDeletedModuleId]) ) {
446  unset( $aModuleVersions[$sDeletedModuleId] );
447  }
448  }
449 
450  $this->getConfig()->saveShopConfVar( 'aarr', 'aModuleVersions', $aModuleVersions );
451  }
452 
460  protected function _removeFromModulesEventsArray( $aDeletedModule )
461  {
462  $aModuleEvents = $this->getModuleEvents();
463 
464  foreach ( $aDeletedModule as $sDeletedModuleId ) {
465  if ( isset($aModuleEvents[$sDeletedModuleId]) ) {
466  unset( $aModuleEvents[$sDeletedModuleId] );
467  }
468  }
469 
470  $this->getConfig()->saveShopConfVar( 'aarr', 'aModuleEvents', $aModuleEvents );
471  }
472 
480  protected function _removeFromModulesFilesArray( $aDeletedModule )
481  {
482  $aModuleFiles = $this->getModuleFiles();
483 
484  foreach ( $aDeletedModule as $sDeletedModuleId ) {
485  if ( isset($aModuleFiles[$sDeletedModuleId]) ) {
486  unset( $aModuleFiles[$sDeletedModuleId] );
487  }
488  }
489 
490  $this->getConfig()->saveShopConfVar( 'aarr', 'aModuleFiles', $aModuleFiles );
491  }
492 
500  protected function _removeFromModulesTemplatesArray( $aDeletedModule )
501  {
502  $aModuleTemplates = $this->getModuleTemplates();
503 
504  foreach ( $aDeletedModule as $sDeletedModuleId ) {
505  if ( isset($aModuleTemplates[$sDeletedModuleId]) ) {
506  unset( $aModuleTemplates[$sDeletedModuleId] );
507  }
508  }
509 
510  $this->getConfig()->saveShopConfVar( 'aarr', 'aModuleTemplates', $aModuleTemplates );
511  }
512 
520  protected function _removeFromDatabase( $aDeletedExtIds )
521  {
522  if ( !is_array($aDeletedExtIds) || !count($aDeletedExtIds) ) {
523  return;
524  }
525 
526  $oDb = oxDb::getDb();
527 
528  $aConfigIds = $sDelExtIds = array();
529  foreach ( $aDeletedExtIds as $sDeletedExtId ) {
530  $aConfigIds[] = $oDb->quote('module:'.$sDeletedExtId);
531  $sDelExtIds[] = $oDb->quote($sDeletedExtId);
532  }
533 
534  $sConfigIds = implode( ', ', $aConfigIds );
535  $sDelExtIds = implode( ', ', $sDelExtIds );
536 
537  $aSql[] = "DELETE FROM oxconfig where oxmodule IN ($sConfigIds)";
538  $aSql[] = "DELETE FROM oxconfigdisplay where oxcfgmodule IN ($sConfigIds)";
539  $aSql[] = "DELETE FROM oxtplblocks where oxmodule IN ($sDelExtIds)";
540 
541  foreach ( $aSql as $sQuery ) {
542  $oDb->execute( $sQuery );
543  }
544  }
545 
555  public function getModulesFromDir( $sModulesDir, $sVendorDir = null )
556  {
557  $sModulesDir = oxRegistry::get('oxUtilsFile')->normalizeDir( $sModulesDir );
558 
559  foreach ( glob( $sModulesDir.'*' ) as $sModuleDirPath ) {
560 
561  $sModuleDirPath .= ( is_dir( $sModuleDirPath ) ) ?'/':'';
562  $sModuleDirName = basename( $sModuleDirPath );
563 
564  // skipping some file
565  if ( in_array( $sModuleDirName, $this->_aSkipFiles ) || (!is_dir( $sModuleDirPath ) && substr($sModuleDirName, -4) != ".php")) {
566  continue;
567  }
568 
569  if ( $this->_isVendorDir( $sModuleDirPath ) ) {
570  // scanning modules vendor directory
571  $this->getModulesFromDir( $sModuleDirPath, basename( $sModuleDirPath ) );
572  } else {
573  // loading module info
574  $oModule = oxNew( 'oxModule' );
575  $sModuleDirName = ( !empty($sVendorDir) ) ? $sVendorDir.'/'.$sModuleDirName : $sModuleDirName;
576  $oModule->loadByDir( $sModuleDirName );
577  $sModuleId = $oModule->getId();
578  $this->_aModules[$sModuleId] = $oModule;
579 
580  $aModulePaths = $this->getModulePaths();
581 
582  if ( !is_array($aModulePaths) || !array_key_exists( $sModuleId, $aModulePaths ) ) {
583  // saving module path info
584  $this->_saveModulePath( $sModuleId, $sModuleDirName );
585 
586  //checking if this is new module and if it extends any eshop class
587  if ( !$this->_extendsClasses( $sModuleDirName ) ) {
588  // if not - marking it as disabled by default
589  $oModule->deactivate();
590  }
591  }
592  }
593  }
594  // sorting by name
595  if ( $this->_aModules !== null ) {
596  uasort($this->_aModules, array($this, '_sortModules'));
597  }
598 
599  return $this->_aModules;
600  }
601 
610  protected function _sortModules( $oModule1, $oModule2 )
611  {
612  return strcasecmp($oModule1->getTitle(), $oModule2->getTitle());
613  }
614 
622  protected function _isVendorDir( $sModuleDir )
623  {
624  if ( is_dir( $sModuleDir ) && file_exists( $sModuleDir . 'vendormetadata.php' ) ) {
625  return true;
626  }
627 
628  return false;
629  }
630 
638  protected function _extendsClasses ( $sModuleDir )
639  {
640  $aModules = $this->getConfig()->getConfigParam( 'aModules' );
641  if (is_array($aModules)) {
642  $sModules = implode( '&', $aModules );
643 
644  if ( preg_match("@(^|&+)".$sModuleDir."\b@", $sModules ) ) {
645  return true;
646  }
647  }
648 
649  return false;
650  }
651 
660  protected function _saveModulePath( $sModuleId, $sModulePath )
661  {
662  $aModulePaths = $this->getModulePaths();
663 
664  $aModulePaths[$sModuleId] = $sModulePath;
665  $this->getConfig()->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
666  }
667 
668 }