GearmanCacheWrapper.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * Gearman Bundle for Symfony2
  4. *
  5. * @author Marc Morera <yuhu@mmoreram.com>
  6. * @since 2013
  7. */
  8. namespace Mmoreram\GearmanBundle\Service;
  9. use Doctrine\Common\Annotations\Reader;
  10. use Doctrine\Common\Annotations\AnnotationRegistry;
  11. use Symfony\Component\HttpKernel\Kernel;
  12. use Doctrine\Common\Cache\Cache;
  13. use Symfony\Component\Finder\Finder;
  14. use Doctrine\Common\Annotations\SimpleAnnotationReader;
  15. use Mmoreram\GearmanBundle\Module\WorkerCollection;
  16. use Mmoreram\GearmanBundle\Module\WorkerClass as Worker;
  17. use Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
  18. use ReflectionClass;
  19. /**
  20. * Gearman cache loader class
  21. *
  22. * @author Marc Morera <yuhu@mmoreram.com>
  23. */
  24. class GearmanCacheWrapper
  25. {
  26. /**
  27. * @var Array
  28. *
  29. * Bundles loaded by kernel
  30. */
  31. private $kernelBundles;
  32. /**
  33. * @var Kernel
  34. *
  35. * Kernel object
  36. */
  37. private $kernel;
  38. /**
  39. * @var Array
  40. *
  41. * Bundles available to perform search
  42. */
  43. private $bundles;
  44. /**
  45. * @var Array
  46. *
  47. * paths to search on
  48. */
  49. private $paths = array();
  50. /**
  51. * @var Array
  52. *
  53. * paths to ignore
  54. */
  55. private $excludedPaths = array();
  56. /**
  57. * @var GearmanCache
  58. *
  59. * Gearman Cache
  60. */
  61. private $cache;
  62. /**
  63. * @var string
  64. *
  65. * Gearman cache id
  66. */
  67. private $cacheId;
  68. /**
  69. * @var array
  70. *
  71. * WorkerCollection with all workers and jobs available
  72. */
  73. private $workerCollection;
  74. /**
  75. * @var array
  76. *
  77. * Collection of servers to connect
  78. */
  79. private $servers;
  80. /**
  81. * @var array
  82. *
  83. * Default settings defined by user in config.yml
  84. */
  85. private $defaultSettings;
  86. /**
  87. * Return workerCollection
  88. *
  89. * @return array all available workers
  90. */
  91. public function getWorkers()
  92. {
  93. return $this->workerCollection;
  94. }
  95. /**
  96. * Construct method
  97. *
  98. * @param Kernel $kernel Kernel instance
  99. * @param Cache $cache Cache
  100. * @param string $cacheId Cache id where to save parsing data
  101. * @param array $bundles Bundle array where to parse workers, defined on condiguration
  102. * @param array $servers Server list defined on configuration
  103. * @param array $defaultSettings Default settings defined on configuration
  104. */
  105. public function __construct(Kernel $kernel, Cache $cache, $cacheId, array $bundles, array $servers, array $defaultSettings)
  106. {
  107. $this->kernelBundles = $kernel->getBundles();
  108. $this->kernel = $kernel;
  109. $this->bundles = $bundles;
  110. $this->cache = $cache;
  111. $this->cacheId = $cacheId;
  112. $this->servers = $servers;
  113. $this->defaultSettings = $defaultSettings;
  114. }
  115. /**
  116. * loads Gearman cache, only if is not loaded yet
  117. *
  118. * @return GearmanCacheLoader self Object
  119. */
  120. public function load()
  121. {
  122. if ($this->cache->contains($this->cacheId)) {
  123. $this->workerCollection = $this->cache->fetch($this->cacheId);
  124. } else {
  125. $this->workerCollection = $this->parseNamespaceMap()->toArray();
  126. $this->cache->save($this->cacheId, $this->workerCollection);
  127. }
  128. return $this;
  129. }
  130. /**
  131. * flush all cache
  132. *
  133. * @return GearmanCacheLoader self Object
  134. */
  135. public function flush()
  136. {
  137. $this->cache->delete($this->cacheId);
  138. return $this;
  139. }
  140. /**
  141. * Return Gearman bundle settings, previously loaded by method load()
  142. * If settings are not loaded, a SettingsNotLoadedException Exception is thrown
  143. *
  144. * @return array Bundles that gearman will be able to search annotations
  145. */
  146. public function loadNamespaceMap()
  147. {
  148. /**
  149. * Iteratinc all bundle settings
  150. */
  151. foreach ($this->bundles as $bundleSettings) {
  152. if (!$bundleSettings['active']) {
  153. break;
  154. }
  155. $bundleNamespace = $bundleSettings['name'];
  156. $bundlePath = $this->kernelBundles[$bundleNamespace]->getPath();
  157. if (!empty($bundleSettings['include'])) {
  158. foreach ($bundleSettings['include'] as $include) {
  159. $this->paths[] = rtrim(rtrim($bundlePath, '/') . '/' . $include, '/') . '/';
  160. }
  161. } else {
  162. /**
  163. * If no include is set, include all namespace
  164. */
  165. $this->paths[] = rtrim($bundlePath, '/') . '/';
  166. }
  167. foreach ($bundleSettings['ignore'] as $ignore) {
  168. $this->excludedPaths[] = trim($ignore, '/');
  169. }
  170. }
  171. }
  172. /**
  173. * Perform a parsing inside all namespace map
  174. *
  175. * @return WorkerCollection collection of all info
  176. */
  177. private function parseNamespaceMap()
  178. {
  179. AnnotationRegistry::registerFile($this->kernel->locateResource("@GearmanBundle/Driver/Gearman/Work.php"));
  180. AnnotationRegistry::registerFile($this->kernel->locateResource("@GearmanBundle/Driver/Gearman/Job.php"));
  181. $reader = new SimpleAnnotationReader();
  182. $reader->addNamespace('Mmoreram\GearmanBundle\Driver');
  183. $finder = new Finder();
  184. $finder
  185. ->files()
  186. ->followLinks()
  187. ->exclude($this->excludedPaths)
  188. ->in($this->paths);
  189. return $this->parseFiles($finder, $reader);
  190. }
  191. /**
  192. * Load all workers with their jobs
  193. *
  194. * @param Finder $finder Finder
  195. * @param Reader $reader Reader
  196. *
  197. * @return WorkerCollection collection of all info
  198. */
  199. private function parseFiles(Finder $finder, Reader $reader)
  200. {
  201. $workerCollection = new WorkerCollection;
  202. /**
  203. * Every file found is parsed
  204. */
  205. foreach ($finder as $file) {
  206. /**
  207. * File is checked to be parsed. Is just parsed if is a php file
  208. * Otherwise, jump to next file
  209. */
  210. if ('php' !== strtolower($file->getExtension())) {
  211. continue;
  212. }
  213. /**
  214. * File is accepted to be parsed
  215. */
  216. $classNamespace = $this->getFileClassNamespace($file->getRealpath());
  217. $reflClass = new ReflectionClass($classNamespace);
  218. $classAnnotations = $reader->getClassAnnotations($reflClass);
  219. /**
  220. * Every annotation found is parsed
  221. */
  222. foreach ($classAnnotations as $annot) {
  223. /**
  224. * Annotation is only laoded if is typeof WorkAnnotation
  225. */
  226. if ($annot instanceof WorkAnnotation) {
  227. /**
  228. * Creates new Worker element with all its Job data
  229. */
  230. $worker = new Worker($annot, $reflClass, $reader, $this->servers, $this->defaultSettings);
  231. $workerCollection->add($worker);
  232. }
  233. }
  234. }
  235. return $workerCollection;
  236. }
  237. /**
  238. * Returns the full class name for the first class in the file.
  239. *
  240. * @param string $file A PHP file path
  241. *
  242. * @return string|false Full class name if found, false otherwise
  243. */
  244. protected function getFileClassNamespace($file)
  245. {
  246. $class = false;
  247. $namespace = false;
  248. $tokens = token_get_all(file_get_contents($file));
  249. for ($i = 0, $count = count($tokens); $i < $count; $i++) {
  250. $token = $tokens[$i];
  251. if (!is_array($token)) {
  252. continue;
  253. }
  254. if (true === $class && T_STRING === $token[0]) {
  255. return $namespace.'\\'.$token[1];
  256. }
  257. if (true === $namespace && T_STRING === $token[0]) {
  258. $namespace = '';
  259. do {
  260. $namespace .= $token[1];
  261. $token = $tokens[++$i];
  262. } while ($i < $count && is_array($token) && in_array($token[0], array(T_NS_SEPARATOR, T_STRING)));
  263. }
  264. if (T_CLASS === $token[0]) {
  265. $class = true;
  266. }
  267. if (T_NAMESPACE === $token[0]) {
  268. $namespace = true;
  269. }
  270. }
  271. return false;
  272. }
  273. }