Skip to content

Commit

Permalink
Remove controller manipulation from EventListener/ControllerListener
Browse files Browse the repository at this point in the history
Add customAction which handles all custom actions (and calls the right method based on provided action name)
Generate attempt, execute, success and error methods for each action.
Rename action.php.twig.twig  => action.php.twig
Remove unnecesary setVariables from CutomBuilderTemplate.php
  • Loading branch information
loostro committed Jun 15, 2013
1 parent 09e32e5 commit b160fe6
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 79 deletions.
14 changes: 0 additions & 14 deletions Builder/Admin/CustomBuilderTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,4 @@ public function getOutputName()
{
return 'Resources/views/'.$this->getBaseGeneratorName().'Custom/index.html.twig';
}

/**
* Prevent no BC Break
*
* @param array $variables
*/
public function setVariables(array $variables)
{
if (!array_key_exists('title', $variables)) {
$variables['title'] = 'Are you sure?';
}

parent::setVariables($variables);
}
}
19 changes: 0 additions & 19 deletions EventListener/ControllerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ public function onKernelRequest(GetResponseEvent $event)
$this->container->get('twig')->getExtension('core')->setNumberFormat($twig_params['number_format']['decimal'], $twig_params['number_format']['decimal_point'], $twig_params['number_format']['thousand_separator']);
}
}

$this->rewriteCustomActionController($event->getRequest());
}

protected function getGenerator($generatorYaml)
Expand Down Expand Up @@ -115,22 +113,5 @@ protected function getGeneratorYml($controller)

throw new NotAdminGeneratedException;
}

/**
* Rewrites custom actions controller
*/
private function rewriteCustomActionController($request)
{
$controller = $request->attributes->get('_controller');
$pattern = "/Custom:custom$/";
$replacement = "Custom:".$request->query->get('action');

if (preg_match($pattern, $controller)) {
$rewritten = preg_replace($pattern, $replacement, $controller);
$request->attributes->set('_controller', $rewritten);
}

return $request;
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% use '../CommonAdmin/CustomAction/custom.php.twig' %}
{% use '../CommonAdmin/CustomAction/action.php.twig' %}
{% use '../CommonAdmin/security_action.php.twig' %}
{% use '../CommonAdmin/csrf_protection.php.twig' %}
Expand All @@ -14,8 +15,23 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
class CustomController extends BaseController
{
/**
* Call custom action based on $action parameter
*/
public function customAction($pk, $action)
{
$methodName = ucfirst(strtolower($action));
return $this->$methodName($pk);
}
{% for action in builder.Actions %}
{{- block('action') -}}
{{- block('attemptAction') -}}
{{- block('executeAction') -}}
{{- block('successAction') -}}
{{- block('errorAction') -}}
{% endfor %}
{{- block('csrf_check_token') -}}
Expand All @@ -25,10 +41,11 @@ class CustomController extends BaseController
/**
* Get additional parameters for rendering.
*
* @param \{{ model }} ${{ builder.ModelClass }} your \{{ model }} object
* return array
**/
protected function getAdditionalRenderParameters(\{{ model }} ${{ builder.ModelClass }})
* @param \{{ model }} ${{ builder.ModelClass }} Your \{{ model }} object
* @param string $action Action
* @return array Additional render parameters
*/
protected function getAdditionalRenderParameters(\{{ model }} ${{ builder.ModelClass }}, $action)
{
return array();
}
Expand Down
98 changes: 98 additions & 0 deletions Resources/templates/CommonAdmin/CustomAction/action.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{% use '../CommonAdmin/security_action.php.twig' %}
{% use '../CommonAdmin/csrf_protection.php.twig' %}
{% block attemptAction %}
/**
* This function handles common actions behaviour like
* checking CSRF protection token or credentials.
*
* To customize your action look into:
* execute{{ action.name|capitalize }}() - holds action logic
* success{{ action.name|capitalize }}() - called if action was successfull
* error{{ action.name|capitalize }}() - called if action errored
*/
public function attempt{{ action.name|capitalize }}($pk)
{
try {
${{ builder.ModelClass }} = $this->getObject($pk);

{{ block('security_action_with_object') }}

{% if action.csrfProtected %}
if ('POST' == $this->get('request')->getMethod()) {
{{ block('csrf_action_check_token') }}
{% endif %}
$this->execute{{ action.name|capitalize }}(${{ builder.ModelClass }});

return $this->success{{ action.name|capitalize }}(${{ builder.ModelClass }});
{% if action.csrfProtected %}
}
{% endif %}
} catch (\Exception $e) {
return $this->error{{ action.name|capitalize }}(${{ builder.ModelClass }}, $e);
}

return $this->render('{{ namespace_prefix }}{{ bundle_name }}:{{ builder.BaseGeneratorName }}Custom:index.html.twig', $this->getAdditionalRenderParameters(${{ builder.ModelClass }}) + array(
"{{ builder.ModelClass }}" => ${{ builder.ModelClass }},
"title" => $this->get('translator')->trans(
"{{ action.options.title|default("action.options.title") }}",
array('%name%' => '{{ action.name }}'),
'{{ action.options.i18n|default('Admingenerator') }}'
)
));
}
{% endblock %}

{% block executeAction %}
/**
* This function is for you to customize what action actually does
*/
public function execute{{ action.name|capitalize }}(${{ builder.ModelClass }})
{
// By default action does nothing
// Overwrite this function!
}
{% endblock %}

{% block successAction %}
/**
* This is called when action is successfull
* Default behavior is redirecting to list with success message
*
* @param \{{ model }} ${{ builder.ModelClass }} Your \{{ model }} object
* @return Response Must return a response!
*/
public function success{{ action.name|capitalize }}(${{ builder.ModelClass }})
{
$this->get('session')->getFlashBag()->add('success',
$this->get('translator')->trans(
"{{ action.options.success|default("action.options.success") }}",
array('%name%' => '{{ action.name }}'),
'{{ action.options.i18n|default('Admingenerator') }}'
));

return new RedirectResponse($this->generateUrl("{{ builder.routePrefixWithSubfolder }}_{{ bundle_name }}{{ builder.BaseGeneratorName ? "_" ~ builder.BaseGeneratorName : "" }}_list"));
}
{% endblock %}

{% block errorAction %}
/**
* This is called when action throws an exception
* Default behavior is redirecting to list with error message
*
* @param \{{ model }} ${{ builder.ModelClass }} Your \{{ model }} object
* @param \Exception $e Exception
* @return Response Must return a response!
*/
public function error{{ action.name|capitalize }}(${{ builder.ModelClass }}, $e)
{
$this->get('session')->getFlashBag()->add('error',
$this->get('translator')->trans(
"{{ action.options.error|default("action.options.error") }}",
array('%name%' => '{{ action.name }}'),
'{{ action.options.i18n|default('Admingenerator') }}'
));

return new RedirectResponse($this->generateUrl("{{ builder.routePrefixWithSubfolder }}_{{ bundle_name }}{{ builder.BaseGeneratorName ? "_" ~ builder.BaseGeneratorName : "" }}_list"));
}
{% endblock %}

41 changes: 0 additions & 41 deletions Resources/templates/CommonAdmin/CustomAction/action.php.twig.twig

This file was deleted.

0 comments on commit b160fe6

Please sign in to comment.