<commit_before><?php

/**
 * Single Entry Router
 *
 * PHP version 5
 *
 * Licensed under The MIT License
 * Redistribution of these files must retain the above copyright notice.
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright 2007-2014, <NAME>
 * @license   http://www.opensource.org/licenses/mit-license.html
 * @package   PICKLES
 * @link      https://github.com/joshtronic/pickles
 */

/**
 * Router Class
 *
 * The heavy lifter of PICKLES, makes the calls to get the session and
 * configuration loaded.  Loads modules, serves up user authentication when the
 * module asks for it, and loads the viewer that the module requested. Default
 * values are present to make things easier on the user.
 *
 * @usage <code>new Router();</code>
 */
class Router extends Object
{
    /**
     * Constructor
     *
     * To save a few keystrokes, the Controller is executed as part of the
     * constructor instead of via a method. You either want the Controller or
     * you don't.
     */
    public function __construct()
    {
        parent::__construct();

        $response = new Response();

        try
        {
            // Grabs the requested page
            $request    = $_REQUEST['request'];
            $components = explode('/', $request);
            $version    = array_shift($components);
            $nouns      = [];
            $uids       = [];

            // Loops through the components to determine nouns and IDs
            foreach ($components as $index => $component)
            {
                if ($index % 2)
                {
                    $uids[end($nouns)] = $component;
                }
                else
                {
                    $nouns[] = $component;
                }
            }

            // Creates our class name
            array_unshift($nouns, $version);
            $class = implode('_', $nouns);

            // Creates our filename
            array_unshift($nouns, SITE_MODULE_PATH);
            $filename = implode('/', $nouns) . '.php';

            if (!file_exists($filename))
            {
                throw new Exception('Cannot find the file ' . $filename);
            }

            if (!class_exists($class))
            {
                throw new Exception('Cannot find the class ' . $class);
            }

            $resource = new $class($uids);

            // Determines if we need to serve over HTTP or HTTPS
            if ($resource->secure == false && isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'])
            {
                throw new Exception('This resource expects HTTPS communication.');
            }
            elseif ($resource->secure == true && (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == false))
            {
                throw new Exception('This resource expects HTTP communication.');
            }

            // Gets the profiler status
            $profiler = $this->config->pickles['profiler'];
            $profiler = $profiler === true || stripos($profiler, 'timers') !== false;

            $method = strtolower($_SERVER['REQUEST_METHOD']);

            if (!method_exists($resource, $method))
            {
                throw new Exception('Cannot find the method ' . $class . '::' . $method);
            }

            // Starts a timer before the resource is executed
            if ($profiler)
            {
                Profiler::timer('resource ' . $method);
            }

            if ($resource->validate)
            {
                $validation_errors = $resource->__validate();

                if ($validation_errors)
                {
                    $response->status  = 400;
                    $response->message = implode(' ', $validation_errors);
                }
            }

            if ($response->status == 200)
            {
                $resource_return = $resource->$method();

                if ($resource_return)
                {
                    $response->response = $resource_return;
                }
            }

            // Stops the resource timer
            if ($profiler)
            {
                Profiler::timer('resource ' . $method);
            }
        }
        catch (Exception $e)
        {
            $response->status  = 500;
            $response->message = $e->getMessage();
        }

        $response->respond();
    }
}

<commit_msg>Put in a note about 404s<commit_after><?php

/**
 * Single Entry Router
 *
 * PHP version 5
 *
 * Licensed under The MIT License
 * Redistribution of these files must retain the above copyright notice.
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright 2007-2014, <NAME>
 * @license   http://www.opensource.org/licenses/mit-license.html
 * @package   PICKLES
 * @link      https://github.com/joshtronic/pickles
 */

/**
 * Router Class
 *
 * The heavy lifter of PICKLES, makes the calls to get the session and
 * configuration loaded.  Loads modules, serves up user authentication when the
 * module asks for it, and loads the viewer that the module requested. Default
 * values are present to make things easier on the user.
 *
 * @usage <code>new Router();</code>
 */
class Router extends Object
{
    /**
     * Constructor
     *
     * To save a few keystrokes, the Controller is executed as part of the
     * constructor instead of via a method. You either want the Controller or
     * you don't.
     */
    public function __construct()
    {
        parent::__construct();

        $response = new Response();

        try
        {
            // Grabs the requested page
            $request    = $_REQUEST['request'];
            $components = explode('/', $request);
            $version    = array_shift($components);
            $nouns      = [];
            $uids       = [];

            // Loops through the components to determine nouns and IDs
            foreach ($components as $index => $component)
            {
                if ($index % 2)
                {
                    $uids[end($nouns)] = $component;
                }
                else
                {
                    $nouns[] = $component;
                }
            }

            // Creates our class name
            array_unshift($nouns, $version);
            $class = implode('_', $nouns);

            // Creates our filename
            array_unshift($nouns, SITE_MODULE_PATH);
            $filename = implode('/', $nouns) . '.php';

            if (!file_exists($filename))
            {
                // @todo Should be a 404, will need to change it up after I add
                //       namespaces and a Pickles\Exception
                throw new Exception('Cannot find the file ' . $filename);
            }

            if (!class_exists($class))
            {
                throw new Exception('Cannot find the class ' . $class);
            }

            $resource = new $class($uids);

            // Determines if we need to serve over HTTP or HTTPS
            if ($resource->secure == false && isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'])
            {
                throw new Exception('This resource expects HTTPS communication.');
            }
            elseif ($resource->secure == true && (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == false))
            {
                throw new Exception('This resource expects HTTP communication.');
            }

            // Gets the profiler status
            $profiler = $this->config->pickles['profiler'];
            $profiler = $profiler === true || stripos($profiler, 'timers') !== false;

            $method = strtolower($_SERVER['REQUEST_METHOD']);

            if (!method_exists($resource, $method))
            {
                throw new Exception('Cannot find the method ' . $class . '::' . $method);
            }

            // Starts a timer before the resource is executed
            if ($profiler)
            {
                Profiler::timer('resource ' . $method);
            }

            if ($resource->validate)
            {
                $validation_errors = $resource->__validate();

                if ($validation_errors)
                {
                    $response->status  = 400;
                    $response->message = implode(' ', $validation_errors);
                }
            }

            if ($response->status == 200)
            {
                $resource_return = $resource->$method();

                if ($resource_return)
                {
                    $response->response = $resource_return;
                }
            }

            // Stops the resource timer
            if ($profiler)
            {
                Profiler::timer('resource ' . $method);
            }
        }
        catch (Exception $e)
        {
            $response->status  = 500;
            $response->message = $e->getMessage();
        }

        $response->respond();
    }
}


{'fraction_non_alphanumeric': 0.11657130034780658, 'fraction_numerical': 0.004936609446875351, 'mean_word_length': 1.4685682636388813, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 47, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '884237', 'n_tokens_mistral': 2305, 'n_tokens_neox': 2187, 'n_words': 639}