<commit_before><?php

namespace Silber\Bouncer\Middleware;

use Illuminate\Routing\ControllerMiddlewareOptions;

trait AuthorizesResources
{
    /**
     * Authorize a resource action.
     *
     * @param  string  $name
     * @param  string  $model
     * @param  array  $options
     * @param  \Illuminate\Http\Request  $request
     * @return null|\Illuminate\Routing\ControllerMiddlewareOptions
     */
    public function authorizeResource($name, $model, array $options = [], $request = null)
    {
        $request = $request ?: request();

        $method = array_last(explode('@', $request->route()->getActionName()));

        $map = [
            'index' => 'view', 'create' => 'create', 'store' => 'create', 'show' => 'view',
            'edit' => 'update', 'update' => 'update', 'delete' => 'delete',
        ];

        if (property_exists($this, 'abilityMap')) {
            $map = array_merge($map, $this->abilityMap);
        }

        if ( ! in_array($method, array_keys($map))) {
            if (class_exists(ControllerMiddlewareOptions::class)) {
                return new ControllerMiddlewareOptions($options);
            }

            return null;
        }

        $model = in_array($method, ['index', 'create', 'store']) ? $model : $name;

        return $this->middleware("can:{$map[$method]},{$model}");
    }
}<commit_msg>Fix for Laravel 5.1<commit_after><?php

namespace Silber\Bouncer\Middleware;

use Illuminate\Routing\ControllerMiddlewareOptions;

trait AuthorizesResources
{
    /**
     * Authorize a resource action.
     *
     * @param  string  $name
     * @param  string  $model
     * @param  array  $options
     * @param  \Illuminate\Http\Request  $request
     * @return null|\Illuminate\Routing\ControllerMiddlewareOptions
     */
    public function authorizeResource($name, $model, array $options = [], $request = null)
    {
        $request = $request ?: request();

        $method = array_get(explode('@', $request->route()->getActionName()), 1);

        $map = [
            'index' => 'view', 'create' => 'create', 'store' => 'create', 'show' => 'view',
            'edit' => 'update', 'update' => 'update', 'delete' => 'delete',
        ];

        if (property_exists($this, 'abilityMap')) {
            $map = array_merge($map, $this->abilityMap);
        }

        if ( ! in_array($method, array_keys($map))) {
            if (class_exists(ControllerMiddlewareOptions::class)) {
                return new ControllerMiddlewareOptions($options);
            }

            return null;
        }

        $model = in_array($method, ['index', 'create', 'store']) ? $model : $name;

        return $this->middleware("can:{$map[$method]},{$model}");
    }
}
{'fraction_non_alphanumeric': 0.16568483063328424, 'fraction_numerical': 0.0011045655375552283, 'mean_word_length': 2.5656167979002626, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5700283', 'n_tokens_mistral': 789, 'n_tokens_neox': 797, 'n_words': 162}