123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- /**
- * Gearman Bundle for Symfony2
- *
- * @author Marc Morera <yuhu@mmoreram.com>
- * @since 2013
- */
- namespace Mmoreram\GearmanBundle\Module;
- use Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
- use Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
- use Symfony\Component\DependencyInjection\ContainerAware;
- use ReflectionMethod;
- /**
- * Job class
- *
- * This class provide all worker definition.
- */
- class JobClass extends ContainerAware
- {
- /**
- * @var string
- *
- * Callable name for this job
- * If is setted on annotations, this value will be used
- * otherwise, natural method name will be used.
- */
- private $callableName;
- /**
- * @var string
- *
- * Method name
- */
- private $methodName;
- /**
- * @var string
- *
- * RealCallable name for this job
- * natural method name will be used.
- */
- private $realCallableName;
- /**
- * @var string
- *
- * Description of Job
- */
- private $description;
- /**
- * @var integer
- *
- * Number of iterations this job will be alive before die
- */
- private $iterations;
- /**
- * @var string
- *
- * Default method this job will be call into Gearman client
- */
- private $defaultMethod;
- /**
- * @var array
- *
- * Collection of servers to connect
- */
- private $servers;
- /**
- * Construct method
- *
- * @param JobAnnotation $jobAnnotation jobAnnotation class
- * @param ReflectionMethod $method ReflextionMethod class
- * @param string $callableNameClass Callable name class
- * @param array $servers Array of servers defined for Worker
- * @param array $defaultSettings Default settings for Worker
- */
- public function __construct(JobAnnotation $jobAnnotation, ReflectionMethod $method, $callableNameClass, array $servers, array $defaultSettings)
- {
- $this->callableName = is_null($jobAnnotation->name)
- ? $method->getName()
- : $jobAnnotation->name;
- $this->methodName = $method->getName();
- $this->realCallableName = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
- $this->description = is_null($jobAnnotation->description)
- ? 'No description is defined'
- : $jobAnnotation->description;
- $this
- ->loadSettings($jobAnnotation, $defaultSettings)
- ->loadServers($jobAnnotation, $servers);
- }
- /**
- * Load settings
- *
- * @param JobAnnotation $jobAnnotation JobAnnotation class
- * @param array $servers Array of servers defined for Worker
- *
- * @return JobClass self Object
- */
- private function loadServers(JobAnnotation $jobAnnotation, array $servers)
- {
- /**
- * By default, this job takes default servers defined in its worker
- */
- $this->servers = $servers;
- /**
- * If is configured some servers definition in the worker, overwrites
- */
- if ($jobAnnotation->servers) {
- $this->servers = ( is_array($jobAnnotation->servers) && !isset($jobAnnotation->servers['host']) )
- ? $jobAnnotation->servers
- : array($jobAnnotation->servers);
- }
- return $this;
- }
- /**
- * Load settings
- *
- * @param WorkAnnotation $JobAnnotation JobAnnotation class
- * @param array $defaultSettings Default settings for Worker
- *
- * @return JobClass self Object
- */
- private function loadSettings(JobAnnotation $jobAnnotation, array $defaultSettings)
- {
- $this->iterations = is_null($jobAnnotation->iterations)
- ? (int) $defaultSettings['iterations']
- : $jobAnnotation->iterations;
- $this->defaultMethod = is_null($jobAnnotation->defaultMethod)
- ? $defaultSettings['method']
- : $jobAnnotation->defaultMethod;
- return $this;
- }
- /**
- * Retrieve all Job data in cache format
- *
- * @return array
- */
- public function toArray()
- {
- return array(
- 'callableName' => $this->callableName,
- 'methodName' => $this->methodName,
- 'realCallableName' => $this->realCallableName,
- 'description' => $this->description,
- 'iterations' => $this->iterations,
- 'servers' => $this->servers,
- 'defaultMethod' => $this->defaultMethod,
- );
- }
- }
|