Workflow.php 11 KB

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