Skip to content

Commit

Permalink
Configurable template names for Paymill
Browse files Browse the repository at this point in the history
  • Loading branch information
alch committed Feb 12, 2015
1 parent d05d5ba commit 1bc6343
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ public function getConfigTreeBuilder()
->isRequired()
->cannotBeEmpty()
->end()

->arrayNode('templates')
->addDefaultsIfNotSet()
->children()
->scalarNode('view_template')
->defaultValue('PaymillBundle:Paymill:view.html.twig')
->end()
->scalarNode('scripts_template')
->defaultValue('PaymillBundle:Paymill:scripts.html.twig')
->end()
->end()
->end()
->arrayNode('form')
->children()
->scalarNode('submit_label')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('paymill.public.key', $config['public_key']);
$container->setParameter('paymill.controller.route', $config['controller_route']);

$container->setParameter('paymill.templates.view_template', $config['templates']['view_template']);
$container->setParameter('paymill.templates.scripts_template', $config['templates']['scripts_template']);

$container->setParameter('paymill.form.submit.label', $config['form']['submit_label']);
$container->setParameter('paymill.form.submit.css.class', $config['form']['submit_css_class']);

Expand Down
2 changes: 2 additions & 0 deletions src/PaymentSuite/PaymillBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ services:
paymill.public.key: %paymill.public.key%
form.factory: @form.factory
payment.bridge: @payment.bridge
view_template: %paymill.templates.view_template%
scripts_template: %paymill.templates.scripts_template%
tags:
- { name: twig.extension }

Expand Down
46 changes: 35 additions & 11 deletions src/PaymentSuite/PaymillBundle/Twig/PaymillExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace PaymentSuite\PaymillBundle\Twig;

use Symfony\Component\Form\FormFactory;
use Twig_Environment;
use Twig_Extension;
use Twig_SimpleFunction;

Expand All @@ -30,41 +31,64 @@ class PaymillExtension extends Twig_Extension
*
* Form factory
*/
private $formFactory;
protected $formFactory;

/**
* @var Twig_Environment
*
* Twig environment
*/
private $environment;
protected $environment;

/**
* @var string
*
* Public key
*/
private $publicKey;
protected $publicKey;

/**
* @var PaymentBridgeInterfaces
* @var PaymentBridgeInterface
*
* Payment Bridge
*/
private $paymentBridgeInterface;
protected $paymentBridgeInterface;

/**
* @var string
*
* View template name in Bundle notation
*/
protected $viewTemplate;

/**
* @var string
*
* Scripts template in Bundle notation
*/
protected $scriptsTemplate;

/**
* Construct method
*
* @param string $publicKey Public key
* @param FormFactory $formFactory Form factory
* @param PaymentBridgeInterface $paymentBridgeInterface Payment Bridge
* @param string $viewTemplate Twig template name for displaying the form
* @param string $scriptstemplate Twig template name for scripts/js
*/
public function __construct($publicKey, FormFactory $formFactory, PaymentBridgeInterface $paymentBridgeInterface)
public function __construct(
$publicKey,
FormFactory $formFactory,
PaymentBridgeInterface $paymentBridgeInterface,
$viewTemplate,
$scriptsTemplate)
{
$this->publicKey = $publicKey;
$this->formFactory = $formFactory;
$this->paymentBridgeInterface = $paymentBridgeInterface;
$this->viewTemplate = $viewTemplate;
$this->scriptsTemplate = $scriptsTemplate;
}

/**
Expand Down Expand Up @@ -97,25 +121,25 @@ public function getFunctions()
/**
* Render paymill form view
*
* @return string view html
* @return void
*/
public function renderPaymentView()
public function renderPaymentView($viewTemplate = null)
{
$formType = $this->formFactory->create('paymill_view');

return $this->environment->display('PaymillBundle:Paymill:view.html.twig', array(
$this->environment->display($viewTemplate ?: $this->viewTemplate, array(
'paymill_form' => $formType->createView(),
));
}

/**
* Render paymill scripts view
*
* @return string js code needed by Paymill behaviour
* @return void
*/
public function renderPaymentScripts()
{
return $this->environment->display('PaymillBundle:Paymill:scripts.html.twig', array(
$this->environment->display($this->scriptsTemplate, array(
'public_key' => $this->publicKey,
'currency' => $this->paymentBridgeInterface->getCurrency(),
));
Expand Down

0 comments on commit 1bc6343

Please sign in to comment.