Workflow.php 10 KB

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