123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- <?php
- namespace WorkflowBundle\Entity;
- use Base\AdminBundle\Traits\UseOneDefaultTrait;
- use Doctrine\ORM\Mapping as ORM;
- use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
- use Gedmo\Mapping\Annotation as Gedmo;
- use OwnerVoterBundle\Entity\Traits\OwnerTraitInterface;
- use OwnerVoterBundle\Entity\Traits\OwnerTrait;
- use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
- use Symfony\Component\Validator\Constraints as Assert;
- use Symfony\Component\Yaml\Yaml;
- use Symfony\Bundle\FrameworkBundle\Console\Application;
- use Symfony\Component\Console\Input\ArrayInput;
- use Symfony\Component\Console\Output\BufferedOutput;
- use Symfony\Component\Workflow\Registry;
- use WorkflowBundle\Validator\Constraints as WorkflowAssert;
- use WorkflowBundle\Interfaces\UseOneDefaultFilterInterface;
- /**
- * Workflow
- *
- * @ORM\Entity(repositoryClass="WorkflowBundle\Repository\WorkflowRepository")
- * @ORM\HasLifecycleCallbacks
- * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="unique_idx", columns={"name", "tenancy_id"})})
- *
- * @UniqueEntity(fields={"name", "tenancyId"}, message="errors.duplicate_key")
- */
- class Workflow implements OwnerTraitInterface, UseOneDefaultFilterInterface
- {
-
- use OwnerTrait;
- use UseOneDefaultTrait;
-
- /**
- * @var int
- *
- * @ORM\Column(name="id", type="integer", nullable=false)
- * @ORM\Id
- * @ORM\GeneratedValue(strategy="AUTO")
- */
- private $id;
- /**
- * @var string
- *
- * @ORM\Column(type="string", length=100, nullable=false)
- */
- protected $name;
- /**
- * @var string
- *
- * @ORM\Column(type="string", length=100, nullable=false, options={"default":"state_machine"})
- */
- protected $type = "state_machine";
- /**
- * @var string
- *
- * @ORM\Column(type="string", length=100, nullable=false, options={"default":"single_state"})
- */
- protected $markingType = "single_state";
- /**
- * @var string
- *
- * @ORM\Column(type="string", length=100, nullable=false, options={"default":"currentState"})
- */
- protected $markingName = "currentState";
- /**
- * @var string
- *
- * @ORM\Column(type="string", length=350, nullable=true)
- */
- protected $description;
- /**
- * @var text
- *
- * @ORM\Column(type="text", nullable=false)
- * @WorkflowAssert\ContainsYaml
- */
- protected $template;
- /**
- * @Gedmo\Timestampable(on="create")
- * @ORM\Column(type="datetime")
- */
- protected $created;
- /**
- * @Gedmo\Timestampable(on="update")
- * @ORM\Column(type="datetime")
- */
- protected $updated;
- /**
- * @var int
- *
- * @ORM\Column(type="integer", nullable=false, options={"default":1})
- */
- protected $tenancyId = 1;
- /**
- * @ORM\Column(type="boolean", nullable=true, options={"default":true})
- *
- */
- protected $enable = true;
- /**
- * @ORM\Column(type="array", nullable=true)
- *
- */
- protected $support = array();
-
- /**
- * var mixed Contiene el contenedor.
- */
- private $container;
-
- /**
- * Workflow constructor.
- */
- public function __construct()
- {
- $this->support = array();
- }
- /**
- * @return string
- */
- public function __toString()
- {
- return (string)$this->name;
- }
- /**
- * Get id
- *
- * @return int
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * Set name
- *
- * @param string $name
- *
- * @return Workflow
- */
- public function setName($name)
- {
- $_name = strtolower($name);
- $name = str_replace(" ", "_", $_name);
- $this->name = $name;
- return $this;
- }
- /**
- * Get name
- *
- * @return string
- */
- public function getName()
- {
- return $this->name;
- }
- /**
- * Set type
- *
- * @param string $type
- *
- * @return Workflow
- */
- public function setType($type)
- {
- $this->type = $type;
- return $this;
- }
- /**
- * Get type
- *
- * @return string
- */
- public function getType()
- {
- return $this->type;
- }
- /**
- * Set markingType
- *
- * @param string $markingType
- *
- * @return Workflow
- */
- public function setMarkingType($markingType)
- {
- $this->markingType = $markingType;
- return $this;
- }
- /**
- * Get markingType
- *
- * @return string
- */
- public function getMarkingType()
- {
- return $this->markingType;
- }
- /**
- * Set markingName
- *
- * @param string $markingName
- *
- * @return Workflow
- */
- public function setMarkingName($markingName)
- {
- $this->markingName = $markingName;
- return $this;
- }
- /**
- * Get markingName
- *
- * @return string
- */
- public function getMarkingName()
- {
- return $this->markingName;
- }
- /**
- * Set description
- *
- * @param string $description
- *
- * @return Workflow
- */
- public function setDescription($description)
- {
- $this->description = $description;
- return $this;
- }
- /**
- * Get description
- *
- * @return string
- */
- public function getDescription()
- {
- return $this->description;
- }
- /**
- * Set template
- *
- * @param text $template
- *
- * @return Workflow
- */
- public function setTemplate($template)
- {
- //$array = Yaml::parse($template);
- //$text = Yaml::dump($array,100,2);
- $this->template = $template;
- return $this;
- }
- /**
- * Get template
- *
- * @return text
- */
- public function getTemplate()
- {
- return $this->template;
- }
- /**
- * Get created
- *
- * @return \DateTime
- */
- public function getCreated()
- {
- return $this->created;
- }
- /**
- * Get updated
- *
- * @return \DateTime
- */
- public function getUpdated()
- {
- return $this->updated;
- }
- /**
- * Set tenancyId
- *
- * @param int $tenancyId
- *
- * @return Workflow
- */
- public function setTenancyId($tenancyId)
- {
- $this->tenancyId = $tenancyId;
- return $this;
- }
- /**
- * Get tenancyId
- *
- * @return int
- */
- public function getTenancyId()
- {
- return $this->tenancyId;
- }
- /**
- * Set enable
- *
- * @param boolean $enable
- * @return Workflow
- */
- public function setEnable($enable)
- {
- $this->enable = $enable;
- return $this;
- }
- /**
- * Get enable
- *
- * @return boolean
- */
- public function getEnable()
- {
- return $this->enable;
- }
- /**
- * @ORM\PostPersist
- */
- public function postPersist(LifecycleEventArgs $event)
- {
- $this->updateWorkflows();
- }
- /**
- * @ORM\PostUpdate
- */
- public function postUpdate(LifecycleEventArgs $event)
- {
- $this->updateWorkflows();
- }
- /**
- * @ORM\PreRemove
- */
- public function preRemove(LifecycleEventArgs $event)
- {
- $this->updateWorkflows();
- }
- // La idea era peticionar el comando y que actualice, pero desde aquí, el comando no obtiene el enable actualizado.
- public function updateWorkflowsCommand()
- {
- $console = $this->getContainer()->getParameter('kernel.root_dir') . "/../bin/console";
- exec("php {$console} workflow:generate:list");
- exec("php {$console} cache:clear --env=prod");
- }
- public function updateWorkflows()
- {
- $em = $this->getContainer()->get("doctrine.orm.entity_manager");
- $workflows = $em->getRepository("WorkflowBundle:Workflow")->findBy(array('enable' => 1));
- $rootDir = $this->getContainer()->getParameter('kernel.root_dir');
- $workflow_template = array();
- $workflow_template['framework'] = array();
- $templates = array();
- foreach ($workflows as $k => $workflow) {
- $body = array();
- $body['type'] = $workflow->getType();
- if (is_array($workflow->getMarkingName())) {
- $markingName = $workflow->getMarkingName();
- } else {
- $markingName = array(0 => $workflow->getMarkingName());
- }
- $body['marking_store'] = array('type' => $workflow->getMarkingType(), 'arguments' => $markingName);
- $body['supports'] = $workflow->getSupport();
- $body += Yaml::parse($workflow->getTemplate());
- $templates[$workflow->getName()] = $body;
- }
- $workflow_file = $rootDir . "/Resources/workflows/workflow_list.yml";
- $handle = fopen($workflow_file, "w+");
- if ($templates) {
- $workflow_template['framework'] = array('workflows' => $templates);
- $yaml = Yaml::dump($workflow_template, 100, 2);
- fwrite($handle, $yaml);
- } else {
- fwrite($handle, "");
- }
- fclose($handle);
- chmod($workflow_file, 0777);
- $console = $rootDir . "/../bin/console";
- exec("php {$console} cache:clear --env=prod");
- /*$web_workflows = $rootDir."/../web/workflows_png/";
- foreach($workflows as $k => $workflow) {
- $file_locate = "{$web_workflows}/{$workflow->getName()}";
- exec("php {$console} workflow:dump {$workflow->getName()} > {$file_locate}.dot");
- if(file_exists("{$file_locate}.dot")) {
- exec("dot -Tpng {$file_locate}.dot -o {$file_locate}.png");
- }
- }*/
- }
- public function getDefinition($subject)
- {
- try {
- $registry = $this->getContainer()->get("workflow.registry");
- $definition = $registry->get($subject, $this->name)->getDefinition();
- } catch (\Exception $e) {
- return null;
- }
- return $definition;
- }
- public function getInitialPlace($subject)
- {
- try {
- $registry = $this->getContainer()->get("workflow.registry");
- $definition = $registry->get($subject, $this->name)->getDefinition();
- } catch (\Exception $e) {
- return null;
- }
- return $definition->getInitialPlace();
- }
- /**
- * Set support
- *
- * @param array $support
- * @return Workflow
- */
- public function setSupport($support)
- {
- $this->support = $support;
- return $this;
- }
- /**
- * Get support
- *
- * @return array
- */
- public function getSupport()
- {
- return $this->support;
- }
- /**
- * Get subject
- *
- * Para obtener un workflow desde el registry ya que localiza por object y workflowName
- */
- public function getSubject()
- {
- if ($this->support) {
- $className = "\\" . $this->support[0];
- return new $className();
- }
- return null;
- }
- /**
- * @return mixed
- */
- public function getContainer()
- {
- return $this->container;
- }
- /**
- * @param mixed $container
- */
- public function setContainer($container)
- {
- $this->container = $container;
- }
-
- }
|