<commit_before><?php
namespace AmidaMVC\AppSimple;

class Router implements \AmidaMVC\Framework\IModule
{
    /**
     * @var \AmidaMVC\Tools\Route   a static class name for match and scan.
     */
    var $_routeClass = '\AmidaMVC\Tools\Route';
    /**
     * @var array   list of index files when accessing a directory.
     */
    var $_indexes = array( 'index.md', 'index.html', 'index.php' );
    // +-------------------------------------------------------------+
    /**
     * initialize class.
     * @param array $option   options to initialize.
     */
    function _init( $option=array() ) {
        if( isset( $option[ 'routeClass' ] ) ) {
            $this->_routeClass = $option[ 'routeClass' ];
        }
        if( isset( $option[ 'routes' ] ) ) {
            $route = $this->_routeClass;
            $route::set( $option[ 'routes' ] );
        }
        if( isset( $option[ 'indexes' ] ) ) {
            if( is_array( $option[ 'indexes' ] ) ) {
                $this->_indexes = array_merge( $option[ 'indexes' ], $this->_indexes );
            }
            else {
                $this->_indexes[] = $option[ 'indexes' ];
            }
        }
    }
    // +-------------------------------------------------------------+
    /**
     * default is to use route map first, then scan the file system
     * to determine which file to load.
     * @param \AmidaMVC\AppSimple\Application $_ctrl
     * @param \AmidaMVC\Framework\PageObj $_pageObj
     * @param array $option
     * @return array   $loadInfo for Loader.
     */
    function actionDefault( $_ctrl, &$_pageObj, $option=array() )
    {
        $route = $this->_routeClass;
        $path  = $_ctrl->getPathInfo();
        $root  = $_ctrl->getLocation();
        if( $loadInfo = $route::match( $path ) ) {
            // found by route map.
            $loadInfo[ 'foundBy' ] = 'route';
        }
        else if( $loadInfo = $route::scan( $root, $path ) ) {
            // found (something) by scan.
            if( isset( $loadInfo[ 'reload' ] ) ) {
                // reload if it is a directory without trailing slash.
                $_ctrl->redirect( $loadInfo[ 'reload' ] );
            }
            else if( isset( $loadInfo[ 'is_dir' ] ) ) {
                if( $loadInfo = $route::index( $root, $loadInfo[ 'file' ], $this->_indexes ) ) {
                    // found an index file in the directory.
                    $loadInfo[ 'foundBy' ] = 'index';
                }
            }
            else {
                // normal file.
                $loadInfo[ 'foundBy' ] = 'scan';
            }
        }
        if( empty( $loadInfo ) ) {
            $_ctrl->setAction( '_pageNotFound' );
        }
        if( !is_callable( $loadInfo[ 'file' ] ) ) {
            $loadInfo[ 'file' ] = $_ctrl->getLocation( $loadInfo[ 'file' ] );
        }
        return $loadInfo;
    }
    // +-------------------------------------------------------------+
    /**
     * @param \AmidaMVC\AppSimple\Application $_ctrl
     * @param \AmidaMVC\Framework\PageObj $_pageObj
     * @param array $option
     * @return array
     */
    function action_LoginForm( $_ctrl, &$_pageObj, $option=array() )
    {
        $loadInfo = array();<commit_msg>fixed a bug when router cannot find a file, but still get location.<commit_after><?php
namespace AmidaMVC\AppSimple;

class Router implements \AmidaMVC\Framework\IModule
{
    /**
     * @var \AmidaMVC\Tools\Route   a static class name for match and scan.
     */
    var $_routeClass = '\AmidaMVC\Tools\Route';
    /**
     * @var array   list of index files when accessing a directory.
     */
    var $_indexes = array( 'index.md', 'index.html', 'index.php' );
    // +-------------------------------------------------------------+
    /**
     * initialize class.
     * @param array $option   options to initialize.
     */
    function _init( $option=array() ) {
        if( isset( $option[ 'routeClass' ] ) ) {
            $this->_routeClass = $option[ 'routeClass' ];
        }
        if( isset( $option[ 'routes' ] ) ) {
            $route = $this->_routeClass;
            $route::set( $option[ 'routes' ] );
        }
        if( isset( $option[ 'indexes' ] ) ) {
            if( is_array( $option[ 'indexes' ] ) ) {
                $this->_indexes = array_merge( $option[ 'indexes' ], $this->_indexes );
            }
            else {
                $this->_indexes[] = $option[ 'indexes' ];
            }
        }
    }
    // +-------------------------------------------------------------+
    /**
     * default is to use route map first, then scan the file system
     * to determine which file to load.
     * @param \AmidaMVC\AppSimple\Application $_ctrl
     * @param \AmidaMVC\Framework\PageObj $_pageObj
     * @param array $option
     * @return array   $loadInfo for Loader.
     */
    function actionDefault( $_ctrl, &$_pageObj, $option=array() )
    {
        $route = $this->_routeClass;
        $path  = $_ctrl->getPathInfo();
        $root  = $_ctrl->getLocation();
        if( $loadInfo = $route::match( $path ) ) {
            // found by route map.
            $loadInfo[ 'foundBy' ] = 'route';
        }
        else if( $loadInfo = $route::scan( $root, $path ) ) {
            // found (something) by scan.
            if( isset( $loadInfo[ 'reload' ] ) ) {
                // reload if it is a directory without trailing slash.
                $_ctrl->redirect( $loadInfo[ 'reload' ] );
            }
            else if( isset( $loadInfo[ 'is_dir' ] ) ) {
                if( $loadInfo = $route::index( $root, $loadInfo[ 'file' ], $this->_indexes ) ) {
                    // found an index file in the directory.
                    $loadInfo[ 'foundBy' ] = 'index';
                }
            }
            else {
                // normal file.
                $loadInfo[ 'foundBy' ] = 'scan';
            }
        }
        if( empty( $loadInfo ) ) {
            $_ctrl->setAction( '_pageNotFound' );
        }
        else
        if( !is_callable( $loadInfo[ 'file' ] ) ) {
            $loadInfo[ 'file' ] = $_ctrl->getLocation( $loadInfo[ 'file' ] );
        }
        return $loadInfo;
    }
    // +-------------------------------------------------------------+
    /**
     * @param \AmidaMVC\AppSimple\Application $_ctrl
     * @param \AmidaMVC\Framework\PageObj $_pageObj
     * @param array $option
     * @return array
     */
    function action_LoginForm( $_ctrl, &$_pageObj, $option=array() )
    {
        $loadInfo = array();
{'fraction_non_alphanumeric': 0.19812624788818922, 'fraction_numerical': 0.0, 'mean_word_length': 1.4435272045028142, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '106929', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1888, 'n_words': 457}