<commit_before><?php
/**
 * Sija Framework.
 * 
 * @package Sija
 * @author <NAME> <<EMAIL>>
 */

namespace Sija;

use Sija\Common\Application, Sija\Common\Config, Sija\Common\Request, Sija\Common\ParametersList, Sija\Common\ResponseBuilder, ActiveRecord, Exception;

/**
 * Init general autoload class.
 * 
 * @param string $class_name
 */
function sija_autoloader($class_name) {
    if (strpos($class_name, '\\') !== false) {
        $namespaces = explode('\\', $class_name);
        $class_name = array_pop($namespaces);
    }
    if (isset($namespaces)) {
        $class_name = implode($namespaces, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $class_name;
    }
    $filename = __DIR__ . "/../$class_name.php";
    if (file_exists($filename)) {
        require_once($filename);
    }
}
spl_autoload_register('Sija\sija_autoloader');

/**
 * Sija general class.
 */

class Sija {

    /**
     * General executor.
     *
     * @param array $options
     * @return string
     */
    public function execute($options = array()) {

        // Init sessions.
        session_start();

        // Apply application config.
        Application::$config = new Config(isset($options['config']) && is_array($options['config']) ? $options['config'] : null);

       // Init debug mode.
        error_reporting(Application::$config->debug->bool ? E_ALL : 0);

        // Init Active Record.
        ActiveRecord\Config::initialize(function($cfg)
        {
            $cfg->set_connections(Application::$config->connections->value);
            $cfg->set_default_connection(Application::$config->connection->string);
            if (!Application::$config->directories->isEmpty() && isset(Application::$config->directories->value['models'])) {
                $base_dir = isset(Application::$config->directories->value['base']) ? Application::$config->directories->value['base'] : __DIR__;
                $cfg->set_model_directory(str_replace('{{base}}', $base_dir, Application::$config->directories->value['models']));
            }
        });

        // Parse only AJAX requests.
        if(Application::$config->ajax_only->bool && (
            !isset($_SERVER['HTTP_X_REQUESTED_WITH']) ||
            empty($_SERVER['HTTP_X_REQUESTED_WITH']) ||
            strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')
        ) {
            header('HTTP/1.1 500 Internal server error');
            $response_obj = ResponseBuilder::create(500, "This API allow only AJAX requests.", $_SERVER['HTTP_ACCEPT']);
            return $response_obj->render();
        }

        // Parse incoming request info.
        $request = new Request();

        // Parse request method & parameters
        $request->method = strtoupper(isset($options['method']) ? $options['method'] : $_SERVER['REQUEST_METHOD']);
        if (isset($options['parameters']) && is_array($options['parameters'])) {
            $request->parameters = new ParametersList($options['parameters']);
        } else {
            if (!isset($options['method'])) {
                switch ($request->method) {
                    case 'GET': $request->parameters = new ParametersList($_GET); break;
                    case 'POST': $request->parameters = new ParametersList($_POST); break;
                    default: $request->parameters = new ParametersList(); break;
                }
            } else {
                $request->parameters = new ParametersList();
            }
        }

        // Parse routes settings.
        if (isset($options['path']) || isset($_SERVER['PATH_INFO'])) {
            $path = trim(isset($options['path']) ? $options['path'] : $_SERVER['PATH_INFO'], '/');
            if (!Application::$config->routes->isEmpty()  && is_array(Application::$config->routes->value)) {
                $routes = Application::$config->routes->value;
                if (isset($routes['general']) && is_array($routes['general'])) {
                    foreach($routes['general'] as $key => $route) {
                        $path = preg_replace($key, $route, $path);
                    }
                }
                if (isset($routes[strtolower($request->method)]) && is_array($routes[strtolower($request->method)])) {
                    foreach($routes[strtolower($request->method)] as $key => $route) {
                        $path = preg_replace($key, $route, $path);
                    }
                }
            }
            $path_elements = explode('?', $path);
            if (count($path_elements) > 0) {
                $path = $path_elements[0];
                if (count($path_elements) > 1) {
                    $path_parameters = explode('&', $path_elements[1]);
                    foreach ($path_parameters as $path_parameter) {
                        $path_parameter_pair = explode('=', $path_parameter);
                        switch (count($path_parameter_pair)) {
                            case 1: $request->parameters->add($path_parameter_pair[0]); break;
                            case 2: $request->parameters->add($path_parameter_pair[0], $path_parameter_pair[1]); break;
                        }
                    }
                }

            }

            $request->url_elements = explode('/', trim($path, '/'));
        }

        // Parse incoming data.
        if (isset($options['json'])) {
            $request->json = is_object($options['json']) ? $options['json'] : json_decode($options['json']);
        } else {
            $request_data = file_get_contents('php://input');
            $request->json = json_decode($request_data);
        }

        // Route the request.
        if (!empty($request->url_elements) && !empty($request->url_elements[0])) {
            $controller_name = ucfirst($request->url_elements[0]);
            $controller_classname = ($this->__attachController($controller_name) ? $controller_name : 'Sija\\Controllers\\' . $controller_name) . 'Controller';
            if (class_exists($controller_classname)) {
                $controller = new $controller_classname;
                $action_name = strtolower($request->method);
                try {
                    $response_status = 200;
                    $response_data = call_user_func_array(array($controller, $action_name), array($request));<commit_msg>Updated Sija class: check parent class for controllers.<commit_after><?php
/**
 * Sija Framework.
 * 
 * @package Sija
 * @author <NAME> <<EMAIL>>
 */

namespace Sija;

use Sija\Common\Application, Sija\Common\Config, Sija\Common\Request, Sija\Common\ParametersList, Sija\Common\ResponseBuilder, ActiveRecord, Exception;

/**
 * Init general autoload class.
 * 
 * @param string $class_name
 */
function sija_autoloader($class_name) {
    if (strpos($class_name, '\\') !== false) {
        $namespaces = explode('\\', $class_name);
        $class_name = array_pop($namespaces);
    }
    if (isset($namespaces)) {
        $class_name = implode($namespaces, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $class_name;
    }
    $filename = __DIR__ . "/../$class_name.php";
    if (file_exists($filename)) {
        require_once($filename);
    }
}
spl_autoload_register('Sija\sija_autoloader');

/**
 * Sija general class.
 */

class Sija {

    /**
     * General executor.
     *
     * @param array $options
     * @return string
     */
    public function execute($options = array()) {

        // Init sessions.
        session_start();

        // Apply application config.
        Application::$config = new Config(isset($options['config']) && is_array($options['config']) ? $options['config'] : null);

       // Init debug mode.
        error_reporting(Application::$config->debug->bool ? E_ALL : 0);

        // Init Active Record.
        ActiveRecord\Config::initialize(function($cfg)
        {
            $cfg->set_connections(Application::$config->connections->value);
            $cfg->set_default_connection(Application::$config->connection->string);
            if (!Application::$config->directories->isEmpty() && isset(Application::$config->directories->value['models'])) {
                $base_dir = isset(Application::$config->directories->value['base']) ? Application::$config->directories->value['base'] : __DIR__;
                $cfg->set_model_directory(str_replace('{{base}}', $base_dir, Application::$config->directories->value['models']));
            }
        });

        // Parse only AJAX requests.
        if(Application::$config->ajax_only->bool && (
            !isset($_SERVER['HTTP_X_REQUESTED_WITH']) ||
            empty($_SERVER['HTTP_X_REQUESTED_WITH']) ||
            strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')
        ) {
            header('HTTP/1.1 500 Internal server error');
            $response_obj = ResponseBuilder::create(500, "This API allow only AJAX requests.", $_SERVER['HTTP_ACCEPT']);
            return $response_obj->render();
        }

        // Parse incoming request info.
        $request = new Request();

        // Parse request method & parameters
        $request->method = strtoupper(isset($options['method']) ? $options['method'] : $_SERVER['REQUEST_METHOD']);
        if (isset($options['parameters']) && is_array($options['parameters'])) {
            $request->parameters = new ParametersList($options['parameters']);
        } else {
            if (!isset($options['method'])) {
                switch ($request->method) {
                    case 'GET': $request->parameters = new ParametersList($_GET); break;
                    case 'POST': $request->parameters = new ParametersList($_POST); break;
                    default: $request->parameters = new ParametersList(); break;
                }
            } else {
                $request->parameters = new ParametersList();
            }
        }

        // Parse routes settings.
        if (isset($options['path']) || isset($_SERVER['PATH_INFO'])) {
            $path = trim(isset($options['path']) ? $options['path'] : $_SERVER['PATH_INFO'], '/');
            if (!Application::$config->routes->isEmpty()  && is_array(Application::$config->routes->value)) {
                $routes = Application::$config->routes->value;
                if (isset($routes['general']) && is_array($routes['general'])) {
                    foreach($routes['general'] as $key => $route) {
                        $path = preg_replace($key, $route, $path);
                    }
                }
                if (isset($routes[strtolower($request->method)]) && is_array($routes[strtolower($request->method)])) {
                    foreach($routes[strtolower($request->method)] as $key => $route) {
                        $path = preg_replace($key, $route, $path);
                    }
                }
            }
            $path_elements = explode('?', $path);
            if (count($path_elements) > 0) {
                $path = $path_elements[0];
                if (count($path_elements) > 1) {
                    $path_parameters = explode('&', $path_elements[1]);
                    foreach ($path_parameters as $path_parameter) {
                        $path_parameter_pair = explode('=', $path_parameter);
                        switch (count($path_parameter_pair)) {
                            case 1: $request->parameters->add($path_parameter_pair[0]); break;
                            case 2: $request->parameters->add($path_parameter_pair[0], $path_parameter_pair[1]); break;
                        }
                    }
                }

            }

            $request->url_elements = explode('/', trim($path, '/'));
        }

        // Parse incoming data.
        if (isset($options['json'])) {
            $request->json = is_object($options['json']) ? $options['json'] : json_decode($options['json']);
        } else {
            $request_data = file_get_contents('php://input');
            $request->json = json_decode($request_data);
        }

        // Route the request.
        if (!empty($request->url_elements) && !empty($request->url_elements[0])) {
            $controller_name = ucfirst($request->url_elements[0]);
            $controller_classname = ($this->__attachController($controller_name) ? $controller_name : 'Sija\\Controllers\\' . $controller_name) . 'Controller';
            $controller_parents = class_parents($controller_classname);
            if (class_exists($controller_classname) && $controller_parents && is_array($controller_parents) && in_array("Sija\\Common\\Controller", $controller_parents)) {
                $controller = new $controller_classname;
                $action_name = strtolower($request->method);
                try {
                    $response_status = 200;
                    $response_data = call_user_func_array(array($controller, $action_name), array($request));
{'fraction_non_alphanumeric': 0.15856418259851737, 'fraction_numerical': 0.003589543503706594, 'mean_word_length': 2.2886836027713624, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 117, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2040056', 'n_tokens_mistral': 3462, 'n_tokens_neox': 3376, 'n_words': 582}