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\Module\Installer;
39: use MOC\Api;
40: use MOC\Core\Changelog\Entry;
41: use MOC\Generic\Device\Module;
42:
43: 44: 45:
46: class Server implements Module {
47: 48: 49: 50: 51: 52: 53:
54: public static function InterfaceChangelog() {
55: return Api::Core()->Changelog();
56: }
57:
58: 59: 60: 61: 62: 63: 64:
65: public static function InterfaceDepending() {
66: return Api::Core()->Depending();
67: }
68:
69: 70: 71: 72: 73: 74: 75:
76: public static function InterfaceInstance() {
77: $Server = new Server();
78: $Server->SetServerLibrary( __DIR__.'/../../Library' );
79: return $Server;
80: }
81:
82:
83: public function CreateLibrary() {
84: $BaseDirectory = realpath( __DIR__.'/../../' );
85:
86: $VersionList = $this->GetVersionList();
87:
88: foreach( (array)$VersionList as $Class => $Version ) {
89:
90: set_time_limit( 120 );
91:
92: $File = basename( $Class ).'-'.$Version->Number().'.zip';
93: $Source = str_replace( '\MOC', '', $Class );
94: $Path = dirname( $Source );
95: $Location = self::$ServerLibrary.$Path.DIRECTORY_SEPARATOR.$File;
96: $Handler = Api::Module()->Drive()->File()->Open( $Location );
97:
98: $RootBase = dirname( $BaseDirectory.$Source );
99: $RootDir = $RootBase.DIRECTORY_SEPARATOR.basename( $BaseDirectory.$Source );
100: $RootFile = $RootDir.'.php';
101:
102: if(
103: ( false !== strpos( $Class, 'MOC\Adapter' ) )
104: ) {
105: if( !$Handler->Exists() ) {
106: $PackageDir = Api::Module()->Drive()->Directory()->Open( $Handler->GetPath() );
107: $PackageFile = Api::Module()->Packer()->Zip()->Encoder()->Open( $Handler );
108: $PackageFile->Content( Api::Module()->Drive()->File()->Open( $RootFile ), $RootBase );
109: }
110: }
111: else if(
112: ( false !== strpos( $Class, 'MOC\Core' ) )
113: || ( false !== strpos( $Class, 'MOC\Module' ) )
114: ) {
115: if( !$Handler->Exists() ) {
116: $PackageDir = Api::Module()->Drive()->Directory()->Open( $Handler->GetPath() );
117: $PackageFile = Api::Module()->Packer()->Zip()->Encoder()->Open( $Handler );
118: $PackageFile->Content( Api::Module()->Drive()->File()->Open( $RootFile ), $RootBase );
119: }
120: }
121: else if(
122: ( false !== strpos( $Class, 'MOC\Extension' ) )
123: ) {
124: if( !$Handler->Exists() ) {
125: $PackageDir = Api::Module()->Drive()->Directory()->Open( $Handler->GetPath() );
126: $PackageFile = Api::Module()->Packer()->Zip()->Encoder()->Open( $Handler );
127: $PackageFile->Content( Api::Module()->Drive()->File()->Open( $RootFile ), $RootBase );
128: if( is_dir( $RootBase.DIRECTORY_SEPARATOR.'3rdParty' ) ) {
129: $FileList = Api::Module()->Drive()->Directory()->Open(
130: $RootBase.DIRECTORY_SEPARATOR.'3rdParty'
131: )->FileList( true );
132:
133: foreach( (array)$FileList as $File ) {
134: $PackageFile->Content( $File, $RootBase );
135: }
136: }
137:
138: }
139: }
140: }
141: }
142:
143:
144: private static $ServerLibrary = '';
145: 146: 147: 148: 149:
150: public function SetServerLibrary( $Server ) {
151: self::$ServerLibrary = Api::Module()->Drive()->Directory()->Open( $Server )->GetLocation();
152: return $this;
153: }
154:
155: 156: 157: 158: 159: 160: 161:
162: public function GetProtocol( $Class ) {
163:
164: return $Class::InterfaceChangelog()->Protocol();
165: }
166:
167: 168: 169: 170: 171:
172: public function GetProtocolList() {
173: $ClassList = $this->GetClassList();
174: $List = array();
175:
176: foreach( (array)$ClassList as $Class ) {
177: $List = array_merge( $List, $Class::InterfaceChangelog()->Protocol() );
178: }
179: return $List;
180: }
181:
182: 183: 184:
185: public function GetVersionList() {
186: $ClassList = $this->GetClassList();
187:
188: $Version = array();
189:
190: foreach( (array)$ClassList as $Class ) {
191: $rClass = new \ReflectionClass( $Class );
192: if( $rClass->hasMethod( 'InterfaceChangelog' ) ) {
193: $Version[(string)$Class] = $Class::InterfaceChangelog();
194: $rClass = new \ReflectionClass( get_class( $Version[(string)$Class] ) );
195: if( $rClass->hasMethod( 'Version' ) ) {
196: $Version[(string)$Class] = $Version[(string)$Class]->Version();
197: } else {
198: trigger_error( 'Missing Version() in '.$Class );
199: }
200: } else {
201: trigger_error( 'Missing InterfaceChangelog() in '.$Class );
202: }
203: }
204: return $Version;
205: }
206:
207: 208: 209:
210: public function GetChangeLog() {
211: $ClassList = $this->GetClassList();
212:
213: $Log = array();
214:
215: foreach( (array)$ClassList as $Class ) {
216: $rClass = new \ReflectionClass( $Class );
217: if( $rClass->hasMethod( 'InterfaceChangelog' ) ) {
218: $Log = array_merge( $Log, $Class::InterfaceChangelog()->Log() );
219: }
220: }
221: return $Log;
222: }
223:
224: 225: 226:
227: public function GetChangeLogByTimeLine() {
228: $Log = $this->GetChangeLog();
229:
230: usort( $Log, function( Entry $A, Entry $B ) {
231: if( $A->Timestamp() > $B->Timestamp() ) {
232: return -1;
233: } else if( $A->Timestamp() < $B->Timestamp() ) {
234: return 1;
235: } else {
236: return 0;
237: }
238: });
239:
240: return $Log;
241: }
242:
243:
244: private static $ClassList = array();
245:
246: private $ClassDirectory = array(
247: 'Adapter',
248: 'Core', 'Extension', 'Module',
249: 'Plugin', 'Widget'
250: );
251: 252: 253:
254: public function GetClassList() {
255: if( empty( self::$ClassList ) ) {
256: $BaseDirectory = realpath( __DIR__.'/../../' );
257: foreach( (array)$this->ClassDirectory as $ClassDirectory ) {
258:
259: $FileList = Api::Module()
260: ->Drive()->Directory()->Open(
261: realpath( $BaseDirectory.DIRECTORY_SEPARATOR.$ClassDirectory )
262: )->FileList( true );
263:
264: array_walk( $FileList, function( &$File, $Index, $BaseDirectory ) {
265:
266: if(
267: false !== strpos( $File->GetPath(), '#Trash' )
268: || false !== strpos( $File->GetPath(), '3rdParty' )
269: || $File->GetExtension() !== 'php'
270: ) {
271: $File = false;
272: } else {
273: $File = '\\MOC'.str_replace( $BaseDirectory, '', $File->GetPath() ).'\\'.$File->GetName();
274: }
275: }, $BaseDirectory );
276:
277: $FileList = array_filter( $FileList );
278: self::$ClassList = array_merge( self::$ClassList, $FileList );
279: }
280:
281: sort( self::$ClassList );
282: }
283: return self::$ClassList;
284: }
285: }
286: