Skip to content

Commit

Permalink
feature #19511 Use a dedicated exception in the file locator (leofeyer)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.2-dev branch (closes #19511).

Discussion
----------

Use a dedicated exception in the file locator

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

This PR adds a dedicated `FileLocatorFileNotFoundException` class to the file locator, so it is possible to catch file locator exceptions separately from invalid argument exceptions.

```php
try {
    foreach ($container->get('file_locator')->locate('file.php', null, false) as $file) {
        include $file;
    }
} catch (\InvalidArgumentException $e) {
    // this will catch both file locator exceptions as well as
    // any invalid argument exception thrown in an included file
}
```

With the dedicated exceptions, we could do this:

```php
try {
    foreach ($container->get('file_locator')->locate('file.php', null, false) as $file) {
        include $file;
    }
} catch (FileLocatorFileNotFoundException $e) {
    // this will only ignore file locator exceptions
}
```

Commits
-------

ee4adaa Use a dedicated exception in the file locator
  • Loading branch information
fabpot committed Aug 9, 2016
2 parents 2faf6f3 + ee4adaa commit ccb684a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
@@ -0,0 +1,21 @@
<?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\Component\Config\Exception;

/**
* File locator exception if a file does not exist.
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class FileLocatorFileNotFoundException extends \InvalidArgumentException
{
}
6 changes: 4 additions & 2 deletions src/Symfony/Component/Config/FileLocator.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Config;

use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;

/**
* FileLocator uses an array of pre-defined paths to find files.
*
Expand Down Expand Up @@ -41,7 +43,7 @@ public function locate($name, $currentPath = null, $first = true)

if ($this->isAbsolutePath($name)) {
if (!file_exists($name)) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $name));
throw new FileLocatorFileNotFoundException(sprintf('The file "%s" does not exist.', $name));
}

return $name;
Expand All @@ -66,7 +68,7 @@ public function locate($name, $currentPath = null, $first = true)
}

if (!$filepaths) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist (in: %s).', $name, implode(', ', $paths)));
throw new FileLocatorFileNotFoundException(sprintf('The file "%s" does not exist (in: %s).', $name, implode(', ', $paths)));
}

return $filepaths;
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Config/FileLocatorInterface.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Config;

use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
Expand All @@ -25,7 +27,8 @@ interface FileLocatorInterface
*
* @return string|array The full path to the file or an array of file paths
*
* @throws \InvalidArgumentException When file is not found
* @throws \InvalidArgumentException If $name is empty
* @throws FileLocatorFileNotFoundException If a file is not found
*/
public function locate($name, $currentPath = null, $first = true);
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Config/Tests/FileLocatorTest.php
Expand Up @@ -86,7 +86,7 @@ public function testLocate()
}

/**
* @expectedException \InvalidArgumentException
* @expectedException \Symfony\Component\Config\Exception\FileLocatorFileNotFoundException
* @expectedExceptionMessage The file "foobar.xml" does not exist
*/
public function testLocateThrowsAnExceptionIfTheFileDoesNotExists()
Expand All @@ -97,7 +97,7 @@ public function testLocateThrowsAnExceptionIfTheFileDoesNotExists()
}

/**
* @expectedException \InvalidArgumentException
* @expectedException \Symfony\Component\Config\Exception\FileLocatorFileNotFoundException
*/
public function testLocateThrowsAnExceptionIfTheFileDoesNotExistsInAbsolutePath()
{
Expand Down

0 comments on commit ccb684a

Please sign in to comment.