<commit_before><?php
/*
 * This file is part of the Cygnite package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Cygnite\Base\Router\Controller;

use Exception;
use Reflection;
use Cygnite\Foundation\Application as App;
use Cygnite\Base\Router\Router;
use Cygnite\Helpers\Inflector;
use Cygnite\Helpers\Config;
use Cygnite\Exception\Http\HttpNotFoundException;

if (!defined('CF_SYSTEM')) {
    exit('No External script access allowed');
}

/**
 * Trait RouteController
 *
 * @package Cygnite\Base\Router\Controller
 * @author <NAME> <<EMAIL>>
 */

trait RouteControllerTrait
{
    private $controllerWithNS;
    public $method;
    private $controller;

    public $dispatcher;
    public $controllerName;
    public $actionName;
    public $actionParams = [];
    public $app;
    public $route;

    /**
     * @return array
     */
    public function getUrlSegments()
    {
        $newUrl = str_replace('/index.php', '', rtrim($this->getCurrentUri()));
        return array_filter(explode('/', $newUrl));
    }

    /**
     * @param $exp
     * @return array
     */
    private function setControllerDirectoryConfig($exp)
    {
        return [ucfirst($exp[1]), $exp[2], $exp[3], array_slice($exp, 3)];
    }

    /**
     * @param $arguments
     * @return object
     * @throws \Exception
     */
    public function callController($arguments)
    {
        $params = [];

        /*
         | Check if whether user trying to access module
         | If module we will setup module configuration
         | or else setup default MVC configurations
         */
        if (string_has($arguments[0], '::')) {
            $exp = string_split($arguments[0], '::');
            $this->setModuleConfiguration($exp);
        } else {
            $this->setUpControllerAndMethodName($arguments);
        }

        if (isset($arguments[1])) {
            $params = $arguments[1];
        }

        $file = CYGNITE_BASE . str_replace('\\', DS, '\\src'.$this->controllerWithNS) . EXT;

        if (!is_readable($file)) {
            throw new \Exception("Route class " . $this->controllerWithNS . " not found. ");
        }

        $args = [];
        $args = (!is_array($params)) ? [$params] : $params;

        /*
         | Get the instance of controller from Cygnite Container
         | and inject all dependencies into controller dynamically.
         | It's cool! You can write powerful rest api using restful
         | routing
         */
        return $this->handleControllerDependencies($this->controllerWithNS, $this->method, $args);
    }

    /**
     * Set controller and method name here
     *
     * @param $arguments
     */
    private function setUpControllerAndMethodName($arguments)
    {
        $expression = string_split($arguments[0], '@');
        $this->setControllerConfig($arguments, $expression);
    }

    /**
     * @param $args
     */
    private function setModuleConfiguration($args)
    {
        $param = string_split($args[1], '@');
        $this->bootstrapModule($args[0]);
        $this->setControllerConfig($args, $param, true);
    }

    /**
     * @param $module
     * @return bool
     */
    public function bootstrapModule($module)
    {
        $config = Config::get('module');
        $modulePath = $config['module.path'].DS;
        $moduleConfigDir = $config['module.config'].DS;
        $class = '\\'.APP_NS.'\\'.$this->getModuleDir().'\\'.ucfirst($module).'\\BootStrap';

        $file = $modulePath.ucfirst($module).DS.$moduleConfigDir.strtolower($module).EXT;
        if (!file_exists($file)) {
            return false;
        }

        Config::setConfigurationItems(strtolower($module).'.config', include $file);
        return (new $class)->register($this->getApplication(), $file);
    }

    /**
     * @return mixed
     */
    public function getModuleDir()
    {
        return isset(static::$moduleDir) ? static::$moduleDir : static::MODULE_DIR;
    }

    /**
     * @param      $args
     * @param      $param
     * @param bool $module
     */
    private function setControllerConfig($args, $param, $module = false)
    {
        $this->controller = Inflector::classify($param[0]) . 'Controller';

        if ($module) {
            $this->namespace = '\\' . $this->getModuleDir() . '\\' . $args[0] . '\\Controllers\\';
        }
        $this->controllerWithNS = "\\" . str_replace('src/', '', APPPATH) . $this->namespace . $this->controller;
        $this->method = Inflector::camelize($param[1]) . 'Action';
    }

    /**
     * Get Application instance
     *
     * @return App
     */
    private function getApplication()
    {
        return App::instance();
    }

    /**<commit_msg>[2.0][Router-Module] Fixed Config::setConfigurationItems() method not found exception<commit_after><?php
/*
 * This file is part of the Cygnite package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Cygnite\Base\Router\Controller;

use Exception;
use Reflection;
use Cygnite\Foundation\Application as App;
use Cygnite\Base\Router\Router;
use Cygnite\Helpers\Inflector;
use Cygnite\Helpers\Config;
use Cygnite\Exception\Http\HttpNotFoundException;

if (!defined('CF_SYSTEM')) {
    exit('No External script access allowed');
}

/**
 * Trait RouteController
 *
 * @package Cygnite\Base\Router\Controller
 * @author <NAME> <<EMAIL>>
 */

trait RouteControllerTrait
{
    private $controllerWithNS;
    public $method;
    private $controller;

    public $dispatcher;
    public $controllerName;
    public $actionName;
    public $actionParams = [];
    public $app;
    public $route;

    /**
     * @return array
     */
    public function getUrlSegments()
    {
        $newUrl = str_replace('/index.php', '', rtrim($this->getCurrentUri()));
        return array_filter(explode('/', $newUrl));
    }

    /**
     * @param $exp
     * @return array
     */
    private function setControllerDirectoryConfig($exp)
    {
        return [ucfirst($exp[1]), $exp[2], $exp[3], array_slice($exp, 3)];
    }

    /**
     * @param $arguments
     * @return object
     * @throws \Exception
     */
    public function callController($arguments)
    {
        $params = [];

        /*
         | Check if whether user trying to access module
         | If module we will setup module configuration
         | or else setup default MVC configurations
         */
        if (string_has($arguments[0], '::')) {
            $exp = string_split($arguments[0], '::');
            $this->setModuleConfiguration($exp);
        } else {
            $this->setUpControllerAndMethodName($arguments);
        }

        if (isset($arguments[1])) {
            $params = $arguments[1];
        }

        $file = CYGNITE_BASE . str_replace('\\', DS, '\\src'.$this->controllerWithNS) . EXT;

        if (!is_readable($file)) {
            throw new \Exception("Route class " . $this->controllerWithNS . " not found. ");
        }

        $args = [];
        $args = (!is_array($params)) ? [$params] : $params;

        /*
         | Get the instance of controller from Cygnite Container
         | and inject all dependencies into controller dynamically.
         | It's cool! You can write powerful rest api using restful
         | routing
         */
        return $this->handleControllerDependencies($this->controllerWithNS, $this->method, $args);
    }

    /**
     * Set controller and method name here
     *
     * @param $arguments
     */
    private function setUpControllerAndMethodName($arguments)
    {
        $expression = string_split($arguments[0], '@');
        $this->setControllerConfig($arguments, $expression);
    }

    /**
     * @param $args
     */
    private function setModuleConfiguration($args)
    {
        $param = string_split($args[1], '@');
        $this->bootstrapModule($args[0]);
        $this->setControllerConfig($args, $param, true);
    }

    /**
     * @param $module
     * @return bool
     */
    public function bootstrapModule($module)
    {
        $config = Config::get('module');
        $modulePath = $config['module.path'].DS;
        $moduleConfigDir = $config['module.config'].DS;
        $class = '\\'.APP_NS.'\\'.$this->getModuleDir().'\\'.ucfirst($module).'\\BootStrap';

        $file = $modulePath.ucfirst($module).DS.$moduleConfigDir.strtolower($module).EXT;
        if (!file_exists($file)) {
            return false;
        }

        Config::set(strtolower($module).'.config', include $file);
        return (new $class)->register($this->getApplication(), $file);
    }

    /**
     * @return mixed
     */
    public function getModuleDir()
    {
        return isset(static::$moduleDir) ? static::$moduleDir : static::MODULE_DIR;
    }

    /**
     * @param      $args
     * @param      $param
     * @param bool $module
     */
    private function setControllerConfig($args, $param, $module = false)
    {
        $this->controller = Inflector::classify($param[0]) . 'Controller';

        if ($module) {
            $this->namespace = '\\' . $this->getModuleDir() . '\\' . $args[0] . '\\Controllers\\';
        }
        $this->controllerWithNS = "\\" . str_replace('src/', '', APPPATH) . $this->namespace . $this->controller;
        $this->method = Inflector::camelize($param[1]) . 'Action';
    }

    /**
     * Get Application instance
     *
     * @return App
     */
    public function getApplication()
    {
        return App::instance();
    }

    /**
{'fraction_non_alphanumeric': 0.15269864290894022, 'fraction_numerical': 0.0031078421216202217, 'mean_word_length': 2.3861802876183793, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 57, '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': '3748171', 'n_tokens_mistral': 2805, 'n_tokens_neox': 2764, 'n_words': 610}