Пример #1
0
 public static function getRoutes($path)
 {
     $routes = new \Symfony\Component\Routing\RouteCollection();
     $finder = new \Symfony\Component\Finder\Finder();
     $finder->name('*.php');
     $finder->files()->in($path);
     if (!$finder->count()) {
         throw new Exception('No routes found in ' . $path);
     }
     foreach ($finder as $entry) {
         $route_path = $entry->getRelativePath();
         $basename = $entry->getBasename('.php');
         $cleaned = str_replace('/', '_', str_replace('-', '_', $route_path . $basename));
         $route = $route_path . ($basename === 'index' ? '' : '/' . $basename);
         /*
                    if (strrchr($route, '-')) {
                        $sluglen = strlen(strrchr($route, '-'));
                        $route2 = substr($route, 0, strlen($route) - $sluglen + 1) . '{slug}';
                        $basename2 = substr($basename, 0, $basename - $sluglen + 1);
                    } else {
                        $route2 = $route;
                        $basename2 = $basename;
                    }
         */
         $routes->add($cleaned, new \Symfony\Component\Routing\Route($route, ['_controller' => 'build', 'php_file' => $entry->getRealPath(), 'file_root' => $route_path . '/' . $basename, 'name' => '.+']));
         $routes->add('slash' . $cleaned, new \Symfony\Component\Routing\Route($route . '/', ['_controller' => 'build', 'php_file' => $entry->getRealPath(), 'file_root' => $route_path . '/' . $basename, 'name' => '.+']));
     }
     return $routes;
 }
Пример #2
0
 public function mainMenu(FactoryInterface $factory, array $options)
 {
     $menu = $factory->createItem('root', array('childrenAttributes' => array('class' => 'sidebar-menu')));
     $menu->addChild('Home', array('route' => 'home', 'label' => sprintf('<i class="fa fa-dashboard"></i> %s</a>', $this->translator->trans('menu.dashboard', array(), $this->translationDomain)), 'extras' => array('safe_label' => true), 'attributes' => array('class' => 'treeview')));
     $menu->addChild('Profile', array('uri' => '#', 'label' => sprintf('<i class="fa fa-user"></i> %s<i class="fa fa-angle-double-left pull-right"></i></a>', $this->translator->trans('menu.profile', array(), $this->translationDomain)), 'extras' => array('safe_label' => true), 'attributes' => array('class' => 'treeview')));
     $menu['Profile']->setChildrenAttribute('class', 'treeview-menu');
     $menu['Profile']->addChild('UserProfile', array('label' => $this->translator->trans('menu.profile', array(), $this->translationDomain), 'route' => 'ihsan_simpleadmin_index_profile', 'attributes' => array('class' => 'treeview')));
     $menu['Profile']->addChild('ChangePassword', array('label' => $this->translator->trans('menu.user.change_password', array(), $this->translationDomain), 'route' => 'ihsan_simpleadmin_index_changepassword', 'attributes' => array('class' => 'treeview')));
     if ($this->routeCollection->get('ihsan_simpleadmin_security_user_new')) {
         $this->addUserMenu($menu);
     }
     return $menu;
 }
/**
 * Sets up Symfony2 optimized tests
 */
function setupSymfony2Optimized(Benchmark $benchmark, $routes, $args)
{
    $argString = implode('/', array_map(function ($i) {
        return "{arg{$i}}";
    }, range(1, $args)));
    $str = $firstStr = $lastStr = '';
    $sfRoutes = new \Symfony\Component\Routing\RouteCollection();
    for ($i = 0, $str = 'a'; $i < $routes; $i++, $str++) {
        list($pre, $post) = getRandomParts();
        $str = '/' . $pre . '/' . $argString . '/' . $post;
        if (0 === $i) {
            $firstStr = str_replace(array('{', '}'), '', $str);
        }
        $lastStr = str_replace(array('{', '}'), '', $str);
        $sfRoutes->add($str, new \Symfony\Component\Routing\Route($str, array('controller' => 'handler' . $i)));
    }
    $dumper = new \Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper($sfRoutes);
    file_put_contents(__DIR__ . '/files/worst-case-sf2.php', $dumper->dump(array('class' => 'WorstCaseSf2UrlMatcher')));
    require_once __DIR__ . '/files/worst-case-sf2.php';
    $router = new \WorstCaseSf2UrlMatcher(new \Symfony\Component\Routing\RequestContext());
    $benchmark->register(sprintf('Symfony2 Dumped - last route (%s routes)', $routes), function () use($router, $lastStr) {
        $route = $router->match($lastStr);
    });
    $benchmark->register(sprintf('Symfony2 Dumped - unknown route (%s routes)', $routes), function () use($router) {
        try {
            $route = $router->match('/not-even-real');
        } catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
        }
    });
}
Пример #4
0
#!/usr/bin/php
<?php 
$config = ['require_services' => ['sf_event_dispatcher', 'sf_http_foundation', 'sf_debug', 'psr_log', 'sf_routing'], 'git_urls' => ['https://github.com/symfony/HttpKernel.git' => 'sf_http_kernel/'], 'autoload_config' => ['sf_http_kernel/' => 'Symfony\\Component\\HttpKernel'], 'example' => function () {
    $old_level = error_reporting();
    error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_USER_DEPRECATED & ~E_STRICT);
    $routes = new \Symfony\Component\Routing\RouteCollection();
    $routes->add('hello', new \Symfony\Component\Routing\Route('/', ['_controller' => function (\Symfony\Component\HttpFoundation\Request $request) {
        return new \Symfony\Component\HttpFoundation\Response(sprintf("Hello %s", $request->get('name')));
    }]));
    $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
    $context = new \Symfony\Component\Routing\RequestContext();
    $context->fromRequest($request);
    $matcher = new \Symfony\Component\Routing\Matcher\UrlMatcher($routes, $context);
    $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
    $dispatcher->addSubscriber(new \Symfony\Component\HttpKernel\EventListener\RouterListener($matcher));
    $resolver = new \Symfony\Component\HttpKernel\Controller\ControllerResolver();
    $kernel = new \Symfony\Component\HttpKernel\HttpKernel($dispatcher, $resolver);
    $kernel->handle($request)->send();
    echo PHP_EOL;
    error_reporting($old_level);
}];
if ($return_config) {
    return $config;
}
require_once __DIR__ . '/_yf_autoloader.php';
new yf_autoloader($config);
/**
 * Sets up Symfony2 optimized tests
 */
function setupSymfony2Optimized(Benchmark $benchmark, $routes, $args)
{
    $argString = implode('/', array_map(function ($i) {
        return "{arg{$i}}";
    }, range(1, $args)));
    $str = $firstStr = $lastStr = '';
    $sfRoutes = new \Symfony\Component\Routing\RouteCollection();
    for ($i = 0, $str = 'a'; $i < $routes; $i++, $str++) {
        list($pre, $post) = getRandomParts();
        $str = '/' . $pre . '/' . $argString . '/' . $post;
        if (0 === $i) {
            $firstStr = str_replace(array('{', '}'), '', $str);
        }
        $lastStr = str_replace(array('{', '}'), '', $str);
        $sfRoutes->add($str, new \Symfony\Component\Routing\Route($str, array('controller' => 'handler' . $i)));
    }
    $dumper = new \Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper($sfRoutes);
    file_put_contents(__DIR__ . '/files/first-route-sf2.php', $dumper->dump(array('class' => 'FirstRouteSf2UrlMatcher')));
    require_once __DIR__ . '/files/first-route-sf2.php';
    $router = new \FirstRouteSf2UrlMatcher(new \Symfony\Component\Routing\RequestContext());
    $benchmark->register('Symfony2 Dumped - first route', function () use($router, $firstStr) {
        $route = $router->match($firstStr);
    });
}