WorkerClass.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Gearman Bundle for Symfony2
  4. *
  5. * @author Marc Morera <yuhu@mmoreram.com>
  6. * @since 2013
  7. */
  8. namespace Mmoreram\GearmanBundle\Module;
  9. use Doctrine\Common\Annotations\Reader;
  10. use Mmoreram\GearmanBundle\Driver\Gearman\Work;
  11. use Mmoreram\GearmanBundle\Module\JobCollection;
  12. use Mmoreram\GearmanBundle\Module\JobClass as Job;
  13. use Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
  14. use ReflectionClass;
  15. use ReflectionMethod;
  16. /**
  17. * Worker class
  18. *
  19. * This class provide all worker definition.
  20. */
  21. class WorkerClass
  22. {
  23. /**
  24. * @var string
  25. *
  26. * Namespace of worker class
  27. */
  28. private $namespace;
  29. /**
  30. * @var string
  31. *
  32. * Class name of worker
  33. */
  34. private $className;
  35. /**
  36. * @var string
  37. *
  38. * Filename of worker
  39. */
  40. private $fileName;
  41. /**
  42. * @var string
  43. *
  44. * Callable name for this job.
  45. * If is setted on annotations, this value will be used.
  46. * Otherwise, natural method name will be used.
  47. */
  48. private $callableName;
  49. /**
  50. * @var string
  51. *
  52. * Service alias if this worker is wanted to be built by dependency injection
  53. */
  54. private $service;
  55. /**
  56. * @var string
  57. *
  58. * Description of Job
  59. */
  60. private $description;
  61. /**
  62. * @var integer
  63. *
  64. * Number of iterations this job will be alive before die
  65. */
  66. private $iterations;
  67. /**
  68. * @var string
  69. *
  70. * Default method this job will be call into Gearman client
  71. */
  72. private $defaultMethod;
  73. /**
  74. * @var array
  75. *
  76. * Collection of servers to connect
  77. */
  78. private $servers;
  79. /**
  80. * @var JobCollection
  81. *
  82. * All jobs inside Worker
  83. */
  84. private $jobCollection;
  85. /**
  86. * Retrieves all jobs available from worker
  87. *
  88. * @param Work $classAnnotation ClassAnnotation class
  89. * @param ReflectionClass $reflectionClass Reflexion class
  90. * @param Reader $reader ReaderAnnotation class
  91. * @param array $settings Settings array
  92. */
  93. public function __construct(Work $classAnnotation, ReflectionClass $reflectionClass, Reader $reader, array $servers, array $defaultSettings)
  94. {
  95. $this->namespace = $reflectionClass->getNamespaceName();
  96. /**
  97. * Setting worker callable name
  98. */
  99. $this->callableName = is_null($classAnnotation->name)
  100. ? $reflectionClass->getName()
  101. : $this->namespace .'\\' .$classAnnotation->name;
  102. $this->callableName = str_replace('\\', '', $this->callableName);
  103. /**
  104. * Setting worker description
  105. */
  106. $this->description = is_null($classAnnotation->description)
  107. ? 'No description is defined'
  108. : $classAnnotation->description;
  109. $this->fileName = $reflectionClass->getFileName();
  110. $this->className = $reflectionClass->getName();
  111. $this->service = $classAnnotation->service;
  112. $this->iterations = is_null($classAnnotation->iterations)
  113. ? (int) $defaultSettings['iterations']
  114. : $classAnnotation->iterations;
  115. $defaultSettings['iterations'] = $this->iterations;
  116. $this->defaultMethod = is_null($classAnnotation->defaultMethod)
  117. ? $defaultSettings['method']
  118. : $classAnnotation->defaultMethod;
  119. $defaultSettings['method'] = $this->defaultMethod;
  120. /**
  121. * By default, this worker takes default servers definition
  122. */
  123. $this->servers = $servers;
  124. /**
  125. * If is configured some servers definition in the worker, overwrites
  126. */
  127. if ($classAnnotation->servers) {
  128. if (is_array($classAnnotation->servers)) {
  129. $this->servers = $classAnnotation->servers;
  130. } else {
  131. $this->servers = array($classAnnotation->servers);
  132. }
  133. }
  134. $this->jobCollection = new JobCollection;
  135. foreach ($reflectionClass->getMethods() as $method) {
  136. $reflMethod = new ReflectionMethod($method->class, $method->name);
  137. $methodAnnotations = $reader->getMethodAnnotations($reflMethod);
  138. foreach ($methodAnnotations as $annot) {
  139. if ($annot instanceof JobAnnotation) {
  140. $job = new Job($annot, $reflMethod, $this->callableName, $this->servers, $defaultSettings);
  141. $this->jobCollection->add($job);
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Retrieve all Worker data in cache format
  148. *
  149. * @return array
  150. */
  151. public function toArray()
  152. {
  153. return array(
  154. 'namespace' => $this->namespace,
  155. 'className' => $this->className,
  156. 'fileName' => $this->fileName,
  157. 'callableName' => $this->callableName,
  158. 'description' => $this->description,
  159. 'service' => $this->service,
  160. 'servers' => $this->servers,
  161. 'iterations' => $this->iterations,
  162. 'jobs' => $this->jobCollection->toArray(),
  163. );
  164. }
  165. }