<commit_before><?php
/**
 * For the full copyright and license information, please view the LICENSE file
 * that was distributed with this source code.
 *
 * @copyright ©Talus, Talus' Works 2010+
 * @link http://www.talus-works.net Talus' Works
 * @license http://creativecommons.org/licenses/by-sa/3.0/ CC-BY-SA 3.0+
 * @version $Id$
 */

namespace Controller;

/**
 * Main Controller
 *
 * This is the Entry Point, choosing which controller we'll be using, and how
 * to make it all work. It appends every needed things, like how to get a correct
 * header and a correct footer.
 *
 * To make it simple, this is the basis of all the sub-controllers ; in order to
 * make them work, they MUST extend this class.
 *
 * @package twk.controller
 * @author <NAME>" Clavié <<EMAIL>>
 */
abstract class Front {
  protected
    $_template = null,
    $_status = \Http\Response::OK,

    /**
     * View Engine
     *
     * @var \View\Bridge
     */
    $_view = null,

    /**
     * @var \Http\Request
     */
    $_request = null,

    /**
     * @var \Http\Response
     */
    $_response = null,

    /**
     * @var \Routing\Route
     */
    $_route = null;


  /**
   * Starts and render the frame
   *
   * @param \Http\Request $_request HTTP Request handler
   * @param \Http\Response $_response HTTP Response handler
   * @param \View\Bridge $_view View engine
   */
  final protected function  __construct(\Http\Request $_request, \Http\Response $_response, \View\Bridge $_view) {
    \Obj::$controller = $this;

    $this->_request = $_request;
    $this->_response = $_response;
    $this->_view = $this->_response->view($_view);
    $this->_route = \Obj::$router->route($_request);

    \Debug::info('Loading main content');
    $this->_main();

    \Debug::info('Sending the response to the user');
    $this->_response->status($this->_status);
    echo $this->_response->render($this->_template);
  }

  /**
   * Will be runned before the treatment
   *
   * If it has to be overridden, don't forget to call __this__ method !
   * <code>protected function _prepend() { parent::_prepend(); }</code>
   * ... Or at least, do the gist of what it is doing.
   *
   * @return void
   */
  protected function _prepend() {}

  /**
   * Will be runned after the treatment
   * If it has to be overridden, don't forget to call __this__ method !
   * <code>protected function _append() { parent::_append(); }</code>
   * ... Or at least, do the gist of what it is doing.
   *
   * @return void
   */
  protected function _append() {}

  /**
   * Main function
   *
   * @return bool True if it si correct, false otherwise
   */
  final private function _main() {
    if (!\method_exists($this, $this->_route->action)) {
      \Debug::warning('Action %1$s nonexistent', $this->_route->action);
      $this->_response->redirect404('/error/notfound_404');
      $this->_status = \Http\Response::NOT_FOUND;

      exit;
    }

    \Debug::info('Loading action "%1$s"', $this->_route->action);
    $this->_prepend();
    $this->{$this->_route->action}();
    $this->_append();
  }

  /**
   * Prepare the page, matching a route if any
   *
   * @param self $_controller Controller to be used. null if it has to be guessed.
   * @param array $options Options array (to define new http handlers, view bridges, ...)
   * @return self Matched controller
   */
  final public static function getController(self $_controller = null, array $_options = array()) {
    if ($_controller !== null) {
      return $_controller;
    }

    $options = \array_replace(array(
      'request' => new \Http\Request,
      'response' => new \Http\Response,
      'view' => new \View\PHP
     ), $_options);

    \Debug::info('Routing');
    $requestURI = $options['request']->requestUri();

    $route = \Obj::$router->route($requestURI['URI']);

    if (!isset($options['apploader'])) {
      $options['apploader'] = function($controller) {
        // -- Verifying that this is a controller
        $parts = \explode('\\', $controller);

        // -- controllers must have a \Controller\Sub\MyController form
        if (\count($parts) === 3) {
          if ($parts[0] !== 'Controller' || $parts[1] !== 'Sub' || !\is_scalar($parts[2])) {
            return false;
          }

          $controller = \mb_convert_case($parts[2], \MB_CASE_LOWER);
          $file = \sprintf('%1$s/../../apps/%2$s/controller.%3$s', __DIR__, $controller, \PHP_EXT);

          if (\is_file($file)) {
            require $file;
            return true;
          }
        }

        return false;
       };
    }

    spl_autoload_register($options['apploader']);

    $_controller = \mb_convert_case($route->controller, \MB_CASE_TITLE);
    $_controller = '\Controller\Sub\\' . $_controller;

    \Debug::info('Trying to start the subcontroller %1$s', $_controller);
    return new $_controller($options['request'], $options['response'], $options['view']);
  }

  /**
   * Forwards to a new controller and a new action
   *
   * @param type $_controller Controller's name
   * @param type $_action Action's name
   * @todo Well... Todo.
   */
  final protected function forward($_controller, $_action) {

  }

  /**
   * Sets a variable $var for the view
   *
   * @param string $var Variable's name
   * @param mixed $val  Variable's value
   * @return void<commit_msg>Allow the framework to use a fallback autoloader when autoloading controllers (draft)<commit_after><?php
/**
 * For the full copyright and license information, please view the LICENSE file
 * that was distributed with this source code.
 *
 * @copyright ©Talus, Talus' Works 2010+
 * @link http://www.talus-works.net Talus' Works
 * @license http://creativecommons.org/licenses/by-sa/3.0/ CC-BY-SA 3.0+
 * @version $Id$
 */

namespace Controller;

/**
 * Main Controller
 *
 * This is the Entry Point, choosing which controller we'll be using, and how
 * to make it all work. It appends every needed things, like how to get a correct
 * header and a correct footer.
 *
 * To make it simple, this is the basis of all the sub-controllers ; in order to
 * make them work, they MUST extend this class.
 *
 * @package twk.controller
 * @author Baptiste "Talus" Clavié <<EMAIL>>
 */
abstract class Front {
  protected
    $_template = null,
    $_status = \Http\Response::OK,

    /**
     * View Engine
     *
     * @var \View\Bridge
     */
    $_view = null,

    /**
     * @var \Http\Request
     */
    $_request = null,

    /**
     * @var \Http\Response
     */
    $_response = null,

    /**
     * @var \Routing\Route
     */
    $_route = null;


  /**
   * Starts and render the frame
   *
   * @param \Http\Request $_request HTTP Request handler
   * @param \Http\Response $_response HTTP Response handler
   * @param \View\Bridge $_view View engine
   */
  final protected function  __construct(\Http\Request $_request, \Http\Response $_response, \View\Bridge $_view) {
    \Obj::$controller = $this;

    $this->_request = $_request;
    $this->_response = $_response;
    $this->_view = $this->_response->view($_view);
    $this->_route = \Obj::$router->route($_request);

    \Debug::info('Loading main content');
    $this->_main();

    \Debug::info('Sending the response to the user');
    $this->_response->status($this->_status);
    echo $this->_response->render($this->_template);
  }

  /**
   * Will be runned before the treatment
   *
   * If it has to be overridden, don't forget to call __this__ method !
   * <code>protected function _prepend() { parent::_prepend(); }</code>
   * ... Or at least, do the gist of what it is doing.
   *
   * @return void
   */
  protected function _prepend() {}

  /**
   * Will be runned after the treatment
   * If it has to be overridden, don't forget to call __this__ method !
   * <code>protected function _append() { parent::_append(); }</code>
   * ... Or at least, do the gist of what it is doing.
   *
   * @return void
   */
  protected function _append() {}

  /**
   * Main function
   *
   * @return bool True if it si correct, false otherwise
   */
  final private function _main() {
    if (!\method_exists($this, $this->_route->action)) {
      \Debug::warning('Action %1$s nonexistent', $this->_route->action);
      $this->_response->redirect404('/error/notfound_404');
      $this->_status = \Http\Response::NOT_FOUND;

      exit;
    }

    \Debug::info('Loading action "%1$s"', $this->_route->action);
    $this->_prepend();
    $this->{$this->_route->action}();
    $this->_append();
  }

  /**
   * Prepare the page, matching a route if any
   *
   * @param self $_controller Controller to be used. null if it has to be guessed.
   * @param array $options Options array (to define new http handlers, view bridges, ...)
   * @return self Matched controller
   */
  final public static function getController(self $_controller = null, array $_options = array()) {
    if ($_controller !== null) {
      return $_controller;
    }

    $options = \array_replace(array(
      'request' => new \Http\Request,
      'response' => new \Http\Response,
      'view' => new \View\PHP
     ), $_options);

    \Debug::info('Routing');
    $requestURI = $options['request']->requestUri();

    $route = \Obj::$router->route($requestURI['URI']);

    if (!isset($options['apploader'])) {
      $options['apploader'] = array();
    } elseif (!is_array($options['apploader'])) {
      $options['apploader'] = array($options['apploader']);
    }

    $options['apploader'][] = function($controller) {
      // -- Verifying that this is a controller : it must have a \Controller\Sub\Name form
      $parts = \explode('\\', $controller);

      if (\count($parts) === 3) {
        if ($parts[0] !== 'Controller' || $parts[1] !== 'Sub' || !\is_scalar($parts[2])) {
          return false;
        }

        $controller = \mb_convert_case($parts[2], \MB_CASE_LOWER);
        $file = \sprintf('%1$s/../../apps/%2$s/controller.%3$s', __DIR__, $controller, \PHP_EXT);

        if (\is_file($file)) {
          require $file;
          return true;
        }
      }

      return false;
     };

    foreach ($options['apploader'] as &$apploader) {
      spl_autoload_register($apploader);
    }

    $_controller = \mb_convert_case($route->controller, \MB_CASE_TITLE);
    $_controller = '\Controller\Sub\\' . $_controller;

    \Debug::info('Trying to start the subcontroller %1$s', $_controller);
    return new $_controller($options['request'], $options['response'], $options['view']);
  }

  /**
   * Forwards to a new controller and a new action
   *
   * @param type $_controller Controller's name
   * @param type $_action Action's name
   * @todo Well... Todo.
   */
  final protected function forward($_controller, $_action) {

  }

  /**
   * Sets a variable $var for the view
   *
   * @param string $var Variable's name
   * @param mixed $val  Variable's value
   * @return void
{'fraction_non_alphanumeric': 0.15530233412975555, 'fraction_numerical': 0.004594743613306377, 'mean_word_length': 2.7670474212530287, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 84, 'https://': 0, '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': '3187907', 'n_tokens_mistral': 3376, 'n_tokens_neox': 3254, 'n_words': 969}