8000 feature #18869 [Routing] Throw exception when PHP start tag is missin… · symfony/symfony@e707760 · GitHub
[go: up one dir, main page]

Skip to content

Commit e707760

Browse files
committed
feature #18869 [Routing] Throw exception when PHP start tag is missing (WouterJ)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Routing] Throw exception when PHP start tag is missing | Q | A | ------------- | --- | Branch? | master (?) | Bug fix? | yes | New feature? | no | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony/symfony-docs#6116 (and many more) | License | MIT | Doc PR | - The Problem --- In the documentation, we never use the PHP start tag. However, in the first tutorials, people simply copy/past the code examples, save the file and expect things to work. They seem to often forget to add the PHP start tag. Without start tag, the annotation file loader simply skips the file without providing any reason why. As a big framework is quite overwhelming, simple things like this are completely forgotten. The Fix --- If a `*.php` file only consists of `T_INLINE_HTML`, it means there is no `<?php` start tag. In this case, instead of skipping the file, an exception is throwed with a possible fix. BC Break? --- As the file loader is only executed for `*.php` files, I think the BC break is minimal, but it is possible that people have applications with `*.php` files *without* a start tag. If this file lives in a routing loaded directory (e.g. when doing `@AppBundle/Controller`), it would now result in an exception. I think this BC break is minimal and can be ignored. If you don't agree, we can add a little str match to check if `class ... {` exists. If that's the case, I think it's safe to say that it was meant to be a PHP file. Bug or Feature? --- I don't know if this is considered a bug or a feature, so I submitted it as a feature. Commits ------- 1e765c8 Throw exception when PHP start tag is missing
2 parents 3844fb9 + 1e765c8 commit e707760

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ protected function findClass($file)
9292
$class = false;
9393
$namespace = false;
9494
$tokens = token_get_all(file_get_contents($file));
95+
96+
if (1 === count($tokens) && T_INLINE_HTML === $tokens[0][0]) {
97+
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain PHP code. Did you forgot to add the "<?php" start tag at the beginning of the file?', $file));
98+
}
99+
95100
for ($i = 0; isset($tokens[$i]); ++$i) {
96101
$token = $tokens[$i];
97102

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class NoStartTagClass
2+
{
3+
}

src/Symfony/Component/Routing/Tests/Loader/AnnotationFileLoaderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line A310 change
@@ -45,6 +45,15 @@ public function testLoadTraitWithClassConstant()
4545
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/FooTrait.php');
4646
}
4747

48+
/**
49+
* @expectedException \InvalidArgumentException
50+
* @expectedExceptionMessage Did you forgot to add the "<?php" start tag at the beginning of the file?
51+
*/
52+
public function testLoadFileWithoutStartTag()
53+
{
54+
$this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/NoStartTagClass.php');
55+
}
56+
4857
/**
4958
* @requires PHP 5.6
5059
*/

0 commit comments

Comments
 (0)
0