WorkerClass.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Mmoreramerino\GearmanBundle\Module;
  3. use Doctrine\Common\Annotations\AnnotationReader;
  4. use Mmoreramerino\GearmanBundle\Driver\Gearman\Work;
  5. use Mmoreramerino\GearmanBundle\Module\JobCollection;
  6. use Mmoreramerino\GearmanBundle\Module\JobClass as Job;
  7. /**
  8. * Worker class
  9. *
  10. * @author Marc Morera <marc@ulabox.com>
  11. */
  12. class WorkerClass
  13. {
  14. /**
  15. * All jobs inside Worker
  16. *
  17. * @var JobCollection
  18. */
  19. private $jobCollection;
  20. /**
  21. * Callable name for this job.
  22. * If is setted on annotations, this value will be used.
  23. * Otherwise, natural method name will be used.
  24. *
  25. * @var string
  26. */
  27. private $callableName;
  28. /**
  29. * Namespace of Work class
  30. *
  31. * @var string
  32. */
  33. private $namespace;
  34. /**
  35. * Retrieves all jobs available from worker
  36. *
  37. * @param Work $classAnnotation ClassAnnotation class
  38. * @param \ReflectionClass $reflectionClass Reflexion class
  39. * @param AnnotationReader $reader ReaderAnnotation class
  40. * @param array $settings Settings array
  41. */
  42. public function __construct( Work $classAnnotation, \ReflectionClass $reflectionClass, AnnotationReader $reader, array $settings)
  43. {
  44. $this->namespace = $reflectionClass->getNamespaceName();
  45. $this->callableName = (null !== $classAnnotation->name) ?
  46. $classAnnotation->name :
  47. $this->namespace;
  48. $this->description = (null !== $classAnnotation->description) ?
  49. $classAnnotation->description :
  50. 'No description is defined';
  51. $this->fileName = $reflectionClass->getFileName();
  52. $this->className = $reflectionClass->getName();
  53. $this->service = $classAnnotation->service;
  54. $this->jobCollection = new JobCollection;
  55. foreach ($reflectionClass->getMethods() as $method) {
  56. $reflMethod = new \ReflectionMethod($method->class, $method->name);
  57. $methodAnnotations = $reader->getMethodAnnotations($reflMethod);
  58. foreach ($methodAnnotations as $annot) {
  59. if ($annot instanceof \Mmoreramerino\GearmanBundle\Driver\Gearman\Job) {
  60. $this->jobCollection->add(new Job($annot, $reflMethod, $classAnnotation, $this->callableName, $settings));
  61. }
  62. }
  63. }
  64. }
  65. /**
  66. * Retrieve all Worker data in cache format
  67. *
  68. * @return array
  69. */
  70. public function __toCache()
  71. {
  72. $dump = array(
  73. 'namespace' => $this->namespace,
  74. 'className' => $this->className,
  75. 'fileName' => $this->fileName,
  76. 'callableName' => $this->callableName,
  77. 'description' => $this->description,
  78. 'service' => $this->service,
  79. 'jobs' => array(),
  80. );
  81. $dump['jobs'] = $this->jobCollection->__toCache();
  82. return $dump;
  83. }
  84. }