Jelajahi Sumber

Merge remote-tracking branch 'upstream/master'

Daniel Gomes 14 tahun lalu
induk
melakukan
8870d3f31e

+ 7 - 0
UPDATE.md

@@ -9,6 +9,13 @@ timeline closely anyway.
 beta1 to beta2
 --------------
 
+* The `doctrine:generate:entities` arguments and options changed. Run
+  `./app/console doctrine:generate:entities --help` for more information about
+  the new syntax.
+
+* The `doctrine:generate:repositories` command has been removed. The
+  functionality has been moved to the `doctrine:generate:entities`.
+
 * Doctrine event subscribers now use a unique "doctrine.event_subscriber" tag.
   Doctrine event listeners also use a unique "doctrine.event_listener" tag. To
   specify a connection, use the optional "connection" attribute.

+ 55 - 17
src/Symfony/Bundle/DoctrineBundle/Command/DoctrineCommand.php

@@ -17,6 +17,8 @@ use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
 use Doctrine\ORM\Mapping\ClassMetadata;
 use Doctrine\ORM\Mapping\ClassMetadataInfo;
 use Doctrine\ORM\Tools\EntityGenerator;
+use Doctrine\ORM\Version as DoctrineVersion;
+use Doctrine\ORM\ORMException;
 
 /**
  * Base class for Doctrine console commands to extend from.
@@ -29,7 +31,7 @@ abstract class DoctrineCommand extends Command
     {
         $entityGenerator = new EntityGenerator();
 
-        if (version_compare(\Doctrine\ORM\Version::VERSION, "2.0.2-DEV") >= 0) {
+        if (version_compare(DoctrineVersion::VERSION, "2.0.2-DEV") >= 0) {
             $entityGenerator->setAnnotationPrefix("orm:");
         }
         $entityGenerator->setGenerateAnnotations(false);
@@ -37,6 +39,7 @@ abstract class DoctrineCommand extends Command
         $entityGenerator->setRegenerateEntityIfExists(false);
         $entityGenerator->setUpdateEntityIfExists(true);
         $entityGenerator->setNumSpaces(4);
+
         return $entityGenerator;
     }
 
@@ -70,23 +73,41 @@ abstract class DoctrineCommand extends Command
         return $this->container->get($connections[$name]);
     }
 
-    protected function getBundleMetadatas(Bundle $bundle)
+    protected function findMetadatasByNamespace($namespace)
+    {
+        $metadatas = array();
+        foreach ($this->findAllMetadatas() as $name => $metadata) {
+            if (strpos($name, $namespace) === 0) {
+                $metadatas[$name] = $metadata;
+            }
+        }
+
+        return $metadatas;
+    }
+
+    protected function findMetadatasByClass($entity)
+    {
+        foreach ($this->findAllMetadatas() as $name => $metadata) {
+            if ($name === $entity) {
+                return array($name => $metadata);
+            }
+        }
+
+        return array();
+    }
+
+    protected function findAllMetadatas()
     {
-        $namespace = $bundle->getNamespace();
-        $bundleMetadatas = array();
+        $metadatas = array();
         foreach ($this->container->getParameter('doctrine.orm.entity_managers') as $id) {
-            $em = $this->container->get($id);
             $cmf = new DisconnectedClassMetadataFactory();
-            $cmf->setEntityManager($em);
-            $metadatas = $cmf->getAllMetadata();
-            foreach ($metadatas as $metadata) {
-                if (strpos($metadata->name, $namespace) === 0) {
-                    $bundleMetadatas[$metadata->name] = $metadata;
-                }
+            $cmf->setEntityManager($this->container->get($id));
+            foreach ($cmf->getAllMetadata() as $metadata) {
+                $metadatas[$metadata->name] = $metadata;
             }
         }
 
-        return $bundleMetadatas;
+        return $metadatas;
     }
 
     /**
@@ -95,16 +116,33 @@ abstract class DoctrineCommand extends Command
      * @param Bundle $bundle
      * @return string
      */
-    protected function findBasePathForBundle($bundle)
+    protected function findBasePathForClass($name, $namespace, $path)
     {
-        $path = str_replace('\\', '/', $bundle->getNamespace());
-        $search = str_replace('\\', '/', $bundle->getPath());
-        $destination = str_replace('/'.$path, '', $search, $c);
+        $namespace = str_replace('\\', '/', $namespace);
+        $search = str_replace('\\', '/', $path);
+        $destination = str_replace('/'.$namespace, '', $search, $c);
 
         if ($c != 1) {
-            throw new \RuntimeException(sprintf('Can\'t find base path for bundle (path: "%s", destination: "%s").', $path, $destination));
+            throw new \RuntimeException(sprintf('Can\'t find base path for "%s" (path: "%s", destination: "%s").', $name, $path, $destination));
         }
 
         return $destination;
     }
+
+    protected function getAliasedClassName($name)
+    {
+        $pos = strpos($name, ':');
+        $alias = substr($name, 0, $pos);
+
+        foreach ($this->container->getParameter('doctrine.orm.entity_managers') as $id) {
+            $em = $this->container->get($id);
+
+            try {
+                return $em->getConfiguration()->getEntityNamespace($alias).'\\'.substr($name, $pos + 1);
+            } catch (ORMException $e) {
+            }
+        }
+
+        throw new \RuntimeException(sprintf('Entity "%s" does not exist.', $name));
+    }
 }

+ 80 - 24
src/Symfony/Bundle/DoctrineBundle/Command/GenerateEntitiesDoctrineCommand.php

@@ -29,51 +29,107 @@ class GenerateEntitiesDoctrineCommand extends DoctrineCommand
         $this
             ->setName('doctrine:generate:entities')
             ->setDescription('Generate entity classes and method stubs from your mapping information')
-            ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to initialize the entity or entities in')
-            ->addOption('entity', null, InputOption::VALUE_OPTIONAL, 'The entity class to initialize (shortname without namespace)')
+            ->addArgument('name', InputArgument::REQUIRED, 'A bundle name, a namespace, or a class name')
             ->setHelp(<<<EOT
 The <info>doctrine:generate:entities</info> command generates entity classes
 and method stubs from your mapping information:
 
-You have to limit generation of entities to an individual bundle:
+You have to limit generation of entities:
 
-<info>./app/console doctrine:generate:entities MyCustomBundle</info>
+* To a bundle:
 
-Alternatively, you can limit generation to a single entity within a bundle:
+  <info>./app/console doctrine:generate:entities MyCustomBundle</info>
 
-<info>./app/console doctrine:generate:entities "MyCustomBundle" --entity="User"</info>
+* To a single entity:
 
-You have to specify the shortname (without namespace) of the entity you want
-to filter for.
+  <info>./app/console doctrine:generate:entities MyCustomBundle:User</info>
+  <info>./app/console doctrine:generate:entities MyCustomBundle/Entity/User</info>
+
+* To a namespace
+
+  <info>./app/console doctrine:generate:entities MyCustomBundle/Entity</info>
 EOT
         );
     }
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $bundleName = $input->getArgument('bundle');
-        $filterEntity = $input->getOption('entity');
+        try {
+            $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name'));
 
-        $foundBundle = $this->getApplication()->getKernel()->getBundle($bundleName);
+            $output->writeln(sprintf('Generating entities for bundle "<info>%s</info>"', $bundle->getName()));
+            list($metadatas, $path) = $this->getBundleInfo($bundle);
+        } catch (\InvalidArgumentException $e) {
+            $name = strtr($input->getArgument('name'), '/', '\\');
 
-        if ($metadatas = $this->getBundleMetadatas($foundBundle)) {
-            $output->writeln(sprintf('Generating entities for "<info>%s</info>"', $foundBundle->getName()));
-            $entityGenerator = $this->getEntityGenerator();
+            if (false !== strpos($name, ':')) {
+                $name = $this->getAliasedClassName($name);
+            }
 
-            foreach ($metadatas as $metadata) {
-                if ($filterEntity && $metadata->getReflectionClass()->getShortName() !== $filterEntity) {
-                    continue;
-                }
+            if (class_exists($name)) {
+                $output->writeln(sprintf('Generating entity "<info>%s</info>"', $name));
+                list($metadatas, $path) = $this->getClassInfo($name);
+            } else {
+                $output->writeln(sprintf('Generating entities for namespace "<info>%s</info>"', $name));
+                list($metadatas, $path) = $this->getNamespaceInfo($name);
+            }
+        }
+
+        $generator = $this->getEntityGenerator();
+        foreach ($metadatas as $metadata) {
+            $output->writeln(sprintf('  > generating <comment>%s</comment>', $metadata->name));
+            $generator->generate(array($metadata), $path);
 
-                if (strpos($metadata->name, $foundBundle->getNamespace()) === false) {
-                    throw new \RuntimeException(sprintf('Entity "%s" and bundle don\'t have a common namespace, generation failed because the target directory cannot be detected.', $metadata->name));
+            if ($metadata->customRepositoryClassName) {
+                if (false === strpos($metadata->customRepositoryClassName, $namespace)) {
+                    continue;
                 }
 
-                $output->writeln(sprintf('  > generating <comment>%s</comment>', $metadata->name));
-                $entityGenerator->generate(array($metadata), $this->findBasePathForBundle($foundBundle));
+                $generator->writeEntityRepositoryClass($metadata->customRepositoryClassName, $path);
             }
-        } else {
-            throw new \RuntimeException(sprintf('Bundle "%s" does not contain any mapped entities.', $bundleName));
         }
     }
+
+    private function getBundleInfo($bundle)
+    {
+        $namespace = $bundle->getNamespace();
+        if (!$metadatas = $this->findMetadatasByNamespace($namespace)) {
+            throw new \RuntimeException(sprintf('Bundle "%s" does not contain any mapped entities.', $bundle->getName()));
+        }
+
+        $path = $this->findBasePathForClass($bundle->getName(), $bundle->getNamespace(), $bundle->getPath());
+
+        return array($metadatas, $path);
+    }
+
+    private function getClassInfo($class)
+    {
+        if (!$metadatas = $this->findMetadatasByClass($class)) {
+            throw new \RuntimeException(sprintf('Entity "%s" is not a mapped entity.', $class));
+        }
+
+        $r = $metadatas[$class]->getReflectionClass();
+        if (!$r) {
+            throw new \RuntimeException('Unable to determine where to save the "%s" class.', $class);
+        }
+        $path = $this->findBasePathForClass($class, $r->getNamespacename(), dirname($r->getFilename()));
+
+        return array($metadatas, $path);
+    }
+
+    private function getNamespaceInfo($namespace)
+    {
+        if (!$metadatas = $this->findMetadatasByNamespace($namespace)) {
+            throw new \RuntimeException(sprintf('Namespace "%s" does not contain any mapped entities.', $namespace));
+        }
+
+        $first = reset($metadatas);
+        $r = $first->getReflectionClass();
+        if (!$r) {
+            throw new \RuntimeException('Unable to determine where to save the "%s" class.', $class);
+        }
+        $path = $this->findBasePathForClass($namespace, $r->getNamespacename(), dirname($r->getFilename()));
+
+        return array($metadatas, $path);
+    }
 }

+ 0 - 75
src/Symfony/Bundle/DoctrineBundle/Command/GenerateRepositoriesDoctrineCommand.php

@@ -1,75 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Bundle\DoctrineBundle\Command;
-
-use Symfony\Component\Console\Input\InputArgument;
-use Symfony\Component\Console\Input\InputOption;
-use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Output\OutputInterface;
-use Doctrine\ORM\Tools\EntityRepositoryGenerator;
-
-/**
- * Command to generate repository classes for mapping information.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- * @author Jonathan H. Wage <jonwage@gmail.com>
- */
-class GenerateRepositoriesDoctrineCommand extends DoctrineCommand
-{
-    protected function configure()
-    {
-        $this
-            ->setName('doctrine:generate:repositories')
-            ->setDescription('Generate repository classes from your mapping information')
-            ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to initialize the repositories in')
-            ->addOption('entity', null, InputOption::VALUE_OPTIONAL, 'The entity class to generate the repository for (shortname without namespace)')
-            ->setHelp(<<<EOT
-The <info>doctrine:generate:repositories</info> command generates the
-configured entity repository classes from your mapping information:
-
-<info>./app/console doctrine:generate:repositories</info>
-EOT
-        );
-    }
-
-    protected function execute(InputInterface $input, OutputInterface $output)
-    {
-        $bundleName = $input->getArgument('bundle');
-        $filterEntity = $input->getOption('entity');
-
-        $foundBundle = $this->getApplication()->getKernel()->getBundle($bundleName);
-
-        if ($metadatas = $this->getBundleMetadatas($foundBundle)) {
-            $output->writeln(sprintf('Generating entity repositories for "<info>%s</info>"', $foundBundle->getName()));
-            $generator = new EntityRepositoryGenerator();
-
-            foreach ($metadatas as $metadata) {
-                if ($filterEntity && $filterEntity !== $metadata->reflClass->getShortname()) {
-                    continue;
-                }
-
-                if ($metadata->customRepositoryClassName) {
-                    if (strpos($metadata->customRepositoryClassName, $foundBundle->getNamespace()) === false) {
-                        throw new \RuntimeException(sprintf('Repository "%s" and bundle don\'t have a common namespace, generation failed because the target directory cannot be detected.', $metadata->customRepositoryClassName));
-                    }
-
-                    $output->writeln(sprintf('  > <info>OK</info> generating <comment>%s</comment>', $metadata->customRepositoryClassName));
-                    $generator->writeEntityRepositoryClass($metadata->customRepositoryClassName, $this->findBasePathForBundle($foundBundle));
-                } else {
-                    $output->writeln(sprintf('  > <error>SKIP</error> no custom repository defined for <comment>%s</comment> (no "repositoryClass" option found in the metadata)', $metadata->name));
-                }
-            }
-        } else {
-            throw new \RuntimeException(sprintf('Bundle "%s" does not contain any mapped entities.', $bundleName));
-        }
-    }
-}

+ 7 - 7
src/Symfony/Bundle/FrameworkBundle/Resources/public/css/exception.css

@@ -91,16 +91,16 @@ build: 56
     margin-left:20px;
 }
 
-/* fix for Opera not liking empty <li> */
-.sf-exceptionreset .traces li:after {
-    content: '\00A0'
-}
-
-.sf-exceptionreset #logs .traces em
+.sf-exceptionreset #logs .traces li.error
 {
     font-style:normal;
     color:#AA3333;
-    font-weight: bold;
+    background:#f9ecec;
+}
+
+/* fix for Opera not liking empty <li> */
+.sf-exceptionreset .traces li:after {
+    content: '\00A0'
 }
 
 .sf-exceptionreset .trace

+ 1 - 7
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/logs.html.twig

@@ -1,12 +1,6 @@
 <ol class="traces">
     {% for log in logs %}
-        <li>
-            {% if log.priorityName in ['EMERG', 'ERR', 'CRIT', 'ALERT', 'ERROR', 'CRITICAL'] %}
-                <em>{{ log.priorityName }}</em>
-            {% else %}
-                {{ log.priorityName }}
-            {% endif %}
-
+        <li{% if log.priorityName in ['EMERG', 'ERR', 'CRIT', 'ALERT', 'ERROR', 'CRITICAL'] %} class="error"{% endif %}>
             {{ log.message }}
         </li>
     {% endfor %}

+ 7 - 2
src/Symfony/Component/HttpKernel/Debug/ExceptionListener.php

@@ -49,7 +49,12 @@ class ExceptionListener
         $request = $event->getRequest();
 
         if (null !== $this->logger) {
-            $this->logger->err(sprintf('%s: %s (uncaught exception)', get_class($exception), $exception->getMessage()));
+            $message = sprintf('%s: %s (uncaught exception)', get_class($exception), $exception->getMessage());
+            if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
+                $this->logger->crit($message);
+            } else {
+                $this->logger->err($message);
+            }
         } else {
             error_log(sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));
         }
@@ -77,7 +82,7 @@ class ExceptionListener
         } catch (\Exception $e) {
             $message = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage());
             if (null !== $this->logger) {
-                if ($exception instanceof HttpExceptionInterface && $exception->getStatusCode() >= 500) {
+                if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
                     $this->logger->crit($message);
                 } else {
                     $this->logger->err($message);

+ 6 - 3
src/Symfony/Component/HttpKernel/Kernel.php

@@ -242,8 +242,11 @@ abstract class Kernel implements KernelInterface
             throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name));
         }
 
-        $name = substr($name, 1);
-        list($bundleName, $path) = explode('/', $name, 2);
+        $bundleName = substr($name, 1);
+        $path = '';
+        if (false !== strpos($bundleName, '/')) {
+            list($bundleName, $path) = explode('/', $bundleName, 2);
+        }
 
         $isResource = 0 === strpos($path, 'Resources') && null !== $dir;
         $overridePath = substr($path, 9);
@@ -280,7 +283,7 @@ abstract class Kernel implements KernelInterface
             return $first && $isResource ? $files[0] : $files;
         }
 
-        throw new \InvalidArgumentException(sprintf('Unable to find file "@%s".', $name));
+        throw new \InvalidArgumentException(sprintf('Unable to find file "%s".', $name));
     }
 
     /**

+ 6 - 4
src/Symfony/Component/Routing/Generator/UrlGenerator.php

@@ -105,12 +105,14 @@ class UrlGenerator implements UrlGeneratorInterface
         foreach ($tokens as $token) {
             if ('variable' === $token[0]) {
                 if (false === $optional || !isset($defaults[$token[3]]) || (isset($parameters[$token[3]]) && $parameters[$token[3]] != $defaults[$token[3]])) {
-                    // check requirement
-                    if (!preg_match('#^'.$token[2].'$#', $tparams[$token[3]])) {
-                        throw new \InvalidArgumentException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $tparams[$token[3]]));
+                    if (!$isEmpty = in_array($tparams[$token[3]], array(null, '', false), true)) {
+                        // check requirement
+                        if ($tparams[$token[3]] && !preg_match('#^'.$token[2].'$#', $tparams[$token[3]])) {
+                            throw new \InvalidArgumentException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $tparams[$token[3]]));
+                        }
                     }
 
-                    if (!in_array($tparams[$token[3]], array(null, '', false), true) || !$optional) {
+                    if (!$isEmpty || !$optional) {
                         // %2F is not valid in a URL, so we don't encode it (which is fine as the requirements explicitly allowed it)
                         $url = $token[1].str_replace('%2F', '/', urlencode($tparams[$token[3]])).$url;
                     }

+ 16 - 9
src/Symfony/Component/Routing/RouteCompiler.php

@@ -47,7 +47,7 @@ class RouteCompiler implements RouteCompilerInterface
                 if ($pos !== $len) {
                     $seps[] = $pattern[$pos];
                 }
-                $regexp = sprintf('[^%s]*?', preg_quote(implode('', array_unique($seps)), '#'));
+                $regexp = sprintf('[^%s]+?', preg_quote(implode('', array_unique($seps)), '#'));
             }
 
             $tokens[] = array('variable', $match[0][0][0], $regexp, $var);
@@ -71,15 +71,22 @@ class RouteCompiler implements RouteCompilerInterface
         // compute the matching regexp
         $regex = '';
         $indent = 1;
-        foreach ($tokens as $i => $token) {
-            if ('text' === $token[0]) {
-                $regex .= str_repeat(' ', $indent * 4).preg_quote($token[1], '#')."\n";
-            } else {
-                if ($i >= $firstOptional) {
-                    $regex .= str_repeat(' ', $indent * 4)."(?:\n";
-                    ++$indent;
+        if (1 === count($tokens) && 0 === $firstOptional) {
+            $token = $tokens[0];
+            ++$indent;
+            $regex .= str_repeat(' ', $indent * 4).sprintf("%s(?:\n", preg_quote($token[1], '#'));
+            $regex .= str_repeat(' ', $indent * 4).sprintf("(?P<%s>%s)\n", $token[3], $token[2]);
+        } else {
+            foreach ($tokens as $i => $token) {
+                if ('text' === $token[0]) {
+                    $regex .= str_repeat(' ', $indent * 4).preg_quote($token[1], '#')."\n";
+                } else {
+                    if ($i >= $firstOptional) {
+                        $regex .= str_repeat(' ', $indent * 4)."(?:\n";
+                        ++$indent;
+                    }
+                    $regex .= str_repeat(' ', $indent * 4).sprintf("%s(?P<%s>%s)\n", preg_quote($token[1], '#'), $token[3], $token[2]);
                 }
-                $regex .= str_repeat(' ', $indent * 4).sprintf("%s(?P<%s>%s)\n", preg_quote($token[1], '#'), $token[3], $token[2]);
             }
         }
         while (--$indent) {

+ 6 - 7
tests/Symfony/Tests/Component/HttpKernel/Debug/ExceptionListenerTest.php

@@ -45,9 +45,8 @@ class ExceptionListenerTest extends \PHPUnit_Framework_TestCase
      */
     public function testHandleWithoutLogger($event, $event2)
     {
-        //store the current log_errors, and disable it temporarily
-        $logErrors = ini_get('log_errors');
-        ini_set('log_errors', 'Off');
+        // store the current error_log, and disable it temporarily
+        $errorLog = ini_set('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');
 
         $l = new ExceptionListener('foo');
         $l->onCoreException($event);
@@ -60,8 +59,8 @@ class ExceptionListenerTest extends \PHPUnit_Framework_TestCase
             $this->assertSame('foo', $e->getMessage());
         }
 
-        //restore the old log_errors setting
-        ini_set('log_errors', $logErrors);
+        // restore the old error_log
+        ini_set('error_log', $errorLog);
     }
 
     /**
@@ -83,7 +82,7 @@ class ExceptionListenerTest extends \PHPUnit_Framework_TestCase
         }
 
         $this->assertEquals(3, $logger->countErrors());
-        $this->assertEquals(3, count($logger->getLogs('err')));
+        $this->assertEquals(3, count($logger->getLogs('crit')));
     }
 
     public function provider()
@@ -104,7 +103,7 @@ class TestLogger extends Logger implements DebugLoggerInterface
 {
     public function countErrors()
     {
-        return count($this->logs['err']);
+        return count($this->logs['crit']);
     }
 
     public function getDebugLogger()

+ 7 - 7
tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.apache

@@ -7,10 +7,10 @@ RewriteCond %{REQUEST_URI} ^/foo/(baz|symfony)$
 RewriteRule .* app.php [QSA,L,E=_ROUTING__route:foo,E=_ROUTING_bar:%1,E=_ROUTING_def:test]
 
 # bar
-RewriteCond %{REQUEST_URI} ^/bar/([^/]*?)$
+RewriteCond %{REQUEST_URI} ^/bar/([^/]+?)$
 RewriteCond %{REQUEST_METHOD} !^(get|head)$ [NC]
 RewriteRule .* - [S=1,E=_ROUTING__allow_get:1,E=_ROUTING__allow_head:1]
-RewriteCond %{REQUEST_URI} ^/bar/([^/]*?)$
+RewriteCond %{REQUEST_URI} ^/bar/([^/]+?)$
 RewriteRule .* app.php [QSA,L,E=_ROUTING__route:bar,E=_ROUTING_foo:%1]
 
 # baz
@@ -28,18 +28,18 @@ RewriteCond %{REQUEST_URI} ^/test/baz3/$
 RewriteRule .* app.php [QSA,L,E=_ROUTING__route:baz3]
 
 # baz4
-RewriteCond %{REQUEST_URI} ^/test/([^/]*?)$
+RewriteCond %{REQUEST_URI} ^/test/([^/]+?)$
 RewriteRule .* $0/ [QSA,L,R=301]
-RewriteCond %{REQUEST_URI} ^/test/([^/]*?)/$
+RewriteCond %{REQUEST_URI} ^/test/([^/]+?)/$
 RewriteRule .* app.php [QSA,L,E=_ROUTING__route:baz4,E=_ROUTING_foo:%1]
 
 # baz5
-RewriteCond %{REQUEST_URI} ^/test/([^/]*?)/$
+RewriteCond %{REQUEST_URI} ^/test/([^/]+?)/$
 RewriteCond %{REQUEST_METHOD} !^(post)$ [NC]
 RewriteRule .* - [S=2,E=_ROUTING__allow_post:1]
-RewriteCond %{REQUEST_URI} ^/test/([^/]*?)$
+RewriteCond %{REQUEST_URI} ^/test/([^/]+?)$
 RewriteRule .* $0/ [QSA,L,R=301]
-RewriteCond %{REQUEST_URI} ^/test/([^/]*?)/$
+RewriteCond %{REQUEST_URI} ^/test/([^/]+?)/$
 RewriteRule .* app.php [QSA,L,E=_ROUTING__route:baz5,E=_ROUTING_foo:%1]
 
 # 405 Method Not Allowed

+ 4 - 4
tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php

@@ -30,7 +30,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
         }
 
         // bar
-        if (0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/]*?)$#x', $pathinfo, $matches)) {
+        if (0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
             if (!in_array($this->context->getMethod(), array('get', 'head'))) {
                 $allow = array_merge($allow, array('get', 'head'));
                 goto not_bar;
@@ -56,13 +56,13 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
         }
 
         // baz4
-        if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]*?)/$#x', $pathinfo, $matches)) {
+        if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/$#x', $pathinfo, $matches)) {
             $matches['_route'] = 'baz4';
             return $matches;
         }
 
         // baz5
-        if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]*?)/$#x', $pathinfo, $matches)) {
+        if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/$#x', $pathinfo, $matches)) {
             if ($this->context->getMethod() != 'post') {
                 $allow[] = 'post';
                 goto not_baz5;
@@ -73,7 +73,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
         not_baz5:
 
         // baz.baz6
-        if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]*?)/$#x', $pathinfo, $matches)) {
+        if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/$#x', $pathinfo, $matches)) {
             if ($this->context->getMethod() != 'put') {
                 $allow[] = 'put';
                 goto not_bazbaz6;

+ 4 - 4
tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher2.php

@@ -30,7 +30,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
         }
 
         // bar
-        if (0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/]*?)$#x', $pathinfo, $matches)) {
+        if (0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
             if (!in_array($this->context->getMethod(), array('get', 'head'))) {
                 $allow = array_merge($allow, array('get', 'head'));
                 goto not_bar;
@@ -59,7 +59,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
         }
 
         // baz4
-        if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]*?)/?$#x', $pathinfo, $matches)) {
+        if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/?$#x', $pathinfo, $matches)) {
             if (substr($pathinfo, -1) !== '/') {
                 return $this->redirect($pathinfo.'/', 'baz4');
             }
@@ -68,7 +68,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
         }
 
         // baz5
-        if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]*?)/?$#x', $pathinfo, $matches)) {
+        if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/?$#x', $pathinfo, $matches)) {
             if ($this->context->getMethod() != 'post') {
                 $allow[] = 'post';
                 goto not_baz5;
@@ -82,7 +82,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
         not_baz5:
 
         // baz.baz6
-        if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]*?)/?$#x', $pathinfo, $matches)) {
+        if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/?$#x', $pathinfo, $matches)) {
             if ($this->context->getMethod() != 'put') {
                 $allow[] = 'put';
                 goto not_bazbaz6;

+ 22 - 0
tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php

@@ -96,6 +96,19 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
         $this->assertInternalType('array', $matcher->match('/foo'));
         $matcher = new UrlMatcher($collection, new RequestContext('', 'head'), array());
         $this->assertInternalType('array', $matcher->match('/foo'));
+
+        // route with an optional variable as the first segment
+        $collection = new RouteCollection();
+        $collection->add('bar', new Route('/{bar}/foo', array('bar' => 'bar'), array('bar' => 'foo|bar')));
+        $matcher = new UrlMatcher($collection, new RequestContext(), array());
+        $this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/bar/foo'));
+        $this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo/foo'));
+
+        $collection = new RouteCollection();
+        $collection->add('bar', new Route('/{bar}', array('bar' => 'bar'), array('bar' => 'foo|bar')));
+        $matcher = new UrlMatcher($collection, new RequestContext(), array());
+        $this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo'));
+        $this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/'));
     }
 
     public function testMatchRegression()
@@ -106,5 +119,14 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
 
         $matcher = new UrlMatcher($coll, new RequestContext());
         $this->assertEquals(array('foo' => 'bar', '_route' => 'bar'), $matcher->match('/foo/bar/bar'));
+
+        $collection = new RouteCollection();
+        $collection->add('foo', new Route('/{bar}'));
+        $matcher = new UrlMatcher($collection, new RequestContext(), array());
+        try {
+            $matcher->match('/');
+            $this->fail();
+        } catch (NotFoundException $e) {
+        }
     }
 }

+ 27 - 13
tests/Symfony/Tests/Component/Routing/RouteCompilerTest.php

@@ -43,45 +43,59 @@ class RouteCompilerTest extends \PHPUnit_Framework_TestCase
             array(
                 'Route with a variable',
                 array('/foo/{bar}'),
-                '/foo', '#^/foo/(?P<bar>[^/]*?)$#x', array('bar'), array(
-                    array('variable', '/', '[^/]*?', 'bar'),
+                '/foo', '#^/foo/(?P<bar>[^/]+?)$#x', array('bar'), array(
+                    array('variable', '/', '[^/]+?', 'bar'),
                     array('text', '/foo'),
                 )),
 
             array(
                 'Route with a variable that has a default value',
                 array('/foo/{bar}', array('bar' => 'bar')),
-                '/foo', '#^/foo(?:/(?P<bar>[^/]*?))?$#x', array('bar'), array(
-                    array('variable', '/', '[^/]*?', 'bar'),
+                '/foo', '#^/foo(?:/(?P<bar>[^/]+?))?$#x', array('bar'), array(
+                    array('variable', '/', '[^/]+?', 'bar'),
                     array('text', '/foo'),
                 )),
 
             array(
                 'Route with several variables',
                 array('/foo/{bar}/{foobar}'),
-                '/foo', '#^/foo/(?P<bar>[^/]*?)/(?P<foobar>[^/]*?)$#x', array('bar', 'foobar'), array(
-                    array('variable', '/', '[^/]*?', 'foobar'),
-                    array('variable', '/', '[^/]*?', 'bar'),
+                '/foo', '#^/foo/(?P<bar>[^/]+?)/(?P<foobar>[^/]+?)$#x', array('bar', 'foobar'), array(
+                    array('variable', '/', '[^/]+?', 'foobar'),
+                    array('variable', '/', '[^/]+?', 'bar'),
                     array('text', '/foo'),
                 )),
 
             array(
                 'Route with several variables that have default values',
                 array('/foo/{bar}/{foobar}', array('bar' => 'bar', 'foobar' => '')),
-                '/foo', '#^/foo(?:/(?P<bar>[^/]*?)(?:/(?P<foobar>[^/]*?))?)?$#x', array('bar', 'foobar'), array(
-                    array('variable', '/', '[^/]*?', 'foobar'),
-                    array('variable', '/', '[^/]*?', 'bar'),
+                '/foo', '#^/foo(?:/(?P<bar>[^/]+?)(?:/(?P<foobar>[^/]+?))?)?$#x', array('bar', 'foobar'), array(
+                    array('variable', '/', '[^/]+?', 'foobar'),
+                    array('variable', '/', '[^/]+?', 'bar'),
                     array('text', '/foo'),
                 )),
 
             array(
                 'Route with several variables but some of them have no default values',
                 array('/foo/{bar}/{foobar}', array('bar' => 'bar')),
-                '/foo', '#^/foo/(?P<bar>[^/]*?)/(?P<foobar>[^/]*?)$#x', array('bar', 'foobar'), array(
-                    array('variable', '/', '[^/]*?', 'foobar'),
-                    array('variable', '/', '[^/]*?', 'bar'),
+                '/foo', '#^/foo/(?P<bar>[^/]+?)/(?P<foobar>[^/]+?)$#x', array('bar', 'foobar'), array(
+                    array('variable', '/', '[^/]+?', 'foobar'),
+                    array('variable', '/', '[^/]+?', 'bar'),
                     array('text', '/foo'),
                 )),
+
+            array(
+                'Route with an optional variable as the first segment',
+                array('/{bar}', array('bar' => 'bar')),
+                '', '#^/(?:(?P<bar>[^/]+?))?$#x', array('bar'), array(
+                    array('variable', '/', '[^/]+?', 'bar'),
+                )),
+
+            array(
+                'Route with an optional variable as the first segment with requirements',
+                array('/{bar}', array('bar' => 'bar'), array('bar' => '(foo|bar)')),
+                '', '#^/(?:(?P<bar>(foo|bar)))?$#x', array('bar'), array(
+                    array('variable', '/', '(foo|bar)', 'bar'),
+                )),
         );
     }
 }