This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Added source

  • Loading branch information...
jeremyFreeAgent committed May 15, 2012
1 parent 50c1561 commit 7977bb49364b8dd093cf7b3b1a743c1510668dd7
View
@@ -0,0 +1,14 @@
+language: php
+
+php:
+ - 5.3
+ - 5.4
+
+env:
+ - SYMFONY_VERSION=origin/master
+
+before_script:
+ - curl -s http://getcomposer.org/installer | php
+ - php composer.phar --dev install
+
+script: phpunit --coverage-text
@@ -0,0 +1,8 @@
+<?php
+
+namespace FreeAgent\Bundle\WorkflowBundle\Action;
+
+interface ActionInterface
+{
+ public function run($model);
+}
View
@@ -0,0 +1,11 @@
+<?php
+
+namespace FreeAgent\Bundle\WorkflowBundle\Action;
+
+class Example implements ActionInterface
+{
+ public function run($model)
+ {
+ return true;
+ }
+}
@@ -0,0 +1,56 @@
+<?php
+
+namespace FreeAgent\Bundle\WorkflowBundle\DependencyInjection;
+
+use Symfony\Component\Config\Definition\Builder\TreeBuilder;
+use Symfony\Component\Config\Definition\ConfigurationInterface;
+
+/**
+ * This is the class that validates and merges configuration from your app/config files
+ *
+ * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
+ */
+class Configuration implements ConfigurationInterface
+{
+ /**
+ * {@inheritDoc}
+ */
+ public function getConfigTreeBuilder()
+ {
+ $treeBuilder = new TreeBuilder();
+ $rootNode = $treeBuilder->root('free_agent_workflow');
+
+ $rootNode
+ ->fixXmlConfig('workflow')
+ ->children()
+ ->arrayNode('workflows')
+ ->useAttributeAsKey('id')
+ ->prototype('array')
+ ->children()
+ ->scalarNode('default_step')->end()
+ ->arrayNode('steps')
+ ->useAttributeAsKey('id')
+ ->prototype('array')
+ ->children()
+ ->scalarNode('label')->end()
+ ->arrayNode('actions')
+ ->prototype('scalar')->end()
+ ->end()
+ ->arrayNode('validators')
+ ->prototype('scalar')->end()
+ ->end()
+ ->arrayNode('possible_next_steps')
+ ->prototype('scalar')->end()
+ ->end()
+ ->end()
+ ->end()
+ ->end()
+ ->end()
+ ->end()
+ ->end()
+ ->end()
+ ;
+
+ return $treeBuilder;
+ }
+}
@@ -0,0 +1,32 @@
+<?php
+
+namespace FreeAgent\Bundle\WorkflowBundle\DependencyInjection;
+
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\Config\FileLocator;
+use Symfony\Component\HttpKernel\DependencyInjection\Extension;
+use Symfony\Component\DependencyInjection\Loader;
+
+/**
+ * This is the class that loads and manages your bundle configuration
+ *
+ * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
+ */
+class FreeAgentWorkflowExtension extends Extension
+{
+ /**
+ * {@inheritDoc}
+ */
+ public function load(array $configs, ContainerBuilder $container)
+ {
+ $configuration = new Configuration();
+ $config = $this->processConfiguration($configuration, $configs);
+
+ $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
+ $loader->load('services.xml');
+
+ foreach ($config['workflows'] as $key => $workflow) {
+ $container->setParameter('free_agent_workflow.workflows.'.$key, $workflow);
+ }
+ }
+}
@@ -0,0 +1,9 @@
+<?php
+
+namespace FreeAgent\Bundle\WorkflowBundle;
+
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+
+class FreeAgentWorkflowBundle extends Bundle
+{
+}
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) Jérémy Romey <jeremy@free-agent.fr>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
@@ -0,0 +1,198 @@
+<?php
+
+namespace FreeAgent\Bundle\WorkflowBundle\Manager;
+
+use Symfony\Component\DependencyInjection\Container;
+use FreeAgent\Bundle\WorkflowBundle\Model\ModelInterface;
+
+class Manager
+{
+ private $model;
+ private $workflow;
+ private $steps = array();
+ private $actions = array();
+ private $container;
+
+ /**
+ * [__construct description]
+ * @param Container $container [description]
+ */
+ public function __construct(Container $container)
+ {
+ $this->container = $container;
+ }
+
+ /**
+ * [getDefaultStepName description]
+ * @return string The default step name.
+ */
+ public function getDefaultStepName()
+ {
+ return $this->workflow['default_step'];
+ }
+
+ /**
+ * [configureWorkflow description]
+ * @param string $workflowName The workflow name.
+ * @return array The workflow.
+ */
+ public function configureWorkflow($workflowName)
+ {
+ $this->workflow = $this->container->getParameter('free_agent_workflow.workflows.'.$workflowName, null);
+
+ if (is_null($this->workflow)) {
+ throw new \Exception('The workflow "'.$workflowName.'" does not exist');
+ }
+
+ return $this->getWorkflow();
+ }
+
+ /**
+ * [getWorkflow description]
+ * @return array The workflow.
+ */
+ public function getWorkflow()
+ {
+ return $this->workflow;
+ }
+
+ /**
+ * [setModel description]
+ * @param ModelInterface $model The model subject of the workflow.
+ */
+ public function setModel(ModelInterface $model)
+ {
+ $this->model = $model;
+ $this->configureWorkflow($this->model->getWorkflowName());
+ }
+
+ /**
+ * [getModel description]
+ * @return ModelInterface The model subject of the workflow.
+ */
+ public function getModel()
+ {
+ return $this->model;
+ }
+
+ /**
+ * [getSteps description]
+ * @return array The steps of the workflow.
+ */
+ public function getSteps()
+ {
+ return $this->workflow['steps'];
+ }
+
+ /**
+ * [getStep description]
+ * @param string $stepName The name of the step.
+ * @return array The step.
+ */
+ public function getStep($stepName)
+ {
+ if (!array_key_exists($stepName, $this->workflow['steps'])) {
+ throw new \Exception('Step with name "'.$stepName.'" is not in "'.get_class($this).'" workflow');
+ }
+
+ return $this->workflow['steps'][$stepName];
+ }
+
+ /**
+ * [getCurrentStep description]
+ * @return array The current step.
+ */
+ public function getCurrentStep()
+ {
+ return $this->getStep($this->getCurrentStepName());
+ }
+
+ /**
+ * [getCurrentStepName description]
+ * @return string The current step name.
+ */
+ public function getCurrentStepName()
+ {
+ return $this->getModel()->getWorkflowStepName();
+ }
+
+ /**
+ * [reachStep description]
+ * @param string $stepName The name of the step to reach.
+ * @return boolean [description]
+ */
+ public function reachStep($stepName)
+ {
+ if ($this->canReachStep($stepName)){
+
+ $this->getModel()->setWorkflowStepName($stepName);
+
+ $this->runStepActions($stepName);
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * [canReachStep description]
+ * @param string $stepName The name of the step to reach.
+ * @return [type] [description]
+ */
+ public function canReachStep($stepName)
+ {
+ if ($stepName != $this->getCurrentStepName())
+ {
+ $step = $this->getStep($stepName);
+ $currentStep = $this->getCurrentStep();
+
+ if (!array_key_exists('possible_next_steps', $currentStep)) {
+
+ return false;
+ }
+
+ if (in_array($stepName, $currentStep['possible_next_steps'])) {
+
+ if (!array_key_exists('validators', $step)) {
+ return true;
+ } else {
+ foreach ($step['validators'] as $validator) {
+ $validator = $this->container->get($validator);
+
+ if (false == $validator->validate($this->getModel())) {
+
+ return false;
+ }
+
+ return true;
+ }
+ }
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * [runStepActions description]
+ * @return [type] [description]
+ */
+ public function runStepActions()
+ {
+ $currentStep = $this->getCurrentStepName();
+ if (array_key_exists('actions', $currentStep)) {
+
+ foreach ($step['actions'] as $action) {
+ $action = $this->container->get($action);
+
+ if (false == $action->run($this->getModel())) {
+
+ return false;
+ }
+
+ return true;
+ }
+ }
+ }
+}
View
@@ -0,0 +1,29 @@
+<?php
+
+namespace FreeAgent\Bundle\WorkflowBundle\Model;
+
+class Example implements ModelInterface
+{
+ private $workflow_step_name;
+ private $workflow_name = 'example';
+
+ public function getWorkflowName()
+ {
+ return $this->workflow_name;
+ }
+
+ public function setWorkflowName($workflowName)
+ {
+ return $this->workflow_name = $workflowName;
+ }
+
+ public function setWorkflowStepName($stepName)
+ {
+ $this->workflow_step_name = $stepName;
+ }
+
+ public function getWorkflowStepName()
+ {
+ return $this->workflow_step_name;
+ }
+}
View
@@ -0,0 +1,11 @@
+<?php
+
+namespace FreeAgent\Bundle\WorkflowBundle\Model;
+
+interface ModelInterface
+{
+ public function getWorkflowName();
+ public function setWorkflowName($workflowName);
+ public function setWorkflowStepName($stepName);
+ public function getWorkflowStepName();
+}
Oops, something went wrong.

0 comments on commit 7977bb4

Please sign in to comment.