Esempio n. 1
0
 /**
  * Starts monitoring.
  * @param  string class/interface type
  * @return void
  */
 public function monitor($type)
 {
     fixNamespace($type);
     $this->monitors[$type] = NULL;
     $this->lookup($type, FALSE);
     // call attached()
 }
Esempio n. 2
0
 /**
  * Iterates over a components.
  * @param  bool    recursive?
  * @param  string  class types filter
  * @return \ArrayIterator
  */
 public final function getComponents($deep = FALSE, $filterType = NULL)
 {
     $iterator = new RecursiveComponentIterator($this->components);
     if ($deep) {
         $deep = $deep > 0 ? RecursiveIteratorIterator::SELF_FIRST : RecursiveIteratorIterator::CHILD_FIRST;
         $iterator = new RecursiveIteratorIterator($iterator, $deep);
     }
     if ($filterType) {
         /**/
         fixNamespace($filterType);
         /**/
         $iterator = new InstanceFilterIterator($iterator, $filterType);
     }
     return $iterator;
 }
Esempio n. 3
0
 /**
  * Returns mailer.
  * @return IMailer
  */
 public function getMailer()
 {
     if ($this->mailer === NULL) {
         fixNamespace(self::$defaultMailer);
         $this->mailer = is_object(self::$defaultMailer) ? self::$defaultMailer : new self::$defaultMailer();
     }
     return $this->mailer;
 }
Esempio n. 4
0
 /**
  * Sends e-mail.
  * @param  IMailer
  * @return void
  */
 public function send(IMailer $mailer = NULL)
 {
     if ($mailer === NULL) {
         fixNamespace(self::$defaultMailer);
         $mailer = is_object(self::$defaultMailer) ? self::$defaultMailer : new self::$defaultMailer();
     }
     $mailer->send($this->build());
 }
Esempio n. 5
0
 /**
  * Gets the service object of the specified type.
  * @param  string service name
  * @param  array  options in case service is not singleton
  * @return mixed
  */
 public function getService($name, array $options = NULL)
 {
     if (!is_string($name) || $name === '') {
         throw new InvalidArgumentException("Service name must be a non-empty string, " . gettype($name) . " given.");
     }
     $lower = strtolower($name);
     if (isset($this->registry[$lower])) {
         // instantiated singleton
         if ($options) {
             throw new InvalidArgumentException("Service named '{$name}' is singleton and therefore can not have options.");
         }
         return $this->registry[$lower];
     } elseif (isset($this->factories[$lower])) {
         list($factory, $singleton, $defOptions) = $this->factories[$lower];
         if ($singleton && $options) {
             throw new InvalidArgumentException("Service named '{$name}' is singleton and therefore can not have options.");
         } elseif ($defOptions) {
             $options = $options ? $options + $defOptions : $defOptions;
         }
         if (is_string($factory) && strpos($factory, ':') === FALSE) {
             // class name
             /**/
             fixNamespace($factory);
             /**/
             if (!class_exists($factory)) {
                 throw new AmbiguousServiceException("Cannot instantiate service '{$name}', class '{$factory}' not found.");
             }
             $service = new $factory();
             if ($options && method_exists($service, 'setOptions')) {
                 $service->setOptions($options);
                 // TODO: better!
             }
         } else {
             // factory callback
             /**/
             fixCallback($factory);
             /**/
             if (!is_callable($factory)) {
                 $able = is_callable($factory, TRUE, $textual);
                 throw new AmbiguousServiceException("Cannot instantiate service '{$name}', handler '{$textual}' is not " . ($able ? 'callable.' : 'valid PHP callback.'));
             }
             $service = call_user_func($factory, $options);
             if (!is_object($service)) {
                 $able = is_callable($factory, TRUE, $textual);
                 throw new AmbiguousServiceException("Cannot instantiate service '{$name}', value returned by '{$textual}' is not object.");
             }
         }
         if ($singleton) {
             $this->registry[$lower] = $service;
             unset($this->factories[$lower]);
         }
         return $service;
     }
     if ($this->parent !== NULL) {
         return $this->parent->getService($name, $options);
     } else {
         throw new InvalidStateException("Service '{$name}' not found.");
     }
 }
Esempio n. 6
0
 /**
  * Gets the service object of the specified type.
  * @param  string service name
  * @param  bool   throw exception if service doesn't exist?
  * @return mixed
  */
 public function getService($name, $need = TRUE)
 {
     if (!is_string($name) || $name === '') {
         throw new InvalidArgumentException("Service name must be a non-empty string, " . gettype($name) . " given.");
     }
     $lower = strtolower($name);
     if (isset($this->registry[$lower])) {
         return $this->registry[$lower];
     } elseif (isset($this->factories[$lower])) {
         $service = $this->factories[$lower];
         if (is_string($service)) {
             if (substr($service, -2) === '()') {
                 // trick to pass callback as string
                 $service = substr($service, 0, -2);
             } else {
                 fixNamespace($service);
                 if (!class_exists($service)) {
                     throw new AmbiguousServiceException("Cannot instantiate service '{$name}', class '{$service}' not found.");
                 }
                 return $this->registry[$lower] = new $service();
             }
         }
         fixCallback($service);
         if (!is_callable($service)) {
             $able = is_callable($service, TRUE, $textual);
             throw new AmbiguousServiceException("Cannot instantiate service '{$name}', handler '{$textual}' is not " . ($able ? 'callable.' : 'valid PHP callback.'));
         }
         return $this->registry[$lower] = call_user_func($service);
     }
     if ($this->parent !== NULL) {
         return $this->parent->getService($name, $need);
     } elseif ($need) {
         throw new InvalidStateException("Service '{$name}' not found.");
     } else {
         return NULL;
     }
 }
Esempio n. 7
0
 /**
  * Stops monitoring.
  * @param  string class/interface type
  * @return void
  */
 public function unmonitor($type)
 {
     fixNamespace($type);
     unset($this->monitors[$type]);
 }