123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace Mmoreramerino\GearmanBundle\Module;
- use Doctrine\Common\Annotations\AnnotationReader;
- use Mmoreramerino\GearmanBundle\Driver\Gearman\Work;
- use Mmoreramerino\GearmanBundle\Module\JobCollection;
- use Mmoreramerino\GearmanBundle\Module\JobClass as Job;
- /**
- * Worker class
- *
- * @author Marc Morera <marc@ulabox.com>
- */
- class WorkerClass
- {
- /**
- * All jobs inside Worker
- *
- * @var JobCollection
- */
- private $jobCollection;
- /**
- * Callable name for this job.
- * If is setted on annotations, this value will be used.
- * Otherwise, natural method name will be used.
- *
- * @var string
- */
- private $callableName;
- /**
- * Namespace of Work class
- *
- * @var string
- */
- private $namespace;
- /**
- * Retrieves all jobs available from worker
- *
- * @param Work $classAnnotation ClassAnnotation class
- * @param \ReflectionClass $reflectionClass Reflexion class
- * @param AnnotationReader $reader ReaderAnnotation class
- * @param array $settings Settings array
- */
- public function __construct( Work $classAnnotation, \ReflectionClass $reflectionClass, AnnotationReader $reader, array $settings)
- {
- $this->namespace = $reflectionClass->getNamespaceName();
- $this->callableName = (null !== $classAnnotation->name) ?
- $classAnnotation->name :
- $this->namespace;
- $this->description = (null !== $classAnnotation->description) ?
- $classAnnotation->description :
- 'No description is defined';
- $this->fileName = $reflectionClass->getFileName();
- $this->className = $reflectionClass->getName();
- $this->service = $classAnnotation->service;
- $this->jobCollection = new JobCollection;
- foreach ($reflectionClass->getMethods() as $method) {
- $reflMethod = new \ReflectionMethod($method->class, $method->name);
- $methodAnnotations = $reader->getMethodAnnotations($reflMethod);
- foreach ($methodAnnotations as $annot) {
- if ($annot instanceof \Mmoreramerino\GearmanBundle\Driver\Gearman\Job) {
- $this->jobCollection->add(new Job($annot, $reflMethod, $classAnnotation, $this->callableName, $settings));
- }
- }
- }
- }
- /**
- * Retrieve all Worker data in cache format
- *
- * @return array
- */
- public function __toCache()
- {
- $dump = array(
- 'namespace' => $this->namespace,
- 'className' => $this->className,
- 'fileName' => $this->fileName,
- 'callableName' => $this->callableName,
- 'description' => $this->description,
- 'service' => $this->service,
- 'jobs' => array(),
- );
- $dump['jobs'] = $this->jobCollection->__toCache();
- return $dump;
- }
- }
|