Workflow.php 11 KB

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