Workflow.php 11 KB

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