1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37: 
 38: namespace MOC\Plugin;
 39: use MOC\Api;
 40: use MOC\Generic\Common;
 41: 
 42:  43:  44: 
 45: class Repository implements Common {
 46:      47:  48:  49:  50:  51:  52: 
 53:     public static function InterfaceChangelog() {
 54:         Api::Core()->Changelog();
 55:     }
 56: 
 57:      58:  59:  60:  61:  62:  63: 
 64:     public static function InterfaceDepending() {
 65:         Api::Core()->Depending();
 66:     }
 67: 
 68:     
 69:     private static $Singleton = null;
 70: 
 71:      72:  73:  74:  75:  76:  77: 
 78:     public static function InterfaceInstance() {
 79:         if( self::$Singleton === null ) {
 80:             self::$Singleton = new Repository();
 81:             self::$Singleton->LoadRepository();
 82:         } return self::$Singleton;
 83:     }
 84: 
 85:     
 86:     private $Repository = array();
 87:     
 88:     private $Helper = array(
 89:         'mocStyleSheetHelper',
 90:         'mocJavaScriptHelper'
 91:     );
 92: 
 93:     private function LoadRepository() {
 94:         $Repository = Api::Module()->Drive()->Directory()->Open( __DIR__.DIRECTORY_SEPARATOR.'Repository' )->FileList();
 95: 
 96:         
 97:         foreach( $this->Helper as $Plugin ) {
 98:             $Reflection = new \ReflectionClass( 'MOC\\Plugin\\Repository\\'.$Plugin );
 99:             $Plugin = $Reflection->newInstance();
100:             $this->Repository[$Reflection->getParentClass()->getName()][$Reflection->getShortName()] = $Plugin;
101:         }
102: 
103:         foreach( $Repository as $Plugin ) {
104:             try {
105:                 $Reflection = new \ReflectionClass( 'MOC\\Plugin\\Repository\\'.$Plugin->GetName() );
106:                 if( is_object( $Reflection->getParentClass() )
107:                     && is_object( $Reflection->getParentClass()->getParentClass() )
108:                     && is_object( $Reflection->getParentClass()->getParentClass()->getParentClass() )
109:                     && $Reflection->getParentClass()->getParentClass()->getParentClass()->getName() == 'MOC\\Plugin\\Shared'
110:                 ) {
111: 
112:                     
113:                     $Plugin = $Reflection->newInstance();
114:                     if( !isset( $this->Repository[$Reflection->getParentClass()->getName()] )
115:                         || !isset( $this->Repository[$Reflection->getParentClass()->getName()][$Reflection->getShortName()] )
116:                     ) {
117:                         $this->Repository[$Reflection->getParentClass()->getName()][$Reflection->getShortName()] = $Plugin;
118:                         $Plugin->PluginLoader();
119:                     }
120:                 } else {
121:                     Api::Core()->Error()->Type()->Exception()->Trigger( 'Plugin-Repository: '.$Reflection->getName().' is not a plugin!', $Plugin->GetLocation(), $Reflection->getStartLine() );
122:                 }
123:             } catch( \Exception $Exception  ) {
124:                 Api::Core()->Error()->Type()->Exception()->Trigger( $Exception->getMessage(), $Exception->getFile(), $Exception->getLine(), $Exception->getTraceAsString() );
125:             }
126:         }
127:     }
128: 
129:     130: 131: 132: 133: 134: 
135:     public function Execute( Shared $Shared, $PluginName = null ) {
136:         $SharedReflection = new \ReflectionObject( $Shared );
137:         if( $SharedReflection->getNamespaceName() != 'MOC\\Plugin\\Shared' ) {
138:             Api::Core()->Error()->Type()->Exception()->Trigger( 'Plugin-Repository: '.$SharedReflection->getName().' is not a valid plugin configuration!', __FILE__, __LINE__ );
139:         }
140:         $SharedPropertyList = $SharedReflection->getProperties();
141:         $PluginGateway = 'MOC\\Plugin\\Gateway\\'.$SharedReflection->getShortName();
142:         foreach( $this->Repository[$PluginGateway] as $Plugin ) {
143:             
144:             if( $PluginName !== null && $PluginName != $Plugin->PluginName() ) {
145:                 continue;
146:             }
147:             
148:             $Prospect = clone $Plugin;
149:             foreach( $SharedPropertyList as $Property ) {
150:                 $Prospect->{$Property->getName()}( $Shared->{$Property->getName()}() );
151:             }
152:             if( $Prospect->PluginCapable() ) {
153:                 return $Prospect;
154:             }
155:         }
156:         if( $PluginName !== null ) {
157:             Api::Core()->Error()->Type()->Exception()->Trigger( 'Plugin-Repository: '.$PluginName.' is either not available or not a capable plugin!', __FILE__, __LINE__ );
158:         } else {
159:             Api::Core()->Error()->Type()->Exception()->Trigger( 'Plugin-Repository: Missing capable '.$PluginGateway.' plugin!', __FILE__, __LINE__ );
160:         }
161:         return null;
162:     }
163: }
164: