gluon-web-remote

Web remote Administration for big size of gluon routers
git clone git://archive.git.mtrnord.blog/MTRNord/gluon-web-remote.git
Log | Files | Refs | README

ThrowPromise.php (2617B)


      1 <?php
      2 
      3 /*
      4  * This file is part of the Prophecy.
      5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
      6  *     Marcello Duarte <marcello.duarte@gmail.com>
      7  *
      8  * For the full copyright and license information, please view the LICENSE
      9  * file that was distributed with this source code.
     10  */
     11 
     12 namespace Prophecy\Promise;
     13 
     14 use Doctrine\Instantiator\Instantiator;
     15 use Prophecy\Prophecy\ObjectProphecy;
     16 use Prophecy\Prophecy\MethodProphecy;
     17 use Prophecy\Exception\InvalidArgumentException;
     18 use ReflectionClass;
     19 
     20 /**
     21  * Throw promise.
     22  *
     23  * @author Konstantin Kudryashov <ever.zet@gmail.com>
     24  */
     25 class ThrowPromise implements PromiseInterface
     26 {
     27     private $exception;
     28 
     29     /**
     30      * @var \Doctrine\Instantiator\Instantiator
     31      */
     32     private $instantiator;
     33 
     34     /**
     35      * Initializes promise.
     36      *
     37      * @param string|\Exception $exception Exception class name or instance
     38      *
     39      * @throws \Prophecy\Exception\InvalidArgumentException
     40      */
     41     public function __construct($exception)
     42     {
     43         if (is_string($exception)) {
     44             if (!class_exists($exception)
     45              && 'Exception' !== $exception
     46              && !is_subclass_of($exception, 'Exception')) {
     47                 throw new InvalidArgumentException(sprintf(
     48                     'Exception class or instance expected as argument to ThrowPromise, but got %s.',
     49                     gettype($exception)
     50                 ));
     51             }
     52         } elseif (!$exception instanceof \Exception) {
     53             throw new InvalidArgumentException(sprintf(
     54                 'Exception class or instance expected as argument to ThrowPromise, but got %s.',
     55                 gettype($exception)
     56             ));
     57         }
     58 
     59         $this->exception = $exception;
     60     }
     61 
     62     /**
     63      * Throws predefined exception.
     64      *
     65      * @param array          $args
     66      * @param ObjectProphecy $object
     67      * @param MethodProphecy $method
     68      *
     69      * @throws object
     70      */
     71     public function execute(array $args, ObjectProphecy $object, MethodProphecy $method)
     72     {
     73         if (is_string($this->exception)) {
     74             $classname   = $this->exception;
     75             $reflection  = new ReflectionClass($classname);
     76             $constructor = $reflection->getConstructor();
     77 
     78             if ($constructor->isPublic() && 0 == $constructor->getNumberOfRequiredParameters()) {
     79                 throw $reflection->newInstance();
     80             }
     81 
     82             if (!$this->instantiator) {
     83                 $this->instantiator = new Instantiator();
     84             }
     85 
     86             throw $this->instantiator->instantiate($classname);
     87         }
     88 
     89         throw $this->exception;
     90     }
     91 }