src/Router.php
protected function generateRoutes()
    {
        $parsingNeeded = !file_exists($this->cacheDir . $this->routesFile);
        // We look for controller files
        $files = $this->findControllerFiles();

        // We check if there has been modifications since last cache generation
        if (!$parsingNeeded) {
            $routesCacheMtime = filemtime($this->cacheDir . $this->routesFile);
            foreach ($files as $file => $mtime) {
                if ($mtime > $routesCacheMtime) {
                    $parsingNeeded = true;
                    break;
                }
            }
        }

        // We look for deleted controller files
        if (!$parsingNeeded and file_exists($this->cacheDir . $this->controllersFile)) {
            include_once $this->cacheDir . $this->controllersFile;
            foreach ($this->usedControllers as $controllerFile) {
                if (!file_exists($controllerFile)) {
                    $parsingNeeded = true;
                    break;
                }
            }
        }

        $routes = [];
        foreach ($files as $file => $mtime) {
            $parseFile = $this->parseFile($file);
            if (!empty($parseFile)) {
                $routes = array_merge($routes, $parseFile);
            }
        }

        if (!empty($routes)) {
            usort(
                $routes,
                function ($a, $b) {
                    if (strlen($a['routePath']) === strlen($b['routePath'])) {
                        return 0;
                    }
                    return strcmp($b['routePath'],
                        $a['routePath']) ?: strlen($b['routePath']) - strlen($a['routePath']);
                }
            );

            // We regenerate cache file if needed
            if ($parsingNeeded) {
                $controllerFiles = [];
                $commonFileContent = '<?php' . "\r\n" . '/**' . "\r\n" . ' * annotations router %s cache file, create ' . date('c') . "\r\n" . ' */' . "\r\n\r\n";
                $routesFileContent = sprintf($commonFileContent, 'routes');
                $controllersFileContent = sprintf($commonFileContent, 'controllers');
                $routesFileContent .= 'return [';

                foreach ($routes as $key => $route) {
                    $routesFileContent .= "\r\n";
                    $routesFileContent .= "    '" . $route['routeName'] . "' => [" . "\r\n";
                    $routesFileContent .= "        '" . $route['routePath'] . "'," . "\r\n";
                    $routesFileContent .= "        'task=" . $route['task'] . "&action=" . $route['action'] . $route['substring'] . "'," . "\r\n";
                    $routesFileContent .= "    ]," . "\r\n";
                }

                $routesFileContent = rtrim($routesFileContent, ',' . "\r\n");
                $routesFileContent .= "\r\n" . "];";
                file_put_contents($this->cacheDir . $this->routesFile, $routesFileContent);
                $usedControllers = (count($controllerFiles) > 0) ? '$this->usedControllers = [\'' . implode('\',\'',
                        $controllerFiles) . '\'];' : '';
                file_put_contents($this->cacheDir . $this->controllersFile, $controllersFileContent . $usedControllers);
            }
        }
    }
protected function generateRoutes()
    {
        $parsingNeeded = !file_exists($this->cacheDir . $this->routesFile);
        // We look for controller files
        $files = $this->findControllerFiles();

        // We check if there has been modifications since last cache generation
        if (!$parsingNeeded) {
            $routesCacheMtime = filemtime($this->cacheDir . $this->routesFile);
            foreach ($files as $file => $mtime) {
                if ($mtime > $routesCacheMtime) {
                    $parsingNeeded = true;
                    break;
                }
            }
        }

        // We look for deleted controller files
        if (!$parsingNeeded and file_exists($this->cacheDir . $this->controllersFile)) {
            include_once $this->cacheDir . $this->controllersFile;
            foreach ($this->usedControllers as $controllerFile) {
                if (!file_exists($controllerFile)) {
                    $parsingNeeded = true;
                    break;
                }
            }
        }

        $routes = [];
        foreach ($files as $file => $mtime) {
            $parseFile = $this->parseFile($file);
            if (!empty($parseFile)) {
                $routes = array_merge($routes, $parseFile);
            }
        }

        if (!empty($routes)) {
            usort(
                $routes,
                function ($a, $b) {
                    if (strlen($a['routePath']) === strlen($b['routePath'])) {
                        return 0;
                    }
                    return strcmp($b['routePath'],
                        $a['routePath']) ?: strlen($b['routePath']) - strlen($a['routePath']);
                }
            );

            // We regenerate cache file if needed
            if ($parsingNeeded) {
                $controllerFiles = [];
                $commonFileContent = '<?php' . "\r\n" . '/**' . "\r\n" . ' * annotations router %s cache file, create ' . date('c') . "\r\n" . ' */' . "\r\n\r\n";
                $routesFileContent = sprintf($commonFileContent, 'routes');
                $controllersFileContent = sprintf($commonFileContent, 'controllers');
                $routesFileContent .= 'return [';

                foreach ($routes as $key => $route) {
                    $routesFileContent .= "\r\n";
                    $routesFileContent .= "    '" . $route['routeName'] . "' => [" . "\r\n";
                    $routesFileContent .= "        '" . $route['routePath'] . "'," . "\r\n";
                    $routesFileContent .= "        'task=" . $route['task'] . "&action=" . $route['action'] . $route['substring'] . "'," . "\r\n";
                    $routesFileContent .= "    ]," . "\r\n";
                }

                $routesFileContent = rtrim($routesFileContent, ',' . "\r\n");
                $routesFileContent .= "\r\n" . "];";
                file_put_contents($this->cacheDir . $this->routesFile, $routesFileContent);
                $usedControllers = (count($controllerFiles) > 0) ? '$this->usedControllers = [\'' . implode('\',\'',
                        $controllerFiles) . '\'];' : '';
                file_put_contents($this->cacheDir . $this->controllersFile, $controllersFileContent . $usedControllers);
            }
        }
    }
protected function generateRoutes ( ) { $ parsingNeeded = ! file_exists ( $ this -> cacheDir . $ this -> routesFile ) ; // We look for controller files $ files = $ this -> findControllerFiles ( ) ; // We check if there has been modifications since last cache generation if ( ! $ parsingNeeded ) { $ routesCacheMtime = filemtime ( $ this -> cacheDir . $ this -> routesFile ) ; foreach ( $ files as $ file => $ mtime ) { if ( $ mtime > $ routesCacheMtime ) { $ parsingNeeded = true ; break ; } } } // We look for deleted controller files if ( ! $ parsingNeeded and file_exists ( $ this -> cacheDir . $ this -> controllersFile ) ) { include_once $ this -> cacheDir . $ this -> controllersFile ; foreach ( $ this -> usedControllers as $ controllerFile ) { if ( ! file_exists ( $ controllerFile ) ) { $ parsingNeeded = true ; break ; } } } $ routes = [ ] ; foreach ( $ files as $ file => $ mtime ) { $ parseFile = $ this -> parseFile ( $ file ) ; if ( ! empty ( $ parseFile ) ) { $ routes = array_merge ( $ routes , $ parseFile ) ; } } if ( ! empty ( $ routes ) ) { usort ( $ routes , function ( $ a , $ b ) { if ( strlen ( $ a [ 'routePath' ] ) === strlen ( $ b [ 'routePath' ] ) ) { return 0 ; } return strcmp ( $ b [ 'routePath' ] , $ a [ 'routePath' ] ) ? : strlen ( $ b [ 'routePath' ] ) - strlen ( $ a [ 'routePath' ] ) ; } ) ; // We regenerate cache file if needed if ( $ parsingNeeded ) { $ controllerFiles = [ ] ; $ commonFileContent = '<?php' . "\r\n" . '/**' . "\r\n" . ' * annotations router %s cache file, create ' . date ( 'c' ) . "\r\n" . ' */' . "\r\n\r\n" ; $ routesFileContent = sprintf ( $ commonFileContent , 'routes' ) ; $ controllersFileContent = sprintf ( $ commonFileContent , 'controllers' ) ; $ routesFileContent .= 'return [' ; foreach ( $ routes as $ key => $ route ) { $ routesFileContent .= "\r\n" ; $ routesFileContent .= "    '" . $ route [ 'routeName' ] . "' => [" . "\r\n" ; $ routesFileContent .= "        '" . $ route [ 'routePath' ] . "'," . "\r\n" ; $ routesFileContent .= "        'task=" . $ route [ 'task' ] . "&action=" . $ route [ 'action' ] . $ route [ 'substring' ] . "'," . "\r\n" ; $ routesFileContent .= "    ]," . "\r\n" ; } $ routesFileContent = rtrim ( $ routesFileContent , ',' . "\r\n" ) ; $ routesFileContent .= "\r\n" . "];" ; file_put_contents ( $ this -> cacheDir . $ this -> routesFile , $ routesFileContent ) ; $ usedControllers = ( count ( $ controllerFiles ) > 0 ) ? '$this->usedControllers = [\'' . implode ( '\',\'' , $ controllerFiles ) . '\'];' : '' ; file_put_contents ( $ this -> cacheDir . $ this -> controllersFile , $ controllersFileContent . $ usedControllers ) ; } } }
Annotations parser.
Annotations parser .