Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor all the service to register life cycle #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 39 additions & 58 deletions src/WorkflowServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Silex\Api\BootableProviderInterface;
use Silex\Application;
use Symfony\Bridge\Twig\Extension\WorkflowExtension;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
Expand All @@ -19,7 +18,7 @@
use Symfony\Component\Workflow\Validator\WorkflowValidator;
use Symfony\Component\Workflow\Workflow;

class WorkflowServiceProvider implements ServiceProviderInterface, BootableProviderInterface
class WorkflowServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}
Expand Down Expand Up @@ -50,75 +49,57 @@ function ($property = 'marking', PropertyAccessorInterface $propertyAccessor = n
return new Registry();
};

$app['workflow.config'] = [];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll prefer to remain this service parameter for two reasons:

  1. it's a sort of "fallback" in case no config was provided by user.
  2. one can add workflow configurations from any place after workflow service provider registration:
    <?php
    // some service provider
    $app->extend('workflow.config', function (array $config) {
        $config[] = ... // here comes workflow configuration
    });

or simply

    <?php
    // yet another service provider
    $app['workflow.config'][] = ... // here comes another workflow configuration

}

/**
* {@inheritdoc}
*/
public function boot(Application $app)
{
$this->registerWorkflowConfiguration($app);
$app['workflow.registry'] = function ($app) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This service definition rewrites previous one. Earlier can be removed.

$needle = new Registry();

if (class_exists('Symfony\Bridge\Twig\Extension\WorkflowExtension')) {
$app->extend('twig', function (\Twig_Environment $twig, Container $app) {
$twig->addExtension(new WorkflowExtension($app['workflow.registry']));
foreach ($app['workflow.config'] as $name => $workflow) {
$type = $workflow['type'];

return $twig;
});
}
}
if (!isset($workflow['transitions'][0])) {
foreach ((array)$workflow['transitions'] as $workflowName => $transition) {
if (array_key_exists('name', $transition)) {
continue;
}

/**
* @param Container $app
*/
private function registerWorkflowConfiguration(Container $app)
{
if (!$workflows = $app['workflow.config']) {
return;
}
$transition['name'] = $workflowName;
$workflow['transitions'][$workflowName] = $transition;
}
}

foreach ($workflows as $name => $workflow) {
$type = $workflow['type'];
$workflowId = $type . '.' . $name;
$definitionId = $workflowId . '.definition';
$markingStoreId = $workflowId . '.marking_store';

if (!isset($workflow['transitions'][0])) {
foreach ((array) $workflow['transitions'] as $workflowName => $transition) {
if (array_key_exists('name', $transition)) {
continue;
}
$app[$definitionId] = $this->createDefinition($workflow, $type);

$transition['name'] = $workflowName;
$workflow['transitions'][$workflowName] = $transition;
}
}
if ($app['debug']) {
$this
->createValidator($type, isset($workflow['marking_store']['type']) ? $workflow['marking_store']['type'] : null)
->validate($app[$definitionId], $name);
}

$workflowId = $type.'.'.$name;
$definitionId = $workflowId.'.definition';
$markingStoreId = $workflowId.'.marking_store';
$app[$markingStoreId] = $this->createMarkingStore($workflow);

$app[$definitionId] = $this->createDefinition($workflow, $type);
$app[$workflowId] = function (Container $app) use ($name, $type, $definitionId, $markingStoreId) {
return call_user_func($app[sprintf('%s.factory', $type)], $app[$definitionId], $app[$markingStoreId], $name);
};

if ($app['debug']) {
$this
->createValidator($type, isset($workflow['marking_store']['type']) ? $workflow['marking_store']['type'] : null)
->validate($app[$definitionId], $name);
}
foreach ($workflow['supports'] as $supportedClass) {
$needle->add($app[$workflowId], $supportedClass);
}

$app[$markingStoreId] = $this->createMarkingStore($workflow);
}
return $needle;
};

$app[$workflowId] = function (Container $app) use ($name, $type, $definitionId, $markingStoreId) {
return call_user_func($app[sprintf('%s.factory', $type)], $app[$definitionId], $app[$markingStoreId], $name);
};
//add symfony twig extension
$app->extend('twig', function ($twig, $app) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should remain class_exists check for those who don't use Twig.

$twig->addExtension(new WorkflowExtension($app['workflow.registry']));
return $twig;
});
}

$app->extend('workflow.registry', function (Registry $registry, Container $app) use ($workflow, $workflowId) {
foreach ($workflow['supports'] as $supportedClass) {
$registry->add($app[$workflowId], $supportedClass);
}

return $registry;
});
}
}

/**
* @param array $workflow
Expand Down