Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 13 additions & 9 deletions src/Dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,30 @@ private function __construct()
*
* @param array $dependencies Dependencies
* @param bool $required Whether dependencies are required or not.
*
* @throws \RuntimeException If required and no file is successfully loaded
* via {@link requireFile()}.
*/
protected static function process(array $dependencies, $required)
{
foreach ($dependencies as $dependency) {
if (is_array($dependency)) {
$dependencyToLoad = null;
$loadedDependency = null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

A boolean can be enough ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes! Updated.


foreach ($dependency as $firstExistsDependency) {
if (file_exists($firstExistsDependency)) {
$dependencyToLoad = $firstExistsDependency;
try {
requireFile($firstExistsDependency);
$loadedDependency = $firstExistsDependency;
break;
} catch (\RuntimeException $e) {
Copy link
Member Author

Choose a reason for hiding this comment

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

For the change here, I was trying to make this block of code use the same conditionals logic as Fedora\Autoloader::requireFile() without copying-and-pasting the conditionals. This does have the overhead of an Exception object creation (from Fedora\Autoloader::requireFile()) and the catch though. Perhaps a copy-and-paste of the conditionals here would be better or a common function (something like Fedora\Autoloader::fileLoadable()?) that this code block and Fedora\Autoloader::requireFile() could both use?

}
}

if ($required || isset($dependencyToLoad)) {
if (!isset($dependencyToLoad)) {
$dependencyToLoad = end($dependency);
}

requireFile($dependencyToLoad);
if ($required && empty($loadedDependency)) {
throw new \RuntimeException(sprintf(
'Files not found: "%s"',
Copy link
Collaborator

Choose a reason for hiding this comment

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

for consistency with other exception use single quote around each name.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated.

implode('" || "', $dependency)
));
}
} elseif ($required || file_exists($dependency)) {
requireFile($dependency);
Expand Down
4 changes: 3 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
* {@link https://github.com/composer/composer/blob/master/src/Composer/Autoload/ClassLoader.php Composer/Autoload/ClassLoader::includeFile()}.
*
* @param string $file File to `require_once`.
*
* @throws \RuntimeException If file does not exist or is not readable.
*/
function requireFile($file)
{
if (is_file($file) && is_readable($file)) {
require_once $file;
} else {
throw new \RuntimeException("File not found '$file'");
throw new \RuntimeException("File not found: '$file'");
}
}

Expand Down
11 changes: 7 additions & 4 deletions tests/DependenciesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,27 @@ public function testRequiredExists()

/**
* @expectedException RuntimeException
* @expectedExceptionMessageRegex /File Not Found.*AlsoNotExist/
* @expectedExceptionMessageRegex /Files not found.*DoesNotExist.*AlsoNotExist/
**/
public function testRequiredNotExistsLast()
{
Dependencies::required(array(
array(
__DIR__.'/fixtures/DoesNotExist.php',
__DIR__.'/fixtures/AlsoNotExist.php',
), ));
),
));
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessageRegex /File Not Found.*DoesNotExist/
* @expectedExceptionMessageRegex /File not found.*DoesNotExist/
**/
public function testRequiredNotExists()
{
Dependencies::required(array(__DIR__.'/fixtures/DoesNotExist.php'));
Dependencies::required(array(
__DIR__.'/fixtures/DoesNotExist.php',
));
}

public function testRequiredFirstExists()
Expand Down