Workflow.php 11 KB

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