Workflow.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 int
  81. *
  82. * @ORM\Column(type="integer", nullable=false, options={"default":1})
  83. */
  84. protected $tenancyId = 1;
  85. /**
  86. * @ORM\Column(type="boolean", nullable=true, options={"default":true})
  87. *
  88. */
  89. protected $enable = true;
  90. public function __construct()
  91. {
  92. }
  93. /**
  94. * @return string
  95. */
  96. public function __toString()
  97. {
  98. return (string) $this->name;
  99. }
  100. /**
  101. * Get id
  102. *
  103. * @return int
  104. */
  105. public function getId()
  106. {
  107. return $this->id;
  108. }
  109. /**
  110. * Set name
  111. *
  112. * @param string $name
  113. *
  114. * @return Workflow
  115. */
  116. public function setName($name)
  117. {
  118. $_name = strtolower($name);
  119. $name = str_replace(" ","_",$_name);
  120. $this->name = $name;
  121. return $this;
  122. }
  123. /**
  124. * Get name
  125. *
  126. * @return string
  127. */
  128. public function getName()
  129. {
  130. return $this->name;
  131. }
  132. /**
  133. * Set type
  134. *
  135. * @param string $type
  136. *
  137. * @return Workflow
  138. */
  139. public function setType($type)
  140. {
  141. $this->type = $type;
  142. return $this;
  143. }
  144. /**
  145. * Get type
  146. *
  147. * @return string
  148. */
  149. public function getType()
  150. {
  151. return $this->type;
  152. }
  153. /**
  154. * Set markingType
  155. *
  156. * @param string $markingType
  157. *
  158. * @return Workflow
  159. */
  160. public function setMarkingType($markingType)
  161. {
  162. $this->markingType = $markingType;
  163. return $this;
  164. }
  165. /**
  166. * Get markingType
  167. *
  168. * @return string
  169. */
  170. public function getMarkingType()
  171. {
  172. return $this->markingType;
  173. }
  174. /**
  175. * Set markingName
  176. *
  177. * @param string $markingName
  178. *
  179. * @return Workflow
  180. */
  181. public function setMarkingName($markingName)
  182. {
  183. $this->markingName = $markingName;
  184. return $this;
  185. }
  186. /**
  187. * Get markingName
  188. *
  189. * @return string
  190. */
  191. public function getMarkingName()
  192. {
  193. return $this->markingName;
  194. }
  195. /**
  196. * Set description
  197. *
  198. * @param string $description
  199. *
  200. * @return Workflow
  201. */
  202. public function setDescription($description)
  203. {
  204. $this->description = $description;
  205. return $this;
  206. }
  207. /**
  208. * Get description
  209. *
  210. * @return string
  211. */
  212. public function getDescription()
  213. {
  214. return $this->description;
  215. }
  216. /**
  217. * Set template
  218. *
  219. * @param text $template
  220. *
  221. * @return Workflow
  222. */
  223. public function setTemplate($template)
  224. {
  225. //$array = Yaml::parse($template);
  226. //$text = Yaml::dump($array,100,2);
  227. $this->template = $template;
  228. return $this;
  229. }
  230. /**
  231. * Get template
  232. *
  233. * @return text
  234. */
  235. public function getTemplate()
  236. {
  237. return $this->template;
  238. }
  239. /**
  240. * Get created
  241. *
  242. * @return \DateTime
  243. */
  244. public function getCreated()
  245. {
  246. return $this->created;
  247. }
  248. /**
  249. * Get updated
  250. *
  251. * @return \DateTime
  252. */
  253. public function getUpdated()
  254. {
  255. return $this->updated;
  256. }
  257. /**
  258. * Set tenancyId
  259. *
  260. * @param int $tenancyId
  261. *
  262. * @return Workflow
  263. */
  264. public function setTenancyId($tenancyId)
  265. {
  266. $this->tenancyId = $tenancyId;
  267. return $this;
  268. }
  269. /**
  270. * Get tenancyId
  271. *
  272. * @return int
  273. */
  274. public function getTenancyId()
  275. {
  276. return $this->tenancyId;
  277. }
  278. /**
  279. * Set enable
  280. *
  281. * @param boolean $enable
  282. * @return Workflow
  283. */
  284. public function setEnable($enable)
  285. {
  286. $this->enable = $enable;
  287. return $this;
  288. }
  289. /**
  290. * Get enable
  291. *
  292. * @return boolean
  293. */
  294. public function getEnable()
  295. {
  296. return $this->enable;
  297. }
  298. /**
  299. * @ORM\PostPersist
  300. */
  301. public function postPersist(LifecycleEventArgs $event)
  302. {
  303. $this->updateWorkflows();
  304. }
  305. /**
  306. * @ORM\PostUpdate
  307. */
  308. public function postUpdate(LifecycleEventArgs $event)
  309. {
  310. $this->updateWorkflows();
  311. }
  312. /**
  313. * @ORM\PreRemove
  314. */
  315. public function preRemove(LifecycleEventArgs $event)
  316. {
  317. $this->updateWorkflows();
  318. }
  319. // La idea era peticionar el comando y que actualice, pero desde aquí, el comando no obtiene el enable actualizado.
  320. public function updateWorkflowsCommand()
  321. {
  322. global $kernel;
  323. $console = $kernel->getRootDir()."/../bin/console";
  324. exec("php {$console} workflow:generate:list");
  325. exec("php {$console} cache:clear --env=prod");
  326. }
  327. public function updateWorkflows()
  328. {
  329. global $kernel;
  330. $em = $kernel->getContainer()->get("doctrine.orm.entity_manager");
  331. $workflows = $em->getRepository("WorkflowBundle:Workflow")->findBy(array('enable'=>1));
  332. $rootDir = $kernel->getRootDir();
  333. $workflow_template = array();
  334. $workflow_template['framework'] = array();
  335. $templates = array();
  336. foreach($workflows as $k => $workflow) {
  337. $body = array();
  338. $body['type'] = $workflow->getType();
  339. if(is_array($workflow->getMarkingName())) {
  340. $markingName = $workflow->getMarkingName();
  341. } else {
  342. $markingName = array(0 => $workflow->getMarkingName());
  343. }
  344. $body['marking_store'] = array('type' => $workflow->getMarkingType(), 'arguments' => $markingName);
  345. $body += Yaml::parse($workflow->getTemplate());
  346. $templates[$workflow->getName()] = $body;
  347. }
  348. $workflow_file = $rootDir."/Resources/workflows/workflow_list.yml";
  349. $handle = fopen($workflow_file,"w+");
  350. if($templates) {
  351. $workflow_template['framework'] = array('workflows' => $templates);
  352. $yaml = Yaml::dump($workflow_template,100,2);
  353. fwrite($handle,$yaml);
  354. } else {
  355. fwrite($handle,"");
  356. }
  357. fclose($handle);
  358. chmod($workflow_file, 0777);
  359. $console = $rootDir."/../bin/console";
  360. exec("php {$console} cache:clear --env=prod");
  361. $web_workflows = $rootDir."/../web/workflows_png/";
  362. foreach($workflows as $k => $workflow) {
  363. $file_locate = "{$web_workflows}/{$workflow->getName()}";
  364. exec("php {$console} workflow:dump {$workflow->getName()} > {$file_locate}.dot");
  365. if(file_exists("{$file_locate}.dot")) {
  366. exec("dot -Tpng {$file_locate}.dot -o {$file_locate}.png");
  367. }
  368. }
  369. }
  370. public function getDefinition($subject) {
  371. global $kernel;
  372. try {
  373. $registry = $kernel->getContainer()->get("workflow.registry");
  374. $definition = $registry->get($subject, $this->name)->getDefinition();
  375. } catch (\Exception $e) {
  376. return null;
  377. }
  378. return $definition;
  379. }
  380. public function getInitialPlace($subject) {
  381. global $kernel;
  382. try {
  383. $registry = $kernel->getContainer()->get("workflow.registry");
  384. $definition = $registry->get($subject, $this->name)->getDefinition();
  385. } catch (\Exception $e) {
  386. return null;
  387. }
  388. return $definition->getInitialPlace();
  389. }
  390. }