Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- NAMESPACE ILLI\Core;
- USE ILLI\Core\Object\tAdapter;
- USE ILLI\Core\Object\tFilter;
- USE ILLI\Core\Object\tObserver;
- USE ILLI\Core\Object\tSignal;
- USE ILLI\Core\Object\tClass;
- USE ILLI\Core\Object\tTo;
- USE ILLI\Core\Object\tMethod;
- CLASS Object
- {
- USE tTo;
- USE tClass;
- USE tAdapter;
- USE tFilter;
- USE tObserver;
- USE tSignal;
- USE tMethod;
- protected static $__parentClasses = [];
- /**
- * allowed/expected fields: [optionName1 => flag, optionName2, ...]
- */
- protected $__autoConfig = [];
- /**
- * runtime options: init + defaults
- */
- protected $__runtimeConfig = [];
- /**
- * default options
- */
- protected $__defaultConfig = [];
- /**
- * constructor initial config
- */
- protected $__initConfig = [];
- /**
- * Lib::load() -> ::__init()
- *
- * __construct
- * [
- * __run?:true
- * -> __configure()
- * -> __install()
- * -> __run()
- * -> tSignal __construct runtimeConfig PRE_EVENT
- * -> __main()
- * -> tFilter __construct runtimeConfig *
- * -> tObserver __construct runtimeConfig
- * -> tSignal __construct runtimeConfig POST_EVENT
- * ]
- */
- public function __construct(array $__options = [])
- {
- $defaults =
- [
- '__run' => TRUE, // excecute setup()
- '__main' => '__main', // onConstructEvent
- '__delegate' => NULL, // class-delegate: adapter for every method; instance or className
- '__adapter' => // trigger__FUNCTION__ => hook | trigger__FUNCTION__ => [event => hook]
- [
- __FUNCTION__ => [] // 0 => class|instance, 1 => method | 0 => closure; overwrites methods from __delegate
- ],
- '__filter' => // trigger__FUNCTION__ => hook
- [
- __FUNCTION__ => [] // [filter 1, filter 2, filter n ...] | [eventName => [filter 1, filter 2, filter n]]; default event is '*'
- ],
- '__method' =>
- [
- // name => closure; public virtual methods, called by __call
- ],
- '__observer' =>
- [
- __FUNCTION__ => [] // event => [notify 1, notify 2, ...]
- ],
- '__signal' =>
- [
- __FUNCTION__ => // trigger__FUNCTION__ => [event => hook]
- [
- 'PRE_EVENT' => [], // event => [signal 1, signal 2, ...]
- 'POST_EVENT' => [] // event => [signal 1, signal 2, ...]
- ],
- ],
- '__class' => [], // class alias hook: new __class['Alias']() instead of new hardCodedClassName()
- '__to' => // cast to: type => handler
- [
- //'string' => 'toString',
- //'array' => 'toArray'
- ]
- ];
- $this->__initConfig = $this->extend(TRUE, $__options, $defaults);
- if(TRUE === $this->__initConfig['__run'])
- $this->__run();
- return $this;
- }
- public function extend()
- {
- $args = func_get_args();
- $tar = $args[0];
- $i = 1;
- $length = count($args);
- $rec = false;
- if($tar === true || $tar === false)
- {
- $rec = $tar;
- $tar = isset($args[1]) ? $args[1] : [];
- $i = 2;
- }
- if(!is_array($tar) && !is_object($tar))
- $tar = [];
- if($length === $i)
- {
- if(FALSE === ($this instanceof \Iterator))
- throw new \Exception('Unable to iterate throw '.get_called_class());
- $tar = $this;
- --$i;
- }
- for( ; $i < $length; $i++)
- {
- if(NULL === ($opt = $args[$i]))
- continue;
- foreach($opt as $key => $value)
- {
- if(is_array($value) && isset($tar[$key]) && is_array($tar[$key]))
- {
- $tar[$key] = $this->extend($tar[$key], $value);
- continue;
- }
- if(is_object($value))
- {
- //$tar[$key] = TRUE === $rec ? clone $value : $value;
- $tar[$key] = $value;
- continue;
- }
- $value = $opt[$key];
- $tar[$key] = $value;
- }
- }
- return $tar;
- }
- public function __clone()
- {
- if($this instanceOf iLink)
- return $this->link();
- if($this instanceOf iCopy)
- return $this->copy();
- throw new \Exception('Not supported');
- }
- /**
- * referenced runtime to create new object from called-class
- */
- public function link()
- {
- $class = get_called_class();
- return $class::__construct($this->__runtimeConfig);
- }
- /**
- * dereference runtime and create new object from called-class
- */
- public function copy()
- {
- $class = get_called_class();
- $config = $this->__runtimeConfig; // config is a reference!
- unset($this->__runtimeConfig); // unlink
- $this->__runtimeConfig = $config; // re-reference original
- return new $class($config); // build
- }
- /**
- * fileOnLoad event
- *
- * @see Lib::load()
- */
- public static function __init()
- {
- }
- /**
- * construct-event
- */
- protected function __main()
- {
- }
- protected function __run()
- {
- $this->__install();
- $this->Core_Object_tSignal_emit(get_called_class(), '__construct', 'PRE_EVENT', $this->__runtimeConfig);
- $this->{$this->__initConfig['__main']}();
- $filtered = $this->Core_Object_tFilter_emit(get_called_class(), '__construct', [$this->__runtimeConfig])[0];
- $this->Core_Object_tObserver_emit('__construct', $filtered);
- $this->Core_Object_tSignal_emit(get_called_class(), '__construct', 'POST_EVENT', $this->__runtimeConfig);
- return $this;
- }
- protected function __install()
- {
- $this->__configure();
- $class = get_called_class();
- $this->Core_Object_tClass_install ($this->__initConfig['__class']);
- $this->Core_Object_tMethod_install ($this->__initConfig['__method']);
- $this->Core_Object_tTo_install ($this->__initConfig['__to']);
- $this->Core_Object_tAdapter_install ($class, $this->__initConfig['__delegate']);
- $this->Core_Object_tAdapter_install ($class, $this->__initConfig['__adapter']);
- $this->Core_Object_tObserver_install ($class, $this->__initConfig['__observer']);
- $this->Core_Object_tSignal_install ($class, $this->__initConfig['__signal']);
- $this->Core_Object_tFilter_install ($class, $this->__initConfig['__filter']);
- return $this;
- }
- protected function __configure()
- {
- if($this->__autoConfig === [])
- return $this;
- foreach($this->__autoConfig as $key => $flag)
- {
- // option not given. use default
- if(!isset($this->__initConfig[$key])
- && isset($this->__defaultConfig[$key]))
- {
- $this->__runtimeConfig[$key] = $this->__defaultConfig[$key];
- continue;
- }
- if(!isset($this->__initConfig[$flag])
- && isset($this->__defaultConfig[$flag]))
- {
- $this->__runtimeConfig[$flag] = $this->__defaultConfig[$flag];
- continue;
- }
- if($flag === 'merge') // init + default
- {
- if(isset($this->__defaultConfig[$key])) // if exists...
- {
- $this->__runtimeConfig[$key] = $this->__initConfig[$key] + $this->__defaultConfig[$key];
- }
- else
- {
- $this->__runtimeConfig[$key] = $this->__initConfig[$key];
- }
- }
- else // init[${key}]
- if(isset($this->__initConfig[$key]))
- {
- $this->__runtimeConfig[$flag] = $this->__initConfig[$flag];
- }
- else // init[${flag}]
- if(isset($this->__initConfig[$flag]))
- {
- $this->__runtimeConfig[$flag] = $this->__initConfig[$flag];
- }
- else // default[${key}]
- if(isset($this->__defaultConfig[$key]))
- {
- $this->__runtimeConfig[$key] = $this->__defaultConfig[$key];
- }
- else // default[${flag}]
- if(isset($this->__defaultConfig[$flag]))
- {
- $this->__runtimeConfig[$flag] = $this->__defaultConfig[$flag];
- }
- else
- {
- $this->__runtimeConfig[$flag] = NULL;
- }
- }
- foreach($this->__runtimeConfig as $property => &$value)
- {
- $this->{$property} =& $value;
- unset($this->__initConfig[$property]);
- }
- return $this;
- }
- public function __call($__name, array $__parameters = [])
- {
- return $this->Core_Object_tMethod_emit($__name, $__parameters);
- }
- public static function __set_state($data)
- {
- $this->__export = $data;
- $class = get_called_class();
- $object = new $class();
- foreach($object as $property => $value)
- $object->{$property} = $value;
- return $object;
- }
- public static function getParents($__className = NULL)
- {
- $class = NULL === $__className ? get_called_class() : $__className;
- if(!isset(self::$__parentClasses[$class]))
- self::$__parentClasses[$class] = class_parents($class);
- return self::$__parentClasses[$class];
- }
- public function to($__format, array $__options = [])
- {
- $__options +=
- [
- 'internal' => FALSE,
- 'handlers' => []
- ];
- return $this->Core_Object_tTo_emit($__format, [$__options]);
- }
- public function toString(array $__options = [])
- {
- return get_called_class();
- }
- public function toArray(array $__options = [])
- {
- $result = [];
- foreach($this as $property => $value)
- {
- if(substr($property, 0, 2) === '__')
- continue;
- switch(true):
- case is_array($value):
- case (!is_object($property)):
- $result[$property] = $value;
- break;
- case (isset($__options['handlers'][$class = get_class($value)])):
- $result[$property] = $__options['handlers'][$class]($value);
- break;
- case (method_exists($property, 'to')):
- $result[$property] = $value->to('array', $__options);
- break;
- case ($vars = get_object_vars($value)):
- $result[$property] = $vars;
- break;
- case (method_exists($value, '__toString')):
- $result[$property] = (string) $value;
- break;
- default:
- $result[$property] = $value;
- break;
- endswitch;
- }
- return $result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement