<commit_before><?php

namespace MichaelT\Permy;

use Lang;

/**
 * Lists permissions, updates the language file
 *
 * @package michaeltintiuc/laravel-permy
 * @author <NAME>
 **/
class PermyHandler
{
    private $permissions;
    private $needsUpdate = false;

    public function __construct()
    {
        // Get the initial state of the permissions
        $this->permissions = Lang::get('laravel-permy::permy');

        if( ! is_array($this->permissions))
            $this->permissions = [];
    }

    /**
     * See if we can skip supplied route
     *
     * @param Illuminate\Routing\Route $route
     * @return boolean
     **/
    final private function skip(\Illuminate\Routing\Route $route)
    {
        // Skip routes that don't use controllers
        if ( ! isset($route->getAction()['controller']))
            return true;

        // Skip routes that don't use supplied filters
        return !$this->parseFilters($route);
    }

    /**
     * Parse route and/or controller routes
     *
     * @param Illuminate\Routing\Route $route
     * @return boolean
     **/
    final private function parseFilters(\Illuminate\Routing\Route $route)
    {
        // Get available and route filters
        $available_filters = array_fill_keys((array) \Config::get('laravel-permy::filter_names'), null);

        // We've got route filters
        if (array_intersect_key($available_filters, $route->beforeFilters()))
            return true;

        // Get controller name and method
        list($controller, $method) = explode('@', $route->getActionName());

        try
        {
            // Try to instantiate the controller
            // Throws an exception for controllers whose dependencies are not registered
            $controller_filters = \App::make($controller)->getBeforeFilters();
        }
        catch (\Exception $e)
        {
            return false;
        }

        $max = count($controller_filters);

        for ($i=0; $i < $max; $i++)
        {
            // Check if provided methods are set on the controller
            $key_exists = array_key_exists($controller_filters[$i]['original'], $available_filters);
            $applied_to_method = true;

            if (isset($controller_filters[$i]['options']['only']))
                $applied_to_method = in_array($method, (array) $controller_filters[$i]['options']['only']);

            if (isset($controller_filters[$i]['options']['except']))
                $applied_to_method = !in_array($method, (array) $controller_filters[$i]['options']['except']);

            if ($key_exists && $applied_to_method)
                return true;
        }

        return false;
    }

    /**
     * Get permissions info (pretty name, desc for controllers and their methods)
     * For all registered routes
     *
     * @return array
     **/
    final public function getList()
    {
        foreach (\Route::getRoutes() as $route)
        {
            // See what can be skipped
            if ($this->skip($route))
                continue;

            // Get route's controller and method
            list($controller, $method) = explode('@', $route->getActionName());

            // format controller class name
            $controller = $this->formatControllerName($controller);

            // Check if we're up to date
            $this->update($controller, $method);
        }

        // Alphabetic A-Z sorting<commit_msg>update controller filter logic<commit_after><?php

namespace MichaelT\Permy;

use Lang;

/**
 * Lists permissions, updates the language file
 *
 * @package michaeltintiuc/laravel-permy
 * @author <NAME>
 **/
class PermyHandler
{
    private $permissions;
    private $needsUpdate = false;

    public function __construct()
    {
        // Get the initial state of the permissions
        $this->permissions = Lang::get('laravel-permy::permy');

        if( ! is_array($this->permissions))
            $this->permissions = [];
    }

    /**
     * See if we can skip supplied route
     *
     * @param Illuminate\Routing\Route $route
     * @return boolean
     **/
    final private function skip(\Illuminate\Routing\Route $route)
    {
        // Skip routes that don't use controllers
        if ( ! isset($route->getAction()['controller']))
            return true;

        // Skip routes that don't use supplied filters
        return !$this->parseFilters($route);
    }

    /**
     * Parse route and/or controller routes
     *
     * @param Illuminate\Routing\Route $route
     * @return boolean
     **/
    final private function parseFilters(\Illuminate\Routing\Route $route)
    {
        // Get available and route filters
        $available_filters = array_fill_keys((array) \Config::get('laravel-permy::filter_names'), null);

        // We've got route filters
        if (array_intersect_key($available_filters, $route->beforeFilters()))
            return true;

        // Search for controllers with before filters set
        // Get controller name and method
        list($controller, $method) = explode('@', $route->getActionName());

        try
        {
            // Throws an exception for controllers whose dependencies are not registered
            $controller_filters = \App::make($controller)->getBeforeFilters();
        }
        catch (\Exception $e)
        {
            return false;
        }

        $max = count($controller_filters);

        for ($i=0; $i < $max; $i++)
        {
            // Check if provided methods are set on the controller
            $key_exists = array_key_exists($controller_filters[$i]['original'], $available_filters);
            $applied_to_method = true;

            // No point in further logic if it's not our filter
            if ( ! $key_exists)
                return false;

            // is our method whitelisted or blacklisted?
            if (isset($controller_filters[$i]['options']['only']))
                $applied_to_method = in_array($method, (array) $controller_filters[$i]['options']['only']);
            elseif (isset($controller_filters[$i]['options']['except']))
                $applied_to_method = !in_array($method, (array) $controller_filters[$i]['options']['except']);

            if ($applied_to_method)
                return true;
        }

        return false;
    }

    /**
     * Get permissions info (pretty name, desc for controllers and their methods)
     * For all registered routes
     *
     * @return array
     **/
    final public function getList()
    {
        foreach (\Route::getRoutes() as $route)
        {
            // See what can be skipped
            if ($this->skip($route))
                continue;

            // Get route's controller and method
            list($controller, $method) = explode('@', $route->getActionName());

            // format controller class name
            $controller = $this->formatControllerName($controller);

            // Check if we're up to date
            $this->update($controller, $method);
        }

        // Alphabetic A-Z sorting
{'fraction_non_alphanumeric': 0.12329931972789115, 'fraction_numerical': 0.0002834467120181406, 'mean_word_length': 2.13923487544484, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, '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': '4769047', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1824, 'n_words': 497}