Skip to content
Merged
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
29 changes: 29 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@
*/
namespace Fedora\Autoloader;

/**
* Check if given path is a full or relative path
*
* @param string $path to check
*
* @return boolean
*/
function isFullPath($path)
{
if (DIRECTORY_SEPARATOR == '/') {
/* Unix/Linux */
return ($path[0] == '/');
}
/* Windows */
if (ctype_alpha($path[0]) && $path[1]==':') {
return true; // Drive
}
if (($path[0] == '/' && $path[1] == '/') || ($path[0] == '\\' && $path[1] == '\\')) {
return true; // UNC
}
return false;
}

/**
* Scope isolated require.
*
Expand All @@ -23,6 +46,12 @@
*/
function requireFile($file)
{
if (!isFullPath($file)) {
// Search for relative path in the defined include_path
if ($path = stream_resolve_include_path($file)) {
$file = $path;
}
}
if (is_file($file) && is_readable($file)) {
require_once $file;
} else {
Expand Down
47 changes: 47 additions & 0 deletions tests/DependenciesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,51 @@ public function testOptionalFirstExists()
$this->assertTrue(class_exists('Foo\\Bar'));
$this->assertFalse(class_exists('Foo\\Bar\\Baz'));
}

public function testIncludePathRequired()
{
// Check if PEAR is installed
$pear = false;
foreach (explode(PATH_SEPARATOR, get_include_path()) as $p) {
if (file_exists("$p/PEAR.php")) {
$pear = true;
break;
}
}
if (!$pear) {
$this->markTestSkipped('PEAR not found in include_path');
}
$this->assertFalse(class_exists('PEAR'));

Dependencies::required(array(
'PEAR.php',
));

$this->assertTrue(class_exists('PEAR'));
}

/**
* @depends testIncludePathRequired
**/
public function testIncludePathOptional()
{
$this->assertFalse(class_exists('PEAR'));

Dependencies::optional(array(
'PEAR.php',
));

$this->assertTrue(class_exists('PEAR'));
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessageRegex /File not found.*DoesNotExist/
**/
public function testIncludePathNotFound()
{
Dependencies::required(array(
'DoesNotExist.php',
));
}
}
45 changes: 45 additions & 0 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* This file is part of the Fedora Autoloader package.
*
* (c) Shawn Iwinski <shawn@iwin.ski> and Remi Collet <remi@fedoraproject.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Fedora\Autoloader\Test;

use PHPUnit\Framework\TestCase;
use Fedora\Autoloader\Dependencies;

class FunctionsTest extends TestCase
{
public function dataPath()
{
if (DIRECTORY_SEPARATOR == '/') {
return array(
array(true, '/tmp', 'Linux full path'),
array(false, 'foo/bar', 'Relative path'),
);
} else {
return array(
array(true, 'c:/tmp', 'Windows full path'),
array(true, '//foo/bar', 'UNC'),
array(true, '\\\\foo\\bar', 'UNC'),
array(false, 'foo/bar', 'Relative path'),
);
}
}

/**
* @dataProvider dataPath
**/
public function testIsFullPath($full, $path, $msg)
{
if ($full) {
$this->assertTrue(\Fedora\Autoloader\isFullPath($path), $msg);
} else {
$this->assertFalse(\Fedora\Autoloader\isFullPath($path), $msg);
}
}
}