/** * @param DocumentManager $dm * @param string $documentName * @param SpecificationVisitorFactoryInterface $specificationVisitorFactory * @param ComparatorVisitorFactoryInterface $comparatorVisitorFactory */ public function __construct(DocumentManager $dm, $documentName = null, SpecificationVisitorFactoryInterface $specificationVisitorFactory = null, ComparatorVisitorFactoryInterface $comparatorVisitorFactory = null) { parent::__construct($dm, $documentName); if ($specificationVisitorFactory === null) { $specificationVisitorFactory = new SpecificationVisitorFactory(); } if ($comparatorVisitorFactory === null) { $comparatorVisitorFactory = new ComparatorVisitorFactory(); } $this->specificationVisitorFactory = $specificationVisitorFactory; $this->comparatorVisitorFactory = $comparatorVisitorFactory; $this->factory = Delegate::fromClosure(function () use($dm, $documentName) { return new static($dm, $documentName); }); }
/** * Execute the given message... after other running messages are complete. * * @param mixed $message * @param callable $next * * @throws \Exception */ public function handle($message, callable $next) { $this->queue[] = Delegate::fromClosure(function () use($message, $next) { return $next($message); }); if ($this->isRunning) { return; } $this->isRunning = true; try { $this->runQueuedJobs(); } catch (\Exception $e) { $this->isRunning = false; $this->queue = []; throw $e; } $this->isRunning = false; }
/** * @return Delegate */ private function chainedExecution() { $middlewares = []; foreach ($this->middlewares as $priority => $collection) { foreach ($collection as $middleware) { $middlewares[] = $middleware; } } $next = Delegate::fromClosure(function ($message) { // the final middleware return the same message return $message; }); // reverse iteration over middlewares /** @var MiddlewareInterface $middleware */ while ($middleware = array_pop($middlewares)) { $next = Delegate::fromClosure(function ($message) use($middleware, $next) { return $middleware->handle($message, $next); }); } return $next; }
/** * Test reflection method. */ public function testReflection() { $this->given($closure = function ($value = null) { return $value; })->when($reflection = Delegate::fromClosure($closure)->reflection())->then()->object($reflection)->isEqualTo(new \ReflectionFunction($closure)); $this->when($reflection = Delegate::fromMethod($this, 'sampleMethod')->reflection())->then()->object($reflection)->isEqualTo(new \ReflectionMethod($this, 'sampleMethod')); $this->given($foo = new FooCallable())->when($reflection = (new Delegate($foo))->reflection())->then()->object($reflection)->isEqualTo(new \ReflectionMethod($foo, '__invoke')); $this->when($reflection = Delegate::fromStaticMethod(self::class, 'sampleStaticMethod')->reflection())->then()->object($reflection)->isEqualTo(new \ReflectionMethod(self::class . '::sampleStaticMethod')); }