<commit_before><?php
/**
 * Created by PhpStorm.
 * User: janhuang
 * Date: 15/11/27
 * Time: 下午12:18
 * Github: https://www.github.com/janhuang
 * Coding: https://www.coding.net/janhuang
 * SegmentFault: http://segmentfault.com/u/janhuang
 * Blog: http://segmentfault.com/blog/janhuang
 * Gmail: <EMAIL>
 * WebSite: http://www.janhuang.me
 */

namespace FastD\Framework\Dispatcher\Handle;

use FastD\Annotation\Annotation;
use FastD\Framework\Bundle\Bundle;
use FastD\Framework\Dispatcher\Dispatch;
use Symfony\Component\Finder\Finder;
use Routes;

/**
 * 注释处理调度任务
 *
 * Class AnnotationHandle
 *
 * @package FastD\Framework\Dispatcher\Handle
 */
class AnnotationHandle extends Dispatch
{
    const FILTER = 'Action';

    protected $routes = [];

    /**
     * @return string
     */
    public function getName()
    {
        return 'handle.annotation.route';
    }

    /**
     * @param array|null $parameters
     * @return mixed
     */
    public function dispatch(array $parameters = null)
    {
        $bundles = $this->getContainer()->singleton('kernel')->getBundles();

        foreach ($bundles as $bundle) {
            $this->routes = array_merge($this->routes, $this->scanRoutes($bundle));
        }

        foreach ($this->routes as $prefix => $routes) {
            $closure = function () use ($routes) {
                foreach ($routes as $route) {
                    call_user_func_array("\\Routes::{$route['method']}", $route['parameters']);
                }
            };

            Routes::group($prefix, $closure);
        }
    }

    /**
     * @param Bundle $bundle
     * @return array
     */
    protected function scanRoutes(Bundle $bundle)
    {
        $path = $bundle->getRootPath() . '/Controllers';

        if (!is_dir($path)) {
            return [];
        }

        $baseNamespace = $bundle->getNamespace() . '\\Controllers\\';
        $finder = new Finder();
        $files = $finder->name('*.php')->in($path)->files();

        $routes = [];

        foreach ($files as $file) {
            $className = $baseNamespace . pathinfo($file->getFileName(), PATHINFO_FILENAME);
            if (!class_exists($className)) {
                continue;
            }
            
            $annotation = new Annotation($className, self::FILTER);

            foreach ($annotation as $annotator) {

                if (null === ($route = $annotator->getParameter('Route'))) {
                    continue;
                }

                if (!isset($route['name'])) {
                    $route['name'] = $route[0];
                }

                $parameters = [
                    $route['name'],
                    str_replace('//', '/', $route[0]),
                    $annotator->getClassName() . '@' . $annotator->getName(),
                    isset($route['defaults']) ? $route['defaults'] : [],
                    isset($route['requirements']) ? $route['requirements'] : [],
                ];

                $method = null === $annotator->getParameter('Method') ? 'ANY' : $annotator->getParameter('Method')[0];

                if (!empty($parent) && isset($parent[0])) {
                    $routes[$parent[0]][] = [
                        'method' => strtolower($method),
                        'parameters' => $parameters
                    ];
                } else {
                    $routes['/'][] = [
                        'method' => $method,
                        'parameters' => $parameters
                    ];
                }

                unset($route, $method, $parameters, $parent);
            }
        }

        return $routes;
    }
}<commit_msg>fixed scan route bug<commit_after><?php
/**
 * Created by PhpStorm.
 * User: janhuang
 * Date: 15/11/27
 * Time: 下午12:18
 * Github: https://www.github.com/janhuang
 * Coding: https://www.coding.net/janhuang
 * SegmentFault: http://segmentfault.com/u/janhuang
 * Blog: http://segmentfault.com/blog/janhuang
 * Gmail: <EMAIL>
 * WebSite: http://www.janhuang.me
 */

namespace FastD\Framework\Dispatcher\Handle;

use FastD\Annotation\Annotation;
use FastD\Framework\Bundle\Bundle;
use FastD\Framework\Dispatcher\Dispatch;
use Symfony\Component\Finder\Finder;
use Routes;

/**
 * 注释处理调度任务
 *
 * Class AnnotationHandle
 *
 * @package FastD\Framework\Dispatcher\Handle
 */
class AnnotationHandle extends Dispatch
{
    const FILTER = 'Action';

    protected $routes = [];

    /**
     * @return string
     */
    public function getName()
    {
        return 'handle.annotation.route';
    }

    /**
     * @param array|null $parameters
     * @return mixed
     */
    public function dispatch(array $parameters = null)
    {
        $bundles = $this->getContainer()->singleton('kernel')->getBundles();

        foreach ($bundles as $bundle) {
            $this->routes = array_merge($this->routes, $this->scanRoutes($bundle));
        }

        foreach ($this->routes as $prefix => $routes) {
            foreach ($routes as $route) {
                call_user_func_array("\\Routes::{$route['method']}", $route['parameters']);
            }
        }
    }

    /**
     * @param Bundle $bundle
     * @return array
     */
    protected function scanRoutes(Bundle $bundle)
    {
        $path = $bundle->getRootPath() . '/Controllers';

        if (!is_dir($path)) {
            return [];
        }

        $baseNamespace = $bundle->getNamespace() . '\\Controllers\\';
        $finder = new Finder();
        $files = $finder->name('*.php')->in($path)->files();

        $routes = [];

        foreach ($files as $file) {
            $className = $baseNamespace . pathinfo($file->getFileName(), PATHINFO_FILENAME);
            if (!class_exists($className)) {
                continue;
            }
            
            $annotation = new Annotation($className, self::FILTER);

            foreach ($annotation as $annotator) {

                if (null === ($route = $annotator->getParameter('Route'))) {
                    continue;
                }

                if (!isset($route['name'])) {
                    $route['name'] = $route[0];
                }

                $parameters = [
                    $route['name'],
                    str_replace('//', '/', $route[0]),
                    $annotator->getClassName() . '@' . $annotator->getName(),
                    isset($route['defaults']) ? $route['defaults'] : [],
                    isset($route['requirements']) ? $route['requirements'] : [],
                ];

                $method = null === $annotator->getParameter('Method') ? 'any' : strtolower($annotator->getParameter('Method')[0]);

                $routes[$bundle->getName()][] = [
                    'method' => $method,
                    'parameters' => $parameters
                ];

                unset($route, $method, $parameters, $parent);
            }
        }

        return $routes;
    }
}
{'fraction_non_alphanumeric': 0.161794500723589, 'fraction_numerical': 0.004052098408104197, 'mean_word_length': 1.7934518997574778, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 50, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, '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': '4148524', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1911, 'n_words': 344}