Workflow.php 9.4 KB

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