Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
state_machine
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Insights
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Drupal.org issue queue
Drupal.org issue queue
Security & Compliance
Security & Compliance
Dependency List
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
project
state_machine
Commits
af788c8d
Commit
af788c8d
authored
Dec 08, 2015
by
bojanz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement guards.
parent
7ae76b6a
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
240 additions
and
4 deletions
+240
-4
README.md
README.md
+10
-0
commerce_workflow.services.yml
commerce_workflow.services.yml
+4
-0
src/CommerceWorkflowServiceProvider.php
src/CommerceWorkflowServiceProvider.php
+26
-0
src/DependencyInjection/Compiler/WorkflowGuardsPass.php
src/DependencyInjection/Compiler/WorkflowGuardsPass.php
+37
-0
src/Plugin/Workflow/Workflow.php
src/Plugin/Workflow/Workflow.php
+41
-4
src/WorkflowGuard/WorkflowGuardFactory.php
src/WorkflowGuard/WorkflowGuardFactory.php
+57
-0
src/WorkflowGuard/WorkflowGuardFactoryInterface.php
src/WorkflowGuard/WorkflowGuardFactoryInterface.php
+25
-0
src/WorkflowGuard/WorkflowGuardInterface.php
src/WorkflowGuard/WorkflowGuardInterface.php
+40
-0
No files found.
README.md
View file @
af788c8d
...
...
@@ -57,6 +57,16 @@ default:
to
:
canceled
```
Transitions can be further restricted by
[
guards
](
https://github.com/bojanz/commerce_workflow/blob/8.x-1.x/src/WorkflowGuard/WorkflowGuardInterface.php
)
, which are implemented as tagged services:
```
yaml
mymodule.fulfillment_guard
:
class
:
Drupal\mymodule\WorkflowGuard\FulfillmentGuard
tags
:
-
{
name
:
commerce_workflow.workflow_guard
,
group
:
order
}
```
The group argument allows the guard factory to only instantiate the guards relevant
to a specific workflow group.
The current state is stored in a
[
StateItem
](
https://github.com/bojanz/commerce_workflow/blob/8.x-1.x/src/Plugin/Field/FieldType/StateItem.php
)
field.
A field setting specifies the used workflow, or a value callback that allows
the workflow to be resolved at runtime (checkout workflow based on the used plugin, etc.
...
...
commerce_workflow.services.yml
View file @
af788c8d
services
:
commerce_workflow.workflow_guard_factory
:
class
:
Drupal\commerce_workflow\WorkflowGuard\WorkflowGuardFactory
arguments
:
[
'
@service_container'
]
plugin.manager.workflow
:
class
:
Drupal\commerce_workflow\WorkflowManager
arguments
:
[
'
@module_handler'
,
'
@cache.discovery'
,
'
@plugin.manager.workflow_group'
]
...
...
src/CommerceWorkflowServiceProvider.php
0 → 100644
View file @
af788c8d
<?php
/**
* @file
* Contains \Drupal\commerce_workflow\CommerceWorkflowServiceProvider.
*/
namespace
Drupal\commerce_workflow
;
use
Drupal\Core\DependencyInjection\ServiceProviderInterface
;
use
Drupal\Core\DependencyInjection\ContainerBuilder
;
use
Drupal\commerce_workflow\DependencyInjection\Compiler\WorkflowGuardsPass
;
/**
* Registers the workflow guard compiler pass.
*/
class
CommerceWorkflowServiceProvider
implements
ServiceProviderInterface
{
/**
* {@inheritdoc}
*/
public
function
register
(
ContainerBuilder
$container
)
{
$container
->
addCompilerPass
(
new
WorkflowGuardsPass
());
}
}
src/DependencyInjection/Compiler/WorkflowGuardsPass.php
0 → 100644
View file @
af788c8d
<?php
/**
* @file
* Contains \Drupal\commerce_workflow\DependencyInjection\Compiler\WorkflowGuardsPass.
*/
namespace
Drupal\commerce_workflow\DependencyInjection\Compiler
;
use
Symfony\Component\DependencyInjection\ContainerBuilder
;
use
Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface
;
/**
* Adds the context provider service IDs to the context manager.
*/
class
WorkflowGuardsPass
implements
CompilerPassInterface
{
/**
* {@inheritdoc}
*
* Passes the grouped service IDs of workflow guards to the guard factory.
*/
public
function
process
(
ContainerBuilder
$container
)
{
$guards
=
[];
foreach
(
$container
->
findTaggedServiceIds
(
'commerce_workflow.workflow_guard'
)
as
$id
=>
$attributes
)
{
if
(
empty
(
$attributes
[
0
][
'group'
]))
{
continue
;
}
$guards
[
$attributes
[
0
][
'group'
]][]
=
$id
;
}
$definition
=
$container
->
getDefinition
(
'commerce_workflow.workflow_guard_factory'
);
$definition
->
addArgument
(
$guards
);
}
}
src/Plugin/Workflow/Workflow.php
View file @
af788c8d
...
...
@@ -7,13 +7,23 @@
namespace
Drupal\commerce_workflow\Plugin\Workflow
;
use
Drupal\commerce_workflow\WorkflowGuard\WorkflowGuardFactoryInterface
;
use
Drupal\Core\Entity\EntityInterface
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Drupal\Core\Plugin\PluginBase
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Defines the class for workflows.
*/
class
Workflow
extends
PluginBase
implements
WorkflowInterface
{
class
Workflow
extends
PluginBase
implements
WorkflowInterface
,
ContainerFactoryPluginInterface
{
/**
* The workflow guard factory.
*
* @var \Drupal\commerce_workflow\WorkflowGuard\WorkflowGuardFactoryInterface
*/
protected
$guardFactory
;
/**
* The initialized states.
...
...
@@ -30,11 +40,21 @@ class Workflow extends PluginBase implements WorkflowInterface {
protected
$transitions
=
[];
/**
* {@inheritdoc}
* Constructs a new Workflow object.
*
* @param array $configuration
* The plugin configuration.
* @param string $plugin_id
* The workflow plugin_id.
* @param mixed $plugin_definition
* The workflow plugin implementation definition.
* @param \Drupal\commerce_workflow\WorkflowGuard\WorkflowGuardFactoryInterface $guard_factory
* The workflow guard factory.
*/
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
,
WorkflowGuardFactoryInterface
$guard_factory
)
{
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
);
$this
->
guardFactory
=
$guard_factory
;
// Populate value objects for states and transitions.
foreach
(
$plugin_definition
[
'states'
]
as
$id
=>
$state_definition
)
{
$this
->
states
[
$id
]
=
new
WorkflowState
(
$id
,
$state_definition
[
'label'
]);
...
...
@@ -50,6 +70,18 @@ class Workflow extends PluginBase implements WorkflowInterface {
}
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
return
new
static
(
$configuration
,
$plugin_id
,
$plugin_definition
,
$container
->
get
(
'commerce_workflow.workflow_guard_factory'
)
);
}
/**
* {@inheritdoc}
*/
...
...
@@ -135,7 +167,12 @@ class Workflow extends PluginBase implements WorkflowInterface {
* TRUE if the transition is allowed, FALSE otherwise.
*/
protected
function
isTransitionAllowed
(
WorkflowTransition
$transition
,
EntityInterface
$entity
)
{
// @todo
foreach
(
$this
->
guardFactory
->
get
(
$this
->
getGroup
())
as
$guard
)
{
if
(
$guard
->
allowed
(
$transition
,
$this
,
$entity
)
===
FALSE
)
{
return
FALSE
;
}
}
return
TRUE
;
}
...
...
src/WorkflowGuard/WorkflowGuardFactory.php
0 → 100644
View file @
af788c8d
<?php
/**
* @file
* Contains \Drupal\commerce_workflow\WorkflowGuard\WorkflowGuardFactory.
*/
namespace
Drupal\commerce_workflow\WorkflowGuard
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
class
WorkflowGuardFactory
{
/**
* The service container.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected
$container
;
/**
* The guard service ids, grouped by workflow group id.
*
* @var string[]
*/
protected
$guardServiceIds
;
/**
* Constructs a new WorkflowGuardFactory object.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The service container.
* @param string[] $guard_service_ids
* The guard service ids, grouped by workflow group id
*/
public
function
__construct
(
ContainerInterface
$container
,
array
$guard_service_ids
)
{
$this
->
container
=
$container
;
$this
->
guardServiceIds
=
$guard_service_ids
;
}
/**
* {@inheritdoc}
*/
public
function
get
(
$group_id
)
{
if
(
!
isset
(
$this
->
guardServiceIds
[
$group_id
]))
{
return
[];
}
$guards
=
[];
foreach
(
$this
->
guardServiceIds
[
$group_id
]
as
$service_id
)
{
$guards
[]
=
$this
->
container
->
get
(
$service_id
);
}
return
$guards
;
}
}
src/WorkflowGuard/WorkflowGuardFactoryInterface.php
0 → 100644
View file @
af788c8d
<?php
/**
* @file
* Contains \Drupal\commerce_workflow\WorkflowGuard\WorkflowGuardFactoryInterface.
*/
namespace
Drupal\commerce_workflow\WorkflowGuard
;
/**
* Defines the interface for workflow guard factories.
*/
interface
WorkflowGuardFactoryInterface
{
/**
* Gets the instantiated workflow guards for the given group id.
*
* @param string $group_id
* The group id.
*
* @return \Drupal\commerce_workflow\WorkflowGuard\WorkflowGuardInterface[]
*/
public
function
get
(
$group_id
);
}
src/WorkflowGuard/WorkflowGuardInterface.php
0 → 100644
View file @
af788c8d
<?php
/**
* @file
* Contains \Drupal\commerce_workflow\WorkflowGuard\WorkflowGuardInterface.
*/
namespace
Drupal\commerce_workflow\WorkflowGuard
;
use
Drupal\commerce_workflow\Plugin\Workflow\WorkflowInterface
;
use
Drupal\commerce_workflow\Plugin\Workflow\WorkflowTransition
;
use
Drupal\Core\Entity\EntityInterface
;
/**
* Defines the interface for workflow guards.
*
* Allows for custom logic controling the availability of specific transitions.
* Transitions could be restricted based on the current user's permissions, a
* parent entity field, etc.
*
* By default, a transition is allowed unless at least one guard returns FALSE.
*/
interface
WorkflowGuardInterface
{
/**
* Checks whether the given transition is allowed.
*
* @param \Drupal\commerce_workflow\Plugin\Workflow\WorkflowTransition $transition
* The transition.
* @param \Drupal\commerce_workflow\Plugin\Workflow\WorkflowInterface $workflow
* The workflow.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The parent entity.
*
* @return bool
* TRUE if the transition is allowed, FALSE otherwise.
*/
public
function
allowed
(
WorkflowTransition
$transition
,
WorkflowInterface
$workflow
,
EntityInterface
$entity
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment