-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from Payum/payum-action-tag
Payum action tag
- Loading branch information
Showing
9 changed files
with
640 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
namespace Payum\Bundle\PayumBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class PayumActionsPass implements CompilerPassInterface | ||
{ | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
foreach ($container->findTaggedServiceIds('payum.action') as $id => $tagAttributes) { | ||
|
||
foreach ($tagAttributes as $attributes) { | ||
$paymentIds = array(); | ||
|
||
if (isset($attributes['all']) && $attributes['all']) { | ||
$paymentIds = array_merge($paymentIds, $this->findAllPaymentIds($container)); | ||
} | ||
|
||
if (isset($attributes['factory']) && $attributes['factory']) { | ||
$paymentIds = array_merge( | ||
$paymentIds, | ||
$this->findPaymentIdsByFactory($container, $attributes['factory']) | ||
); | ||
} | ||
if (isset($attributes['context']) && $attributes['context']) { | ||
$paymentIds = array_merge( | ||
$paymentIds, | ||
$this->findPaymentIdsByContext($container, $attributes['context']) | ||
); | ||
} | ||
|
||
$paymentIds = array_filter(array_unique($paymentIds)); | ||
foreach ($paymentIds as $paymentId) { | ||
$payment = $container->getDefinition($paymentId); | ||
$payment->addMethodCall('addAction', array( | ||
new Reference($id), | ||
isset($attributes['prepend']) && $attributes['prepend'] | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param ContainerBuilder $container | ||
* @param string $factoryName | ||
* | ||
* @return string[] | ||
*/ | ||
protected function findPaymentIdsByFactory(ContainerBuilder $container, $factoryName) | ||
{ | ||
$paymentIds = array(); | ||
foreach ($container->findTaggedServiceIds('payum.payment') as $id => $tagAttributes) { | ||
foreach ($tagAttributes as $attributes) { | ||
if (isset($attributes['factory']) && $attributes['factory'] == $factoryName) { | ||
$paymentIds[] = $id; | ||
} | ||
} | ||
} | ||
|
||
return $paymentIds; | ||
} | ||
|
||
/** | ||
* @param ContainerBuilder $container | ||
* @param string $contextName | ||
* | ||
* @return string[] | ||
*/ | ||
protected function findPaymentIdsByContext(ContainerBuilder $container, $contextName) | ||
{ | ||
$paymentIds = array(); | ||
foreach ($container->findTaggedServiceIds('payum.payment') as $id => $tagAttributes) { | ||
foreach ($tagAttributes as $attributes) { | ||
if (isset($attributes['context']) && $attributes['context'] == $contextName) { | ||
$paymentIds[] = $id; | ||
} | ||
} | ||
} | ||
|
||
return $paymentIds; | ||
} | ||
|
||
/** | ||
* @param ContainerBuilder $container | ||
* | ||
* @return string[] | ||
*/ | ||
protected function findAllPaymentIds(ContainerBuilder $container) | ||
{ | ||
return array_keys($container->findTaggedServiceIds('payum.payment')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Custom action usage | ||
|
||
Payment comes with built in actions but sometime you have to add your own. First you have to define a service: | ||
|
||
```yaml | ||
# src/Acme/PaymentBundle/Resources/config/services.yml | ||
|
||
services: | ||
acme.payum.action.foo: | ||
class: Acme\PaymentBundle\Payum\Action\FooAction | ||
``` | ||
There are several ways to add it to a payment: | ||
* Set it explicitly in config.yml. | ||
```yaml | ||
# app/config/config.yml | ||
|
||
payum: | ||
contexts: | ||
a_context: | ||
a_factory: | ||
actions: | ||
- acme.payum.action.foo | ||
``` | ||
* Tag it | ||
More powerful method is to add a tag `payum.action` to action server. Payum will do the reset. | ||
You can define a `factory` attribute inside that tag. | ||
In this case the action will be added to all payments created by requested factory. | ||
|
||
```yaml | ||
# app/config/config.yml | ||
payum: | ||
contexts: | ||
a_context: | ||
a_factory: ~ | ||
``` | ||
|
||
```yaml | ||
# src/Acme/PaymentBundle/Resources/config/services.yml | ||
services: | ||
acme.payum.action.foo: | ||
class: Acme\PaymentBundle\Payum\Action\FooAction | ||
tags: | ||
- {payum.action, { factory: a_factory }} | ||
``` | ||
|
||
Or you can set concrete `context` name. | ||
In this case the action will be added only to the payment with requested context name. | ||
|
||
```yaml | ||
# app/config/config.yml | ||
payum: | ||
contexts: | ||
a_context: | ||
a_factory: ~ | ||
``` | ||
|
||
```yaml | ||
# src/Acme/PaymentBundle/Resources/config/services.yml | ||
services: | ||
acme.payum.action.foo: | ||
class: Acme\PaymentBundle\Payum\Action\FooAction | ||
tags: | ||
- {payum.action, {context: a_context}} | ||
``` | ||
|
||
If `prepend` set to true the action is added before the rest. | ||
If you want to add the action to all configured payments set `all` to true. | ||
|
||
```yaml | ||
# src/Acme/PaymentBundle/Resources/config/services.yml | ||
services: | ||
acme.payum.action.foo: | ||
class: Acme\PaymentBundle\Payum\Action\FooAction | ||
tags: | ||
- {payum.action, { prepend: true, all: true }} | ||
``` | ||
|
||
Back to [index](index.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.