/**
  * @param string|null $event_name
  * @return $this
  */
 public function triggerEvent($event_name = null)
 {
     if (is_null($event_name)) {
         $event_name = CodeHelper::getPropertyName(get_class($this));
     }
     $event = new Event($this, $event_name);
     foreach ($this->_observers_storage as $observer) {
         if ($event->isPropagationStopped()) {
             break;
         }
         /* @var EventObserverProxy $observer */
         $observer->handleEvent($event);
     }
     return $this;
 }
 /**
  * Construct a service based on its `constructor` item and references it
  *
  * @param   string  $name
  * @param   array   $arguments
  * @return  void
  * @throws  ErrorException
  */
 protected function _constructService($name, array $arguments = array())
 {
     if ($this->hasProvider($name)) {
         $data = $this->getProvider($name);
         if (is_object($data) && CodeHelper::implementsInterface($data, 'ServiceContainer\\ServiceProviderInterface')) {
             $data->boot($this);
             $this->setService($name, $data);
         } elseif (is_callable($data) || $data instanceof \Closure) {
             try {
                 $item = call_user_func_array($data, array($this, $name, $arguments));
                 $this->setService($name, $item);
             } catch (\Exception $e) {
                 throw new ErrorException(sprintf('An error occurred while trying to create a "%s" service!', $name), 0, 1, __FILE__, __LINE__, $e);
             }
         } elseif (is_array($data)) {
             $this->setService($name, $data[1], isset($data[2]) ? $data[2] : false);
         } else {
             throw new ErrorException(sprintf('A "%s" service constructor must be a valid callback!', $name));
         }
     }
 }
Пример #3
0
 /**
  * Test if a classes names set is in a set of namespaces
  *
  * @param string|array $names
  * @param array $namespaces
  * @param array $logs Passed by reference
  * @return string|bool
  */
 protected function _classesInNamespaces($names, array $namespaces, array &$logs = array())
 {
     if (!is_array($names)) {
         $names = array($names);
     }
     foreach ($names as $_name) {
         foreach ($namespaces as $_namespace) {
             if (CodeHelper::namespaceExists($_namespace)) {
                 $tmp_namespace = rtrim(TextHelper::toCamelCase($_namespace), '\\') . '\\';
                 if (substr_count(TextHelper::toCamelCase($_name), $tmp_namespace) > 0) {
                     return $_name;
                 }
             } else {
                 $logs[] = $this->_getErrorMessage('Namespace "%s" not found!', $_namespace);
             }
         }
     }
     return false;
 }
Пример #4
0
 /**
  * Magic unsetter
  *
  * Magic method called when `unsetProp()` or `unset($this->prop)` are invoked.
  *
  * @see <http://www.php.net/manual/en/language.oop5.overloading.php>
  * @see Patterns\Commons\Registry::_invokeUnset()
  *
  * @param string $name The name of the property to get
  * @return self Returns `$this` for method chaining
  */
 public function __unset($name)
 {
     $this->_data->setEntry(CodeHelper::getPropertyName($name), null);
     return $this;
 }
Пример #5
0
 /**
  * Call a callback fetching organized arguments depending on its declaration
  * @param null $method_name
  * @param null $arguments
  * @param null $class_name
  * @return mixed
  */
 public static function fetchArguments($method_name = null, $arguments = null, $class_name = null)
 {
     return CodeHelper::fetchArguments($method_name, $arguments, $class_name);
 }
Пример #6
0
 /**
  * Magic handler when calling a non-eixsting method statically on an object
  *
  * Magic static handling of `getProp(default)`, `setProp(value)`, `unsetProp()`, `issetProp()` or `resetProp()`.
  *
  * @see <http://www.php.net/manual/en/language.oop5.overloading.php>
  *
  * @param string $name The non-existing method name called on the object
  * @param array $arguments The arguments array passed calling the method
  * @return mixed This will return the result of a magic method, or nothing if nothing can be done
  */
 public static function __callStatic($name, array $arguments)
 {
     $return = null;
     // unset, isset, reset
     if (in_array(substr($name, 0, 5), array('isset', 'reset', 'unset'))) {
         $property = CodeHelper::getPropertyName(substr($name, 5));
     }
     // get, set
     if (in_array(substr($name, 0, 3), array('set', 'get'))) {
         $property = CodeHelper::getPropertyName(substr($name, 3));
     }
     if (!empty($property) && self::__isInvokableStatic($property)) {
         $classname = get_called_class();
         try {
             $object = new $classname();
             $return = call_user_func_array(array($object, '__call'), array($name, $arguments));
         } catch (\Exception $e) {
         }
     }
     return $return;
 }