Workflow.php 9.9 KB

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