eZ Publish  [trunk]
ezpextension.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the ezpExtension class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /**
00012  * Object representing an eZ Publish extension
00013  */
00014 class ezpExtension
00015 {
00016     public $name;
00017 
00018     /**
00019      * Array of multiton instances (Multiton pattern)
00020      *
00021      * @see getInstance
00022      */
00023     private static $instances = array();
00024 
00025     /**
00026      * ezpExtension constructor.
00027      *
00028      * @param string $name Name of the extension
00029      */
00030     protected function __construct( $name )
00031     {
00032         $this->name = $name;
00033     }
00034 
00035     /**
00036      * ezpExtension constructor.
00037      *
00038      * @see $instances
00039      *
00040      * @param string $name Name of the extension
00041      * @return ezpExtension
00042      */
00043     public static function getInstance( $name )
00044     {
00045         if (! isset( self::$instances[$name] ) )
00046             self::$instances[$name] = new self( $name );
00047 
00048         return self::$instances[$name];
00049     }
00050 
00051     /**
00052      * Returns the loading order informations from extension.xml
00053      *
00054      * @return array array( before => array( a, b ), after => array( c, d ) ) or an empty array if not available
00055      */
00056     public function getLoadingOrder()
00057     {
00058         $return = array( 'before' => array(), 'after' => array() );
00059 
00060         if ( is_readable( $XMLDependencyFile = eZExtension::baseDirectory() . "/{$this->name}/extension.xml" ) )
00061         {
00062             libxml_use_internal_errors( true );
00063             $xml = simplexml_load_file( $XMLDependencyFile );
00064             // xml parsing error
00065             if ( $xml === false )
00066             {
00067                 eZDebug::writeError( libxml_get_errors(), "ezpExtension( {$this->name} )::getLoadingOrder()" );
00068                 return null;
00069             }
00070             foreach ( $xml->dependencies as $dependenciesNode )
00071             {
00072                 foreach ( $dependenciesNode as $dependencyType => $dependenciesNode )
00073                 {
00074                     switch ( $dependencyType )
00075                     {
00076                         case 'requires':
00077                             $relationship = 'after';
00078                             break;
00079 
00080                         case 'uses':
00081                             $relationship = 'after';
00082                             break;
00083 
00084                         case 'extends':
00085                             $relationship = 'before';
00086                             break;
00087                     }
00088 
00089                     foreach ( $dependenciesNode as $dependency )
00090                     {
00091                         $return[$relationship][] = (string)$dependency['name'];
00092                     }
00093                 }
00094             }
00095         }
00096 
00097         return $return;
00098     }
00099 
00100     /**
00101      * Returns the extension informations
00102      * Uses extension.xml by default, then tries ezinfo.php for backwards compatibility
00103      *
00104      * @since 4.4
00105      * @return array|null array of extension informations, or null if no source exists
00106      */
00107     public function getInfo()
00108     {
00109         // try extension.xml first
00110         if ( is_readable( $XMLFilePath = eZExtension::baseDirectory() . "/{$this->name}/extension.xml" ) )
00111         {
00112             $infoFields = array( 'name', 'description', 'version', 'copyright', 'author', 'license', 'info_url' );
00113 
00114             libxml_use_internal_errors( true );
00115             $xml = simplexml_load_file( $XMLFilePath );
00116             // xml parsing error
00117             if ( $xml === false )
00118             {
00119                 eZDebug::writeError( libxml_get_errors(), "ezpExtension({$this->name})::getInfo()" );
00120                 return null;
00121             }
00122             $return = array();
00123             $metadataNode = $xml->metadata;
00124 
00125             // standard extension metadata
00126             foreach ( $infoFields as $field )
00127             {
00128                 if ( (string)$metadataNode->$field !== '' )
00129                     $return[$field] = (string)$metadataNode->$field;
00130             }
00131 
00132             // 3rd party software
00133             if ( !$metadataNode->software->uses )
00134                 return $return;
00135 
00136             $index = 1;
00137             foreach ( $metadataNode->software->uses as $software )
00138             {
00139                 $label = "Includes the following third-party software";
00140                 if ( $index > 1 )
00141                     $label .= " (" . $index . ")";
00142 
00143                 foreach ( $infoFields as $field )
00144                 {
00145                     if ( (string)$software->$field !== '' )
00146                         $return[$label][$field] = (string)$software->$field;
00147                 }
00148                 $index++;
00149             }
00150 
00151             return $return;
00152         }
00153         // then try ezinfo.php, for backwards compatibility
00154         elseif ( is_readable( $infoFilePath = eZExtension::baseDirectory() . "/{$this->name}/ezinfo.php" ) )
00155         {
00156             include_once( $infoFilePath );
00157             $className = $this->name . 'Info';
00158             if ( is_callable( array( $className, 'info' ) ) )
00159             {
00160                 $result = call_user_func_array( array( $className, 'info' ), array() );
00161                 if ( is_array( $result ) )
00162                 {
00163                     return $result;
00164                 }
00165             }
00166         }
00167         else
00168         {
00169             return null;
00170         }
00171     }
00172 }
00173 ?>