WorkerClass.php 5.4 KB

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