Efficiently\AuthorityController\ControllerResource::addBeforeFilter PHP Method

addBeforeFilter() public static method

public static addBeforeFilter ( $controller, $method, $args )
    public static function addBeforeFilter($controller, $method, $args)
    {
        $method = last(explode('::', $method));
        $resourceName = array_key_exists(0, $args) ? snake_case(array_shift($args)) : null;
        $lastArg = last($args);
        if (is_array($lastArg)) {
            $args = array_merge($args, array_extract_options($lastArg));
        }
        $options = array_extract_options($args);
        if (array_key_exists('prepend', $options) && $options['prepend'] === true) {
            $beforeFilterMethod = "prependBeforeFilter";
            unset($options['prepend']);
        } else {
            $beforeFilterMethod = "beforeFilter";
        }
        $resourceOptions = array_except($options, ['only', 'except']);
        $filterPrefix = "router.filter: ";
        $filterName = "controller." . $method . "." . get_classname($controller) . "(" . md5(json_encode($args)) . ")";
        if (!Event::hasListeners($filterPrefix . $filterName)) {
            Event::listen($filterPrefix . $filterName, function () use($controller, $method, $resourceOptions, $resourceName) {
                $controllerResource = app('Efficiently\\AuthorityController\\ControllerResource', [$controller, $resourceName, $resourceOptions]);
                $controllerResource->{$method}();
            });
            call_user_func_array([$controller, $beforeFilterMethod], [$filterName, array_only($options, ['only', 'except'])]);
        }
    }

Usage Example

 /**
  * Sets up a before filter which authorizes the resource using the instance variable.
  * For example, if you have an ArticlesController it will check the $this->article instance variable
  * and ensure the user can perform the current action on it. Under the hood it is doing
  * something like the following.
  *
  *   $this->authorize($this->params['action'], $this->article ?: 'Article')
  *
  * Call this method directly on the controller class.
  *
  *   class BooksController extends Controller
  *   {
  *       public function __construct()
  *       {
  *           $this->authorizeResource();
  *       }
  *   }
  *
  * If you pass in the name of a resource which does not match the controller it will assume
  * it is a parent resource.
  *
  *   class BooksController extends Controller
  *   {
  *       public function __construct()
  *       {
  *           $this->authorizeResource('author');
  *           $this->authorizeResource('book');
  *       }
  *   }
  *
  * Here it will authorize '<code>show</code>', <code>$this->author</code> on every action before authorizing the book.
  *
  * That first argument is optional and will default to the singular name of the controller.
  * A hash of options (see below) can also be passed to this method to further customize it.
  *
  * See loadAndAuthorizeResource() to automatically load the resource too.
  *
  * Options:
  * ['<code>only</code>']
  *   Only applies before filter to given actions.
  *
  * ['<code>except</code>']
  *   Does not apply before filter to given actions.
  *
  * ['<code>singleton</code>']
  *   Pass <code>true</code> if this is a singleton resource through a <code>hasOne</code> association.
  *
  * ['<code>parent</code>']
  *   True or false depending on if the resource is considered a parent resource. This defaults to <code>true</code> if a resource
  *   name is given which does not match the controller.
  *
  * ['<code>class</code>']
  *   The class to use for the model (string). This passed in when the instance variable is not set.
  *   Pass <code>false</code> if there is no associated class for this resource and it will use a symbol of the resource name.
  *
  * ['<code>instance_name</code>']
  *   The name of the instance variable for this resource.
  *
  * ['<code>through</code>']
  *   Authorize conditions on this parent resource when instance isn't available.
  *
  * ['<code>prepend</code>']
  *   Passing <code>true</code> will use prependBeforeFilter() instead of a normal beforeFilter().
  *
  */
 public function authorizeResource($args = null)
 {
     $args = is_array($args) ? $args : func_get_args();
     ControllerResource::addBeforeFilter($this, __METHOD__, $args);
 }