2017 © Pedro Peláez
 

library before-after-controllers-hooks-bundle

Provides "@BeforeHook" and "@AfterHook" Annotations support for Symfony Controllers

image

dr-benton/before-after-controllers-hooks-bundle

Provides "@BeforeHook" and "@AfterHook" Annotations support for Symfony Controllers

  • Friday, February 13, 2015
  • by DrBenton
  • Repository
  • 1 Watchers
  • 1 Stars
  • 26,230 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 6 % Grown

The README.md

BeforeAfterControllersHooksBundle

build status, (*1)

If you like Silex routes middlewares or Ruby on Rails actions filters, you may appreciate this Bundle, as it mimics this behaviour in Symfony2, thanks to specific Annotations., (*2)

This type of "before" and "after" callbacks around Controllers Actions may not be very "design patterns compliant", but it's pragmatic and allows easy DRY principle application :-), (*3)

With the "@BeforeHook" and "@AfterHook" Annotations, you can trigger methods of the Controller itself, or Symfony Services methods., (*4)

If a "@BeforeHook" callback returns a Response object, the Request handling is short-circuited (the next hooks won't be run, neither the Controller action), and the Response is passed to the "@AfterHook" callback(s) right away - or returned to the client if no @AfterHook is linked to this Controller Action., (*5)

Synopsis

As always, a code sample may be worth a thousand words:, (*6)

<?php

namespace AppBundle\Controller;

use DrBenton\Bundle\BeforeAfterControllersHooksBundle\Annotation\BeforeControllerHook as BeforeHook;
use DrBenton\Bundle\BeforeAfterControllersHooksBundle\Annotation\AfterControllerHook as AfterHook;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
 * Before any of this Controller Action, its 'checkSomething' method
 * will be triggered:
 *
 * @BeforeHook("checkSomething")
 */
class BooksController extends Controller
{
    /**
     * The Controller 'checkBooksAvailability()' method will be triggered
     * before this Controller action:
     *
     * @BeforeHook("checkBooksAvailability")
     * @Template()
     */
    public function indexAction()
    {
        $books = $this->getDoctrine()
                    ->getRepository('AppBundle:Book')
                    ->findAll();

        return ['books' => $books];
    }

    /**
     * You can also send params to the triggered hook:
     *
     * @BeforeHook("doSomethingBeforeAction", args={"param1", "param2"})
     * @Template()
     */
    public function showAction(Book $book)
    {
        return ['book' => $book];
    }

    /**
     * Want to trigger a Symfony Service method? No problem!
     * Just use the "@[serviceId]::[methodName]" notation:
     *
     * @BeforeHook("@logger::addInfo", args={"showComments() will be called"})
     * @Template()
     */
    public function showCommentsAction(Book $book)
    {
        return ['book' => $book];
    }

    /**
     * You can also trigger a custom callable after the Controller action:
     *
     * @AfterHook("addDebugCodeAfterAction")
     * @Template()
     */
    public function showSomethingAction(Book $book)
    {
        return ['book' => $book];
    }

    /**
     * You can use Services here too, and use params. Any "%response%" param
     * will be replaced with the Controller's returned Symfony Reponse.
     *
     * @AfterHook("@my_service::doSomethingAfterAction", args={"%response%", {"key" => "value"}})
     * @Template()
     */
    public function showSomethingAction(Book $book)
    {
        return ['book' => $book];
    }

    protected function checkSomething()
    {
        // Do something here...
        // It this method returns a Symfony Response, the Controller
        // will be short-circuited and this Response will be sent to the client.
    }

    protected function checkBooksAvailability()
    {
        // idem: return a Response here if oy want to short-circuit the Controller
    }

    protected function doSomethingBeforeAction($arg1, $arg2)
    {
        // Do something here...
    }

    protected function addDebugCodeAfterAction(Response $controllerResponse)
    {
        if ($this->container->getParameter('debug')) {
            $controllerResponse->setContent(
                $controllerResponse->getContent() .
                '<script src="//assets/js/debug.js"></script>'
            );
        }
    }
}

Installation

Step 1: Composer

Add the following line to the composer.json file:, (*7)

``` json { "require": { "dr-benton/before-after-controllers-hooks-bundle": "@dev-master" } }, (*8)


Then run: ``` bash $ composer update "dr-benton/before-after-controllers-hooks-bundle"

Step 2: Enable the bundle

Finally, enable the bundle in the kernel:, (*9)

``` php <?php // app/AppKernel.php, (*10)

public function registerBundles() { $bundles = array( // ... new DrBenton\Bundle\BeforeAfterControllersHooksBundle\BeforeAfterControllersHooksBundle(), ); } ```, (*11)

License

Copyright (c) 2014 Olivier Philippon, (*12)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:, (*13)

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*14)

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*15)

The Versions

13/02 2015

v1.0.0

1.0.0.0

Provides "@BeforeHook" and "@AfterHook" Annotations support for Symfony Controllers

  Sources   Download

MIT

The Requires

 

The Development Requires

13/11 2014

dev-master

9999999-dev

Provides "@BeforeHook" and "@AfterHook" Annotations support for Symfony Controllers

  Sources   Download

MIT

The Requires

 

The Development Requires