GearmanCacheWrapper.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. *
  143. * If settings are not loaded, a SettingsNotLoadedException Exception is thrown
  144. */
  145. public function loadNamespaceMap()
  146. {
  147. /**
  148. * Iteratinc all bundle settings
  149. */
  150. foreach ($this->bundles as $bundleSettings) {
  151. if (!$bundleSettings['active']) {
  152. break;
  153. }
  154. $bundleNamespace = $bundleSettings['name'];
  155. $bundlePath = $this->kernelBundles[$bundleNamespace]->getPath();
  156. if (!empty($bundleSettings['include'])) {
  157. foreach ($bundleSettings['include'] as $include) {
  158. $this->paths[] = rtrim(rtrim($bundlePath, '/') . '/' . $include, '/') . '/';
  159. }
  160. } else {
  161. /**
  162. * If no include is set, include all namespace
  163. */
  164. $this->paths[] = rtrim($bundlePath, '/') . '/';
  165. }
  166. foreach ($bundleSettings['ignore'] as $ignore) {
  167. $this->excludedPaths[] = trim($ignore, '/');
  168. }
  169. }
  170. }
  171. /**
  172. * Perform a parsing inside all namespace map
  173. *
  174. * @return WorkerCollection collection of all info
  175. */
  176. private function parseNamespaceMap()
  177. {
  178. AnnotationRegistry::registerFile($this->kernel->locateResource("@GearmanBundle/Driver/Gearman/Work.php"));
  179. AnnotationRegistry::registerFile($this->kernel->locateResource("@GearmanBundle/Driver/Gearman/Job.php"));
  180. $reader = new SimpleAnnotationReader();
  181. $reader->addNamespace('Mmoreram\GearmanBundle\Driver');
  182. $workerCollection = new WorkerCollection;
  183. if (!empty($this->paths)) {
  184. $finder = new Finder();
  185. $finder
  186. ->files()
  187. ->followLinks()
  188. ->exclude($this->excludedPaths)
  189. ->in($this->paths)
  190. ->name('*.php');
  191. $workerCollection = $this->parseFiles($finder, $reader);
  192. }
  193. return $workerCollection;
  194. }
  195. /**
  196. * Load all workers with their jobs
  197. *
  198. * @param Finder $finder Finder
  199. * @param Reader $reader Reader
  200. *
  201. * @return WorkerCollection collection of all info
  202. */
  203. private function parseFiles(Finder $finder, Reader $reader)
  204. {
  205. $workerCollection = new WorkerCollection;
  206. /**
  207. * Every file found is parsed
  208. */
  209. foreach ($finder as $file) {
  210. /**
  211. * File is accepted to be parsed
  212. */
  213. $classNamespace = $this->getFileClassNamespace($file->getRealpath());
  214. $reflClass = new ReflectionClass($classNamespace);
  215. $classAnnotations = $reader->getClassAnnotations($reflClass);
  216. /**
  217. * Every annotation found is parsed
  218. */
  219. foreach ($classAnnotations as $annot) {
  220. /**
  221. * Annotation is only laoded if is typeof WorkAnnotation
  222. */
  223. if ($annot instanceof WorkAnnotation) {
  224. /**
  225. * Creates new Worker element with all its Job data
  226. */
  227. $worker = new Worker($annot, $reflClass, $reader, $this->servers, $this->defaultSettings);
  228. $workerCollection->add($worker);
  229. }
  230. }
  231. }
  232. return $workerCollection;
  233. }
  234. /**
  235. * Returns the full class name for the first class in the file.
  236. *
  237. * @param string $file A PHP file path
  238. *
  239. * @return string|false Full class name if found, false otherwise
  240. */
  241. protected function getFileClassNamespace($file)
  242. {
  243. $class = false;
  244. $namespace = false;
  245. $tokens = token_get_all(file_get_contents($file));
  246. for ($i = 0, $count = count($tokens); $i < $count; $i++) {
  247. $token = $tokens[$i];
  248. if (!is_array($token)) {
  249. continue;
  250. }
  251. if (true === $class && T_STRING === $token[0]) {
  252. return $namespace.'\\'.$token[1];
  253. }
  254. if (true === $namespace && T_STRING === $token[0]) {
  255. $namespace = '';
  256. do {
  257. $namespace .= $token[1];
  258. $token = $tokens[++$i];
  259. } while ($i < $count && is_array($token) && in_array($token[0], array(T_NS_SEPARATOR, T_STRING)));
  260. }
  261. if (T_CLASS === $token[0]) {
  262. $class = true;
  263. }
  264. if (T_NAMESPACE === $token[0]) {
  265. $namespace = true;
  266. }
  267. }
  268. return false;
  269. }
  270. }