forked from konfirm/konsolidate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkonsolidate.class.php
executable file
·757 lines (685 loc) · 21.9 KB
/
konsolidate.class.php
1
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
<?php
/*
* ________ ___
* / / /\ /\ Konsolidate
* ____/ /___/ \/ \
* / /\ / http://www.konsolidate.nl
* /___ ___/ \ /
* \ / /\ \ / \
* \/___/ \___\/ \
* \ \ /\ \ /\ /
* \___\/ \___\/ \/
* \ \ / $Rev$
* \___ ___\/ $Author$
* \ \ / $Date$
* \___\/
*
* The konsolidate class, which acts as the 'one ring' being responsible for the proper inner workings of the Konsolidate framework/library/foundation
* @name Konsolidate
* @type class
* @author Rogier Spieker <rogier@konsolidate.nl>
*/
class Konsolidate implements Iterator
{
/**
* The parent Konsolidate object
* @name _parent
* @type object
* @access protected
*/
protected $_parent;
/**
* Run in debug mode
* @name _debug
* @type object
* @access protected
*/
protected $_debug;
/**
* The array in which all modules (plugins) are stored for re-use
* @name _module
* @type array
* @access protected
*/
protected $_module;
/**
* The array in which all custom properties are stored
* @name _property
* @type array
* @access protected
*/
protected $_property;
/**
* The array in which all modulepaths relative to the current instance are stored
* @name _path
* @type array
* @access protected
*/
protected $_path;
/**
* The character(s) which seperates objects (and the final method) in the call method
* @name _path
* @type array
* @access protected
*/
protected $_objectseparator;
/**
* Module reference cache, making lookups faster
* @name _lookupcache
* @type array
* @access protected
*/
protected $_lookupcache;
/**
* Module lookup cache, making lookups faster for checkModuleAvailability
* @name _modulecheck
* @type array
* @access protected
*/
static protected $_modulecheck;
/**
* Error traces
* @name _tracelog
* @type array
* @access protected
*/
protected $_tracelog;
/**
* Konsolidate constructor
* @name Konsolidate
* @type constructor
* @access public
* @param array array with the paths to load the modules from (the order of the paths is the order in which to look up modules)
* @return object
* @syntax object = new Konsolidate( array path )
* @note The syntax described is the syntax the implementor of Konsolidate should use, all childnodes constructed by Konsolidate
* are handled by the internals of Konsolidate.
*/
public function __construct( $mPath )
{
$this->_debug = false;
$this->_module = Array();
$this->_property = Array();
$this->_lookupcache = Array();
$this->_tracelog = Array();
if ( is_object( $mPath ) && $mPath instanceof Konsolidate )
{
$this->_parent = $mPath;
$this->_path = $this->getFilePath();
$this->_objectseparator = $this->_parent->_objectseparator;
}
else if ( is_array( $mPath ) ) // We are the Root instance
{
$this->_parent = false;
$this->_path = $mPath;
$this->_objectseparator = isset( $this->_objectseparator ) && !empty( $this->_objectseparator ) ? $this->_objectseparator : "/";
// We always want access to our static tools
$this->import( "tool.class.php" );
}
}
/**
* get a property value from a module using a path
* @name get
* @type method
* @access public
* @param string path to the property to get
* @param mixed default return value (optional, default null)
* @return mixed
* @syntax Konsolidate->get( string module [, mixed default ] );
* @note supplying a default value should be done per call, the default is never stored
*/
public function get()
{
$aArgument = func_get_args();
$sProperty = array_shift( $aArgument );
$mDefault = (bool) count( $aArgument ) ? array_shift( $aArgument ) : null;
$nSeperator = strrpos( $sProperty, $this->_objectseparator );
if ( $nSeperator !== false && ( $oModule = $this->getModule( substr( $sProperty, 0, $nSeperator ) ) ) !== false )
return $oModule->get( substr( $sProperty, $nSeperator + 1 ), $mDefault );
else if ( $this->checkModuleAvailability( $sProperty ) )
return $this->register( $sProperty );
$mReturn = $this->$sProperty;
return is_null( $mReturn ) ? $mDefault : $mReturn; // can (and will be by default!) still be null
}
/**
* set a property in a module using a path
* @name set
* @type method
* @access public
* @param string path to the property to set
* @param mixed value
* @return void
* @syntax Konsolidate->set( string module, mixed value );
*/
public function set()
{
$aArgument = func_get_args();
$sProperty = array_shift( $aArgument );
$nSeperator = strrpos( $sProperty, $this->_objectseparator );
if ( $nSeperator !== false && ( $oModule = $this->getModule( substr( $sProperty, 0, $nSeperator ) ) ) !== false )
{
array_unshift( $aArgument, substr( $sProperty, $nSeperator + 1 ) );
return call_user_func_array(
Array(
$oModule, // the object
"set" // the method
),
$aArgument // the arguments
);
}
$mValue = array_shift( $aArgument );
$this->$sProperty = $mValue;
return $this->$sProperty === $mValue;
}
/**
* Call a method from a module and return its return value
* @name call
* @type method
* @access public
* @param string path to the method to call
* @param mixed [optional] argument
* @return mixed
* @syntax Konsolidate->call( string module [, mixed argument [, mixed argument, [, ... ] ] ] );
* @note One can supply as many arguments as needed
*/
public function call()
{
$aArgument = func_get_args();
$sCall = array_shift( $aArgument );
$nSeperator = strrpos( $sCall, $this->_objectseparator );
if ( $nSeperator !== false )
{
$oModule = $this->getModule( substr( $sCall, 0, $nSeperator ) );
$sMethod = substr( $sCall, $nSeperator + 1 );
}
else
{
$oModule = $this;
$sMethod = $sCall;
}
if ( !is_object( $oModule ) )
{
$this->call( "/Log/write", "Module '" . get_class( $oModule ) . "' not found!" );
return false;
}
return call_user_func_array(
Array(
$oModule, // the object
$sMethod // the method
),
$aArgument // the arguments
);
}
/**
* Register a (unique) sub module of the current one
* @name register
* @type method
* @access public
* @param string modulename
* @return object
* @syntax Konsolidate->register( string module );
* @note register only create a single (unique) instance and always returns the same instance
* use the instance method to create different instances of the same class
*/
public function register( $sModule )
{
$sModule = strToUpper( $sModule );
if ( !array_key_exists( $sModule, $this->_module ) )
{
$oModule = $this->instance( $sModule );
if ( $oModule === false )
return $oModule;
$this->_module[ $sModule ] = $oModule;
}
return $this->_module[ $sModule ];
}
/**
* Create a sub module of the current one
* @name instance
* @type method
* @access public
* @param string modulename
* @param mixed param N
* @return object
* @syntax Konsolidate->instance( string module [, mixed param1 [, mixed param2 [, mixed param N ] ] ] );
* @note instance creates an instance every time you call it, if you require a single instance which
* is always returned, use the register method
*/
public function instance( $sModule )
{
// In case we request an instance of a remote node, we verify it here and leave the instancing to the instance parent
$nSeperator = strrpos( $sModule, $this->_objectseparator );
if ( $nSeperator !== false && ( $oModule = $this->getModule( substr( $sModule, 0, $nSeperator ) ) ) !== false )
{
$aArgument = func_get_args();
if ( count( $aArgument ) )
{
$aArgument[ 0 ] = substr( $aArgument[ 0 ], $nSeperator + 1 );
return call_user_func_array(
Array(
$oModule,
"instance"
),
$aArgument
);
}
}
// optimize the number of calls to import, as importing is rather expensive due to the file I/O involved
static $aImported = Array();
if ( !isset( $aImported[ $sModule ] ) )
{
$aImported[ $sModule ] = microtime(true);
$this->import( "{$sModule}.class.php" );
}
// try to construct the module classes top down, this ensures the correct order of construction
$bConstructed = false;
foreach ( $this->_path as $sMod=>$sPath )
{
$sClass = "{$sMod}" . ucFirst( strToLower( $sModule ) );
if ( class_exists( $sClass ) )
{
$aArgument = func_get_args();
array_shift( $aArgument ); // the first argument is always the module to instance, we discard it
if ( (bool) count( $aArgument ) )
{
array_unshift( $aArgument, $this ); // inject the 'parent reference', as Konsolidate dictates
$oModule = new ReflectionClass( $sClass );
$oModule = $oModule->newInstanceArgs( $aArgument );
}
else
{
$oModule = new $sClass( $this );
}
$bConstructed = is_object( $oModule );
break;
}
}
if ( !$bConstructed )
{
// create class stubs on the fly
eval( "class {$sClass} extends Konsolidate{ public \$_dynamicStubClass=true; }" );
$oModule = new $sClass( $this );
$bConstructed = is_object( $oModule );
if ( !$bConstructed )
return false;
}
return $oModule;
}
/**
* Import a file within the tree
* @name import
* @type method
* @access public
* @param string filename
* @return bool
* @syntax Konsolidate->import( string file );
*/
public function import( $sFile )
{
$nSeperator = strrpos( $sFile, $this->_objectseparator );
if ( $nSeperator !== false && ( $oModule = $this->getModule( substr( $sFile, 0, $nSeperator ) ) ) !== false )
return $oModule->import( substr( $sFile, $nSeperator + 1 ) );
// include all imported files (if they exist) bottom up, this solves the implementation classes having to know core paths
$aIncluded = array_flip( get_included_files() );
$aPath = array_reverse( $this->_path, true );
$bImported = false;
foreach ( $aPath as $sPath )
{
$sCurrentFile = realpath("{$sPath}/" . strToLower( $sFile ));
if ( $sCurrentFile )
{
include_once( $sCurrentFile );
$bImported = true;
}
}
return $bImported;
}
/**
* Verify whether given module exists (either for real, or as required stub as per directory structure)
* @name import
* @type method
* @access public
* @param string module
* @return object
* @syntax Konsolidate->checkModuleAvailability( string module );
*/
public function checkModuleAvailability( $sModule )
{
$sModule = strtolower($sModule);
$sClass = get_class($this);
// lookahead to submodules
if (!isset(self::$_modulecheck[$sClass]))
$this->_indexModuleAvailability();
// if we are dealing with a submodule pattern which is not in our cache by default, test for it
if (strpos($sModule, $this->_objectseparator) !== false)
foreach ( $this->_path as $sMod=>$sPath )
if ( realpath( "{$sPath}/{$sModule}.class.php" ) || realpath( "{$sPath}/{$sModule}" ) )
{
self::$_modulecheck[$sClass][$sModule] = true;
break;
}
return isset(self::$_modulecheck[$sClass][$sModule]) ? self::$_modulecheck[$sClass][$sModule] : false;
}
/**
* Get the root node
* @name getRoot
* @type method
* @access public
* @return mixed
* @syntax Konsolidate->getRoot();
*/
public function getRoot()
{
if ( $this->_parent !== false )
return $this->_parent->getRoot();
return $this;
}
/**
* Get the parent node, if any
* @name getParent
* @type method
* @access public
* @return mixed
* @syntax Konsolidate->getParent();
*/
function getParent()
{
if ( $this->_parent !== false )
return $this->_parent;
return false;
}
/**
* Get the file path based on the location in the Konsolidate Tree
* @name getFilePath
* @type method
* @access public
* @return mixed
* @syntax Konsolidate->getFilePath();
*/
public function getFilePath()
{
if ( is_array( $this->_path ) )
{
return $this->_path;
}
else
{
$aParentPath = $this->_parent->getFilePath();
$sClass = str_replace( array_keys( $aParentPath ), "", get_class( $this ) );
$aPath = Array();
foreach ( $aParentPath as $sTier=>$sPath )
{
$sClassPath = $sPath . "/" . strToLower( $sClass );
if ( realpath( $sClassPath ) )
$aPath[ "{$sTier}{$sClass}" ] = $sClassPath;
}
return $aPath;
}
}
/**
* Get a reference to a module based on it's path
* @name getModule
* @type method
* @access public
* @param string module path
* @return mixed
* @syntax Konsolidate->getModule( string path );
*/
public function getModule( $sCall )
{
$sPath = strToUpper( $sCall );
if ( !array_key_exists( $sPath, $this->_lookupcache ) )
{
$aPath = explode( $this->_objectseparator, $sPath );
$oModule = $this;
while( is_object( $oModule ) && count( $aPath ) )
{
$sSegment = array_shift( $aPath );
switch( strToLower( $sSegment ) )
{
case "": // root
case "_root":
$oTraverse = $oModule->getRoot();
break;
case "..": // parent
case "_parent": //
$oTraverse = $oModule->getParent();
break;
case ".": // self
$oTraverse = $this;
break;
default: // child
$oTraverse = $oModule->register( $sSegment );
break;
}
if ( !is_object( $oTraverse ) )
{
$this->call( "/Log/write", "Module '{$sSegment}' not found in module " . get_class( $oModule ) . "!", 3 );
return $oTraverse;
}
$oModule = $oTraverse;
}
$this->_lookupcache[ $sPath ] = $oModule;
}
return $this->_lookupcache[ $sPath ];
}
public function getTopAuthoredClass()
{
if ( property_exists( $this, "_dynamicStubClass" ) )
return $this->call( "../getTopAuthoredClass" );
return get_class( $this );
}
// Iterator functionality
public function key()
{
return key( $this->_property );
}
public function current()
{
return current( $this->_property );
}
public function next()
{
return next( $this->_property );
}
public function rewind()
{
return reset( $this->_property );
}
public function valid()
{
return !is_null( $this->key() );
}
// End Iterator functionality
/**
* Throw exceptions
* @name exception
* @type method
* @access public
* @param string message (option)
* @param int code (option)
* @return void
* @syntax Konsolidate->exception( [ string message [, int code ] ] );
* @note Exception classes must be an extend of PHP's built-in Exception class, if the exception method is called and the calling module does not
* have an exception class, Konsolidate will generate one dynamically.
* @note Exception classname use the same structure as normal Konsolidated classnames, but they must omit the tiername, e.g. you have a module
* 'Example' in the tier 'Demo' (class DemoExample in example.class.php), its exception class name should be (or will be generated dynamically)
* 'ExampleException' and be located in the file example/exception.class.php.
*/
public function exception( $sMessage=null, $nCode=0 )
{
$this->import( "exception.class.php" );
$sThrowClass = str_replace( array_keys( $this->getRoot()->getFilePath() ), "", get_class( $this ) . "Exception" );
$sExceptionClass = "";
foreach ( $this->_path as $sMod=>$sPath )
{
$sClass = "{$sMod}Exception";
if ( class_exists( $sClass ) )
{
$sExceptionClass = $sClass;
break;
}
}
$aTrace = debug_backtrace();
$sFile = "";
$sLine = "";
if ( count( $aTrace ) >= 4 && isset( $aTrace[ 3 ] ) )
{
// The origin of species
$sFile = $aTrace[ 3 ][ "file" ];
$sLine = $aTrace[ 3 ][ "line" ];
}
if ( empty( $sExceptionClass ) )
$sExceptionClass = "Exception";
// Create tierless Exception on the fly if the requested Exception does not exist
if ( !class_exists( $sThrowClass ) )
{
eval( "class {$sThrowClass} extends {$sExceptionClass}{public function __construct(\$s=null,\$c=0){parent::__construct(\$s,\$c);\$this->file='{$sFile}';\$this->line= (int) '{$sLine}';}}" );
}
if ( class_exists( $sThrowClass ) )
throw new $sThrowClass( $sMessage, $nCode );
throw new Exception( $sMessage, $nCode );
}
// Magic methods.
public function __set( $sProperty, $mValue )
{
if ( array_key_exists( strToUpper( $sProperty ), $this->_module ) )
throw new Exception( "Trying to overwrite existing module {$sProperty} in " . get_class( $this ) . " with " . gettype( $mValue ) . " {$mValue}" );
else if ( $this->checkModuleAvailability( $sProperty ) )
throw new Exception( "Trying to set a property " . gettype( $mValue ) . " {$mValue} in " . get_class( $this ) . " where a module is available" );
$this->_property[ $sProperty ] = $mValue;
}
public function __get( $sProperty )
{
if ( $sProperty == "modules" )
return $this->_module;
else if ( array_key_exists( $sProperty, $this->_property ) )
return $this->_property[ $sProperty ];
else if ( array_key_exists( strToUpper( $sProperty ), $this->_module ) )
return $this->_module[ strToUpper( $sProperty ) ];
else if ( $this->checkModuleAvailability( $sProperty ) )
return $this->get( $sProperty );
return null;
}
public function __call( $sMethod, $aArgument )
{
$sSelf = get_class( $this );
$sTopAuthored = $this->getTopAuthoredClass();
$sMessage = "Call to unknown method '{$sSelf}::{$sMethod}'" . ( $sTopAuthored != $sSelf ? ", nearest authored class is '{$sTopAuthored}'" : "" );
$this->call( "/Log/write", $sMessage, 0 );
throw new Exception( $sMessage );
return false;
}
/**
* Allow modules to be called as 'call' methods
* @name __invoke
* @type method
* @access public
* @param mixed arg N
* @return mixed
* @syntax Konsolidate( [ mixed arg N ] );
* @note __invoke only works in PHP 5.3+
* @note You can now effectively leave out the '->call' part when calling on methods, e.g. $oK( "/DB/query", "SHOW TABLES" ) instead of $oK->call( "/DB/query", "SHOW TABLES" );
* @see call
*/
public function __invoke()
{
return call_user_func_array(
Array(
$this, // the object
"call" // the method
),
func_get_args() // the arguments
);
}
/**
* Allow isset/empty tests on inaccessible properties
* @name __isset
* @type method
* @access public
* @param string property
* @return bool isset
* @syntax isset(Konsolidate->property), empty(Konsolidate->property);
* @note __isset only works in PHP 5.1+
*/
public function __isset( $sProperty )
{
return isset($this->_property[ $sProperty ]);
}
/**
* Allow unsetting of inaccessible properties
* @name __unset
* @type method
* @access public
* @param string property
* @syntax unset(Konsolidate->property);
* @note __unset only works in PHP 5.1+
*/
public function __unset( $sProperty )
{
unset($this->_property[ $sProperty ]);
}
/**
* Create a string representing the Konsolidate instance
* @name __toString
* @type method
* @access public
* @syntax print Konsolidate();
*/
public function __toString()
{
$sReturn = "<div style='font-family:\"Lucida Grande\", Verdana, Arial, sans-serif;font-size:11px;color'>";
$sReturn .= "<h3 style='margin:0;padding:0;'>" . get_class( $this ) . "</h3>\n";
if ( count( $this->_property ) )
{
$sReturn .= "<div style='color:#400;'>\n";
$sReturn .= "<em>Custom properties</em>\n";
$sReturn .= "<ul>";
foreach( $this->_property as $sKey=>$mValue )
if ( is_object( $mValue ) )
$sReturn .= " <li>{$sKey}\t= (object " . get_class( $mValue ) . ")</li>\n";
else if ( is_array( $mValue ) )
$sReturn .= " <li>{$sKey}\t= (array)</li>\n";
else if ( is_bool( $mValue ) )
$sReturn .= " <li>{$sKey}\t= (bool) " . ( $mValue ? "true" : "false" ) . "</li>\n";
else
$sReturn .= " <li>{$sKey}\t= (" . gettype( $mValue ) . ") {$mValue}</li>\n";
$sReturn .= "</ul>";
$sReturn .= "</div>";
}
if ( count( $this->_module ) )
{
$sReturn .= "<strong>Modules</strong>\n";
$sReturn .= "<ul>";
foreach( $this->_module as $sKey=>$mValue )
$sReturn .= " <li style='list-style-type:square;'>{$sKey}\n<br />{$mValue}</li>";
$sReturn .= "</ul>";
}
$sReturn .= "</div>";
return $sReturn;
}
/**
* Look ahead at all available submodules and cache the availability
* @name _indexModuleAvailability
* @type method
* @access protected
* @returns void
* @syntax Konsolidate->_indexModuleAvailability();
*/
protected function _indexModuleAvailability()
{
if (!is_array(self::$_modulecheck))
self::$_modulecheck = Array();
$class = get_class($this);
if (!isset(self::$_modulecheck[$class]))
{
$list = Array();
if (is_array($this->_path))
foreach ($this->_path as $tier=>$path)
foreach (glob($path . '/*') as $item)
$list[strtolower(basename($item, '.class.php'))] = true;
self::$_modulecheck[$class] = $list;
}
}
}
?>