Workflow.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <?php
  2. namespace WorkflowBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Gedmo\Mapping\Annotation as Gedmo;
  5. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
  8. use Symfony\Component\Yaml\Yaml;
  9. use Symfony\Bundle\FrameworkBundle\Console\Application;
  10. use Symfony\Component\Console\Input\ArrayInput;
  11. use Symfony\Component\Console\Output\BufferedOutput;
  12. use WorkflowBundle\Validator\Constraints as WorkflowAssert;
  13. use Symfony\Component\Workflow\Registry;
  14. // TODO : Remplazar las referencias de "global $kenerl por algo mÃs testeable"
  15. /**
  16. * Workflow
  17. *
  18. * @ORM\Entity(repositoryClass="WorkflowBundle\Repository\WorkflowRepository")
  19. * @ORM\HasLifecycleCallbacks
  20. * @UniqueEntity(fields={"name", "tenancyId"}, message="errors.duplicate_key")
  21. * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="unique_idx", columns={"name", "tenancy_id"})})
  22. */
  23. class Workflow
  24. {
  25. /**
  26. * @var int
  27. *
  28. * @ORM\Column(name="id", type="integer", nullable=false)
  29. * @ORM\Id
  30. * @ORM\GeneratedValue(strategy="AUTO")
  31. */
  32. private $id;
  33. /**
  34. * @var string
  35. *
  36. * @ORM\Column(type="string", length=100, nullable=false)
  37. */
  38. protected $name;
  39. /**
  40. * @var string
  41. *
  42. * @ORM\Column(type="string", length=100, nullable=false, options={"default":"state_machine"})
  43. */
  44. protected $type = "state_machine";
  45. /**
  46. * @var string
  47. *
  48. * @ORM\Column(type="string", length=100, nullable=false, options={"default":"single_state"})
  49. */
  50. protected $markingType = "single_state";
  51. /**
  52. * @var string
  53. *
  54. * @ORM\Column(type="string", length=100, nullable=false, options={"default":"currentState"})
  55. */
  56. protected $markingName = "currentState";
  57. /**
  58. * @var string
  59. *
  60. * @ORM\Column(type="string", length=350, nullable=true)
  61. */
  62. protected $description;
  63. /**
  64. * @var text
  65. *
  66. * @ORM\Column(type="text", nullable=false)
  67. * @WorkflowAssert\ContainsYaml
  68. */
  69. protected $template;
  70. /**
  71. * @Gedmo\Timestampable(on="create")
  72. * @ORM\Column(type="datetime")
  73. */
  74. protected $created;
  75. /**
  76. * @Gedmo\Timestampable(on="update")
  77. * @ORM\Column(type="datetime")
  78. */
  79. protected $updated;
  80. /**
  81. * @var int
  82. *
  83. * @ORM\Column(type="integer", nullable=false, options={"default":1})
  84. */
  85. protected $tenancyId = 1;
  86. /**
  87. * @ORM\Column(type="boolean", nullable=true, options={"default":true})
  88. *
  89. */
  90. protected $enable = true;
  91. /**
  92. * @ORM\Column(type="array", nullable=true)
  93. *
  94. */
  95. protected $support = array();
  96. public function __construct()
  97. {
  98. $this->support = array();
  99. }
  100. /**
  101. * @return string
  102. */
  103. public function __toString()
  104. {
  105. return (string) $this->name;
  106. }
  107. /**
  108. * Get id
  109. *
  110. * @return int
  111. */
  112. public function getId()
  113. {
  114. return $this->id;
  115. }
  116. /**
  117. * Set name
  118. *
  119. * @param string $name
  120. *
  121. * @return Workflow
  122. */
  123. public function setName($name)
  124. {
  125. $_name = strtolower($name);
  126. $name = str_replace(" ","_",$_name);
  127. $this->name = $name;
  128. return $this;
  129. }
  130. /**
  131. * Get name
  132. *
  133. * @return string
  134. */
  135. public function getName()
  136. {
  137. return $this->name;
  138. }
  139. /**
  140. * Set type
  141. *
  142. * @param string $type
  143. *
  144. * @return Workflow
  145. */
  146. public function setType($type)
  147. {
  148. $this->type = $type;
  149. return $this;
  150. }
  151. /**
  152. * Get type
  153. *
  154. * @return string
  155. */
  156. public function getType()
  157. {
  158. return $this->type;
  159. }
  160. /**
  161. * Set markingType
  162. *
  163. * @param string $markingType
  164. *
  165. * @return Workflow
  166. */
  167. public function setMarkingType($markingType)
  168. {
  169. $this->markingType = $markingType;
  170. return $this;
  171. }
  172. /**
  173. * Get markingType
  174. *
  175. * @return string
  176. */
  177. public function getMarkingType()
  178. {
  179. return $this->markingType;
  180. }
  181. /**
  182. * Set markingName
  183. *
  184. * @param string $markingName
  185. *
  186. * @return Workflow
  187. */
  188. public function setMarkingName($markingName)
  189. {
  190. $this->markingName = $markingName;
  191. return $this;
  192. }
  193. /**
  194. * Get markingName
  195. *
  196. * @return string
  197. */
  198. public function getMarkingName()
  199. {
  200. return $this->markingName;
  201. }
  202. /**
  203. * Set description
  204. *
  205. * @param string $description
  206. *
  207. * @return Workflow
  208. */
  209. public function setDescription($description)
  210. {
  211. $this->description = $description;
  212. return $this;
  213. }
  214. /**
  215. * Get description
  216. *
  217. * @return string
  218. */
  219. public function getDescription()
  220. {
  221. return $this->description;
  222. }
  223. /**
  224. * Set template
  225. *
  226. * @param text $template
  227. *
  228. * @return Workflow
  229. */
  230. public function setTemplate($template)
  231. {
  232. //$array = Yaml::parse($template);
  233. //$text = Yaml::dump($array,100,2);
  234. $this->template = $template;
  235. return $this;
  236. }
  237. /**
  238. * Get template
  239. *
  240. * @return text
  241. */
  242. public function getTemplate()
  243. {
  244. return $this->template;
  245. }
  246. /**
  247. * Get created
  248. *
  249. * @return \DateTime
  250. */
  251. public function getCreated()
  252. {
  253. return $this->created;
  254. }
  255. /**
  256. * Get updated
  257. *
  258. * @return \DateTime
  259. */
  260. public function getUpdated()
  261. {
  262. return $this->updated;
  263. }
  264. /**
  265. * Set tenancyId
  266. *
  267. * @param int $tenancyId
  268. *
  269. * @return Workflow
  270. */
  271. public function setTenancyId($tenancyId)
  272. {
  273. $this->tenancyId = $tenancyId;
  274. return $this;
  275. }
  276. /**
  277. * Get tenancyId
  278. *
  279. * @return int
  280. */
  281. public function getTenancyId()
  282. {
  283. return $this->tenancyId;
  284. }
  285. /**
  286. * Set enable
  287. *
  288. * @param boolean $enable
  289. * @return Workflow
  290. */
  291. public function setEnable($enable)
  292. {
  293. $this->enable = $enable;
  294. return $this;
  295. }
  296. /**
  297. * Get enable
  298. *
  299. * @return boolean
  300. */
  301. public function getEnable()
  302. {
  303. return $this->enable;
  304. }
  305. /**
  306. * @ORM\PostPersist
  307. */
  308. public function postPersist(LifecycleEventArgs $event)
  309. {
  310. $this->updateWorkflows();
  311. }
  312. /**
  313. * @ORM\PostUpdate
  314. */
  315. public function postUpdate(LifecycleEventArgs $event)
  316. {
  317. $this->updateWorkflows();
  318. }
  319. /**
  320. * @ORM\PreRemove
  321. */
  322. public function preRemove(LifecycleEventArgs $event)
  323. {
  324. $this->updateWorkflows();
  325. }
  326. // La idea era peticionar el comando y que actualice, pero desde aquí, el comando no obtiene el enable actualizado.
  327. public function updateWorkflowsCommand()
  328. {
  329. global $kernel;
  330. $console = $kernel->getRootDir()."/../bin/console";
  331. exec("php {$console} workflow:generate:list");
  332. exec("php {$console} cache:clear --env=prod");
  333. }
  334. public function updateWorkflows()
  335. {
  336. global $kernel;
  337. $em = $kernel->getContainer()->get("doctrine.orm.entity_manager");
  338. $workflows = $em->getRepository("WorkflowBundle:Workflow")->findBy(array('enable'=>1));
  339. $rootDir = $kernel->getRootDir();
  340. $workflow_template = array();
  341. $workflow_template['framework'] = array();
  342. $templates = array();
  343. foreach($workflows as $k => $workflow) {
  344. $body = array();
  345. $body['type'] = $workflow->getType();
  346. if(is_array($workflow->getMarkingName())) {
  347. $markingName = $workflow->getMarkingName();
  348. } else {
  349. $markingName = array(0 => $workflow->getMarkingName());
  350. }
  351. $body['marking_store'] = array('type' => $workflow->getMarkingType(), 'arguments' => $markingName);
  352. $body['supports'] = $workflow->getSupport();
  353. $body += Yaml::parse($workflow->getTemplate());
  354. $templates[$workflow->getName()] = $body;
  355. }
  356. $workflow_file = $rootDir."/Resources/workflows/workflow_list.yml";
  357. $handle = fopen($workflow_file,"w+");
  358. if($templates) {
  359. $workflow_template['framework'] = array('workflows' => $templates);
  360. $yaml = Yaml::dump($workflow_template,100,2);
  361. fwrite($handle,$yaml);
  362. } else {
  363. fwrite($handle,"");
  364. }
  365. fclose($handle);
  366. chmod($workflow_file, 0777);
  367. $console = $rootDir."/../bin/console";
  368. exec("php {$console} cache:clear --env=prod");
  369. /*$web_workflows = $rootDir."/../web/workflows_png/";
  370. foreach($workflows as $k => $workflow) {
  371. $file_locate = "{$web_workflows}/{$workflow->getName()}";
  372. exec("php {$console} workflow:dump {$workflow->getName()} > {$file_locate}.dot");
  373. if(file_exists("{$file_locate}.dot")) {
  374. exec("dot -Tpng {$file_locate}.dot -o {$file_locate}.png");
  375. }
  376. }*/
  377. }
  378. public function getDefinition($subject) {
  379. global $kernel;
  380. if($kernel){
  381. try {
  382. $registry = $kernel->getContainer()->get("workflow.registry");
  383. $definition = $registry->get($subject, $this->name)->getDefinition();
  384. } catch (\Exception $e) {
  385. return null;
  386. }
  387. return $definition;
  388. }
  389. }
  390. public function getInitialPlace($subject) {
  391. global $kernel;
  392. if($kernel){
  393. try {
  394. $registry = $kernel->getContainer()->get("workflow.registry");
  395. $definition = $registry->get($subject, $this->name)->getDefinition();
  396. } catch (\Exception $e) {
  397. return null;
  398. }
  399. return $definition->getInitialPlace();
  400. }
  401. }
  402. /**
  403. * Set support
  404. *
  405. * @param array $support
  406. * @return Workflow
  407. */
  408. public function setSupport($support)
  409. {
  410. $this->support = $support;
  411. return $this;
  412. }
  413. /**
  414. * Get support
  415. *
  416. * @return array
  417. */
  418. public function getSupport()
  419. {
  420. return $this->support;
  421. }
  422. /**
  423. * Get subject
  424. *
  425. * Para obtener un workflow desde el registry ya que localiza por object y workflowName
  426. */
  427. public function getSubject()
  428. {
  429. if($this->support) {
  430. $className = "\\".$this->support[0];
  431. return new $className();
  432. }
  433. return null;
  434. }
  435. }