Skip to content

Use a dedicated exception in the file locator #19511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply FileNotFoundException? The current name is verbose

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the other exception classes also use a prefix: https://github.com/symfony/symfony/tree/master/src/Symfony/Component/Config/Exception

Just wanted to keep things consistent.

{
}
6 changes: 4 additions & 2 deletions src/Symfony/Component/Config/FileLocator.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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