Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

ezextension.php
Go to the documentation of this file.
1 <?php
18 class eZExtension
19 {
25 const CACHE_DIR = 'var/cache/';
26
32 protected static $activeExtensionsCache = array();
33
40 static function baseDirectory( eZINI|null $siteINI = null )
41 {
42 if ( $siteINI === null )
43 $siteINI = eZINI::instance();
44 $extensionDirectory = $siteINI->variable( 'ExtensionSettings', 'ExtensionDirectory' );
45 return $extensionDirectory;
46 }
47
61 public static function activeExtensions( $extensionType = false, eZINI|null $siteINI = null )
62 {
63 if ( $siteINI === null )
64 {
65 $siteINI = eZINI::instance();
66 }
67
68 $activeExtensions = array();
69 if ( !$extensionType || $extensionType === 'default' )
70 {
71 $activeExtensions = $siteINI->variable( 'ExtensionSettings', 'ActiveExtensions' );
72 }
73
74 if ( !$extensionType || $extensionType === 'access' )
75 {
76 $activeExtensions = array_merge( $activeExtensions,
77 $siteINI->variable( 'ExtensionSettings', 'ActiveAccessExtensions' ) );
78 }
79
80 if ( isset( $GLOBALS['eZActiveExtensions'] ) )
81 {
82 $activeExtensions = array_merge( $activeExtensions,
83 $GLOBALS['eZActiveExtensions'] );
84 }
85
86 // return empty array as is to avoid further unneeded overhead
87 if ( !isset( $activeExtensions[0] ) )
88 {
89 return $activeExtensions;
90 }
91
92 // return array as is if ordering is disabled to avoid cache overhead
93 $activeExtensions = array_unique( $activeExtensions );
94 if ( $siteINI->variable( 'ExtensionSettings', 'ExtensionOrdering' ) !== 'enabled' )
95 {
96 // @todo Introduce a debug setting or re use existing dev mods to check that all extensions exists
97 return $activeExtensions;
98 }
99
100 $cacheIdentifier = md5( serialize( $activeExtensions ) );
101 if ( isset ( self::$activeExtensionsCache[$cacheIdentifier] ) )
102 {
103 return self::$activeExtensionsCache[$cacheIdentifier];
104 }
105
106 // cache has to be stored by siteaccess + $extensionType
107 $extensionDirectory = self::baseDirectory();
108 $expiryHandler = eZExpiryHandler::instance();
109 $phpCache = new eZPHPCreator( self::CACHE_DIR, "active_extensions_{$cacheIdentifier}.php" );
110 $expiryTime = $expiryHandler->hasTimestamp( 'active-extensions-cache' ) ? $expiryHandler->timestamp( 'active-extensions-cache' ) : 0;
111
112 if ( !$phpCache->canRestore( $expiryTime ) )
113 {
114 self::$activeExtensionsCache[$cacheIdentifier] = self::extensionOrdering( $activeExtensions );
115
116 // Check that all extensions defined actually exists before storing cache
117 foreach ( self::$activeExtensionsCache[$cacheIdentifier] as $activeExtension )
118 {
119 if ( !file_exists( $extensionDirectory . '/' . $activeExtension ) )
120 {
121 eZDebug::writeError( "Extension '$activeExtension' does not exist, looked for directory '" . $extensionDirectory . '/' . $activeExtension . "'", __METHOD__ );
122 }
123 }
124
125 $phpCache->addVariable( 'activeExtensions', self::$activeExtensionsCache[$cacheIdentifier] );
126 $phpCache->store( true );
127 }
128 else
129 {
130 $data = $phpCache->restore( array( 'activeExtensions' => 'activeExtensions' ) );
131 self::$activeExtensionsCache[$cacheIdentifier] = $data['activeExtensions'];
132 }
133
134 return self::$activeExtensionsCache[$cacheIdentifier];
135 }
136
144 public static function extensionOrdering( array $activeExtensions )
145 {
146 $activeExtensionsSet = array_flip( $activeExtensions );
147
148 $dependencies = array();
149 foreach ( $activeExtensions as $extension )
150 {
151 $loadingOrderData = ezpExtension::getInstance( $extension )->getLoadingOrder();
152
153 // The extension should appear even without dependencies to be taken into account
154 if ( ! isset( $dependencies[$extension] ) )
155 $dependencies[$extension] = array();
156
157 if ( isset( $loadingOrderData['after'] ) )
158 {
159 foreach ( $loadingOrderData['after'] as $dependency )
160 {
161 if ( isset( $activeExtensionsSet[$dependency] ) )
162 $dependencies[$extension][] = $dependency;
163 }
164 }
165 if ( isset( $loadingOrderData['before'] ) )
166 {
167 foreach ( $loadingOrderData['before'] as $dependency )
168 {
169 if ( isset( $activeExtensionsSet[$dependency] ) )
170 $dependencies[$dependency][] = $extension;
171 }
172 }
173 }
174
175 $topologySort = new ezpTopologicalSort( $dependencies );
176 $activeExtensionsSorted = $topologySort->sort();
177
178 return $activeExtensionsSorted !== false ? $activeExtensionsSorted : $activeExtensions;
179 }
180
188 static function activateExtensions( $extensionType = 'default', eZINI $siteINI = null )
189 {
190 if ( $siteINI === null )
191 {
192 $siteINI = eZINI::instance();
193 }
194
195 if ( $extensionType === false )
196 {
197 eZDebug::writeStrict( "Setting parameter \$extensionType to false is deprecated as of 4.4, see doc/bc/4.4!", __METHOD__ );
198 }
199
200 $extensionDirectory = self::baseDirectory();
201 $activeExtensions = self::activeExtensions( $extensionType, $siteINI );
202 $hasExtensions = false;
203 foreach ( $activeExtensions as $activeExtension )
204 {
205 $extensionSettingsPath = $extensionDirectory . '/' . $activeExtension . '/settings';
206
207 if ( $extensionType === 'access' )
208 $siteINI->prependOverrideDir( $extensionSettingsPath, true, 'extension:' . $activeExtension, 'sa-extension' );
209 else
210 $siteINI->prependOverrideDir( $extensionSettingsPath, true, 'extension:' . $activeExtension, 'extension' );
211
212 if ( isset( $GLOBALS['eZCurrentAccess'] ) )
213 self::prependSiteAccess( $activeExtension, $GLOBALS['eZCurrentAccess']['name'], $siteINI );
214
215 $hasExtensions = true;
216 }
217 if ( $hasExtensions )
218 $siteINI->load();
219 }
220
230 static function prependExtensionSiteAccesses( $accessName = false, $ini = false, $globalDir = true, $identifier = null, $order = true )
231 {
232 $extensionList = eZExtension::activeExtensions( 'default' );
233
234 if ( !$order )
235 {
236 $extensionList = array_reverse( $extensionList );
237 }
238
239 foreach( $extensionList as $extension )
240 {
241 self::prependSiteAccess( $extension, $accessName, $ini, $globalDir, $identifier );
242 }
243 }
244
256 static function prependSiteAccess( $extension, $accessName = false, $ini = false, $globalDir = true, $identifier = null )
257 {
258 if ( !$accessName )
259 {
260 $accessName = $GLOBALS['eZCurrentAccess']['name'];
261 }
262
263 $extensionSettingsPath = eZExtension::baseDirectory() . '/' . $extension;
264
265 if ( $identifier === null )
266 {
267 $identifier = "ext-siteaccess:$extension";
268 }
269
270 if ( !$ini instanceof eZINI )
271 {
272 $ini = eZINI::instance();
273 }
274 $ini->prependOverrideDir( $extensionSettingsPath . '/settings/siteaccess/' . $accessName, $globalDir, $identifier, 'siteaccess' );
275 }
276
283 static function expandedPathList( $extensionList, $subdirectory = false )
284 {
285 $pathList = array();
286 $extensionBase = eZExtension::baseDirectory();
287 foreach ( $extensionList as $extensionName )
288 {
289 $path = $extensionBase . '/' . $extensionName;
290 if ( $subdirectory )
291 $path .= '/' . $subdirectory;
292 $pathList[] = $path;
293 }
294 return $pathList;
295 }
296
334 static function findExtensionType( $parameters, &$out )
335 {
336 $iniName = $parameters['ini-name'];
337 $repositoryGroup = $parameters['repository-group'];
338 $repositoryVariable = $parameters['repository-variable'];
339 $extensionGroup = $parameters['extension-group'];
340 $extensionVariable = $parameters['extension-variable'];
341 $subdir = false;
342 if ( isset( $parameters['subdir'] ) )
343 $subdir = $parameters['subdir'];
344 $extensionSubdir = false;
345 if ( isset( $parameters['extension-subdir'] ) )
346 $extensionSubdir = $parameters['extension-subdir'];
347 $typeDirectory = true;
348 if ( isset( $parameters['type-directory'] ) )
349 $typeDirectory = $parameters['type-directory'];
350 $suffixName = $parameters['suffix-name'];
351 $ini = eZINI::instance( $iniName );
352 if ( isset( $parameters['type'] ) )
353 $originalType = $parameters['type'];
354 else if ( isset( $parameters['type-group'] ) and
355 isset( $parameters['type-variable'] ) )
356 $originalType = $ini->variable( $parameters['type-group'], $parameters['type-variable'] );
357 else
358 return false;
359 $type = $originalType;
360 if ( isset( $parameters['alias-group'] ) and
361 isset( $parameters['alias-variable'] ) )
362 {
363 if ( $ini->hasVariable( $parameters['alias-group'], $parameters['alias-variable'] ) )
364 {
365 $aliasMap = $ini->variable( $parameters['alias-group'], $parameters['alias-variable'] );
366 if ( isset( $aliasMap[$type] ) )
367 $type = $aliasMap[$type];
368 }
369 }
370
371 $baseDirectory = eZExtension::baseDirectory();
372 $repositoryDirectoryList = array();
373 $repositoryList = $ini->variable( $repositoryGroup, $repositoryVariable );
374 $extensionDirectories = $ini->variable( $extensionGroup, $extensionVariable );
375 foreach ( $repositoryList as $repository )
376 {
377 $repositoryDirectory = $repository;
378 if ( $subdir != '' )
379 $repositoryDirectory .= '/' . $subdir;
380 $repositoryDirectoryList[] = $repositoryDirectory;
381 }
382 foreach ( $extensionDirectories as $extensionDirectory )
383 {
384 $extensionPath = $baseDirectory . '/' . $extensionDirectory;
385 if ( $extensionSubdir != '' )
386 $extensionPath .= '/' . $extensionSubdir;
387 if ( file_exists( $extensionPath ) )
388 {
389 $repositoryDirectoryList[] = $extensionPath;
390 }
391 else if ( $extensionSubdir )
392 {
393 eZDebug::writeWarning( "Extension '$extensionDirectory' does not have the subdirectory $extensionSubdir, looked for directory '" . $extensionPath . "'" );
394 }
395 }
396 $foundType = false;
397 foreach ( $repositoryDirectoryList as $repositoryDirectory )
398 {
399 $fileDir = $repositoryDirectory;
400 if ( $typeDirectory )
401 $fileDir .= "/$type";
402 $fileName = $type . $suffixName;
403 $filePath = $fileDir . '/' . $fileName;
404 if ( file_exists( $filePath ) )
405 {
406 $foundType = true;
407 break;
408 }
409 }
410 $out['repository-directory-list'] = $repositoryDirectoryList;
411 if ( $foundType )
412 {
413 $out['type'] = $type;
414 $out['original-type'] = $originalType;
415 $out['found-file-dir'] = $fileDir;
416 $out['found-file-path'] = $filePath;
417 $out['found-file-name'] = $fileName;
418 }
419 $out['found-type'] = $foundType;
420 return $foundType;
421 }
422
433 static function extensionInfo( $extension )
434 {
435 return ezpExtension::getInstance( $extension )->getInfo();
436 }
437
445 static function nameFromPath( $path )
446 {
447 $path = eZDir::cleanPath( $path );
448 $base = eZExtension::baseDirectory() . '/';
449 $base = preg_quote( $base, '/' );
450 $pattern = '/'.$base.'([^\/]+)/';
451 if ( preg_match( $pattern, $path, $matches ) )
452 return $matches[1];
453 else
454 false;
455 }
456
463 static function isExtension( $path )
464 {
465 if ( eZExtension::nameFromPath( $path ) )
466 return true;
467 else
468 return false;
469 }
470
480 public static function getHandlerClass( ezpExtensionOptions $options )
481 {
482 $iniFile = $options->iniFile;
483 $iniSection = $options->iniSection;
484 $iniVariable = $options->iniVariable;
485 $handlerIndex = $options->handlerIndex;
486 $callMethod = $options->callMethod;
487 $handlerParams = $options->handlerParams;
488 $aliasSection = $options->aliasSection;
489 $aliasVariable = $options->aliasVariable;
490 $aliasOptionalIndex = $options->aliasOptionalIndex;
491
492 $ini = eZINI::instance( $iniFile );
493
494 if ( !$ini->hasVariable( $iniSection, $iniVariable ) )
495 {
496 eZDebug::writeError( 'Unable to find variable ' . $iniVariable . ' in section ' . $iniSection . ' in file ' . $iniFile, __METHOD__ );
497 return null;
498 }
499
500 $handlers = $ini->variable( $iniSection, $iniVariable );
501
502 if ( $handlerIndex !== null )
503 {
504 if ( is_array( $handlers ) && isset( $handlers[ $handlerIndex ] ) )
505 $handlers = $handlers[ $handlerIndex ];
506 else
507 return null;
508 }
509
510 // prepend alias settings if defined
511 if ( $aliasVariable !== null && is_string( $handlers ) )
512 {
513 if ( $aliasSection === null )
514 {
515 $aliasSection = $iniSection;
516 }
517 $aliasHandlers = $ini->variable( $aliasSection, $aliasVariable );
518 if ( $aliasOptionalIndex !== null && isset( $aliasHandlers[ $aliasOptionalIndex ] ) )
519 {
520 $handlers = array( $aliasHandlers[ $aliasOptionalIndex ], $handlers );
521 }
522 else if ( isset( $aliasHandlers[ $handlers ] ) )
523 {
524 $handlers = array( $aliasHandlers[ $handlers ], $handlers );
525 }
526 else
527 {
528 $handlers = array( $handlers );
529 }
530 }
531 else if ( !is_array( $handlers ) )
532 {
533 $handlers = array( $handlers );
534 }
535
536 foreach( $handlers as $handler )
537 {
538 // we rely on the autoload system here
539 if ( class_exists( $handler ) )
540 {
541 // only use reflection if we have params to avoid exception on objects without constructor
542 if ( $handlerParams !== null && is_array( $handlerParams ) && count( $handlerParams ) > 0 )
543 {
544 $reflection = new ReflectionClass( $handler );
545 // detect if class has a constructor, and if not write a notice about that
546 if ( $reflection->getConstructor() !== null )
547 {
548 $handlerParams = array_values( $handlerParams );
549 $object = $reflection->newInstanceArgs( $handlerParams );
550 }
551 else
552 {
553 eZDebug::writeNotice( 'Constructor is missing but parameters are provided to class ' . $handler . " as defined in setting $iniFile [$iniSection] $iniVariable", __METHOD__ );
554 $object = new $handler();
555 }
556 }
557 else
558 {
559 $object = new $handler();
560 }
561 // if callMethod is set, then call it so handler can decide if it is a valid handler
562 if ( $callMethod !== null )
563 {
564 if ( !is_callable( array( $object, $callMethod ) ) )
565 {
566 eZDebug::writeNotice( 'Method ' . $callMethod . ' is not callable on class ' . $handler . " as defined in setting $iniFile [$iniSection] $iniVariable", __METHOD__ );
567 continue;
568 }
569
570 if ( !call_user_func(array( $object, $callMethod ) ) )
571 continue;
572 }
573 return $object;
574 }
575 else
576 {
577 eZDebug::writeError( "Class '$handler' as defined in setting $iniFile [$iniSection] $iniVariable could not be autoloaded. Did you remember to run bin/php/ezpgenerateautoloads.php after you added extension(s)?", __METHOD__ );
578 }
579 }
580
581 return false;
582 }
583
588 public static function clearActiveExtensionsMemoryCache()
589 {
590 self::$activeExtensionsCache = array();
591 }
592
597 public static function clearActiveExtensionsCache()
598 {
599 $filesList = glob( self::CACHE_DIR . 'active_extensions_*.php' );
600 foreach ( $filesList as $path )
601 {
602 if ( is_file( $path ) )
603 {
604 $handler = eZFileHandler::instance( false );
605 $handler->unlink( $path );
606 }
607 }
608 }
609 }
610
611 ?>
eZDebug\writeStrict
static writeStrict( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:415
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:546
eZDebug\writeNotice
static writeNotice( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:460
eZDebug\writeWarning
static writeWarning( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:503
eZDir\cleanPath
static cleanPath( $path, $toType=self::SEPARATOR_UNIX)
Definition: ezdir.php:203
eZExtension
The class eZExtension does.
Definition: ezextension.php:19
eZExtension\expandedPathList
static expandedPathList( $extensionList, $subdirectory=false)
Definition: ezextension.php:283
eZExtension\prependSiteAccess
static prependSiteAccess( $extension, $accessName=false, $ini=false, $globalDir=true, $identifier=null)
Definition: ezextension.php:256
eZExtension\clearActiveExtensionsMemoryCache
static clearActiveExtensionsMemoryCache()
Definition: ezextension.php:588
eZExtension\activateExtensions
static activateExtensions( $extensionType='default', eZINI $siteINI=null)
Definition: ezextension.php:188
eZExtension\isExtension
static isExtension( $path)
Definition: ezextension.php:463
eZExtension\extensionInfo
static extensionInfo( $extension)
Definition: ezextension.php:433
eZExtension\nameFromPath
static nameFromPath( $path)
Definition: ezextension.php:445
eZExtension\findExtensionType
static findExtensionType( $parameters, &$out)
Definition: ezextension.php:334
eZExtension\$activeExtensionsCache
static $activeExtensionsCache
Definition: ezextension.php:32
eZExtension\extensionOrdering
static extensionOrdering(array $activeExtensions)
Definition: ezextension.php:144
eZExtension\activeExtensions
static activeExtensions( $extensionType=false, eZINI|null $siteINI=null)
Definition: ezextension.php:61
eZExtension\CACHE_DIR
const CACHE_DIR
Definition: ezextension.php:25
eZExtension\baseDirectory
static baseDirectory(eZINI|null $siteINI=null)
Definition: ezextension.php:40
eZExtension\getHandlerClass
static getHandlerClass(ezpExtensionOptions $options)
Definition: ezextension.php:480
eZExtension\prependExtensionSiteAccesses
static prependExtensionSiteAccesses( $accessName=false, $ini=false, $globalDir=true, $identifier=null, $order=true)
Definition: ezextension.php:230
eZExtension\clearActiveExtensionsCache
static clearActiveExtensionsCache()
Definition: ezextension.php:597
eZFileHandler\instance
static instance( $identifier, $filename=false, $mode=false, $binaryFile=true)
Definition: ezfilehandler.php:1018
eZINI
Reads and writes .ini style configuration files.
eZPHPCreator
eZPHPCreator provides a simple interface for creating and executing PHP code.
$ini
$ini
Definition: classes/admin/linklist.php:64
$type
$type
Definition: classes/admin/linkselect.php:220
ezpExtensionOptions
Definition: ezpextensionoptions.php:59
ezpExtension\getInstance
static getInstance( $name)
Definition: ezpextension.php:44
ezpTopologicalSort
Definition: ezptopologicalsort.php:16
file_exists
static file_exists( $inifilename)
Definition: eZPBINIFile.php:195
$fileName
if(eZObjectPermission::hasPermission( $file->id(), "filemanager_file", "r", $user)==false) $fileName
Definition: ezfilemanager/user/filedownload.php:43
$filePath
$filePath
Definition: ezfilemanager/user/filedownload.php:50
$options
$options
Definition: optionlist.php:56
$order
$order
Definition: productreport.php:182
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.