Skip to content
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

Rename getPath() to getRealPath() and getAllPaths() to getAllRealPaths() #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions spec/Autoloader/LegacyModulesAutoloaderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function it_prepends_the_classes_directory_and_appends_php_file_extension($cfs)
$path = 'classes/foo.php';
$real_path = '/real/'.$path;

$cfs->getPath($path)->willReturn($real_path);
$cfs->getRealPath($path)->willReturn($real_path);

$cfs->load($real_path)->shouldBeCalled();
$this->autoload('foo')->shouldReturn(true);
Expand All @@ -29,15 +29,15 @@ function it_translates_underscores_to_directory_separators($cfs)
$path = 'classes/foo/bar/baz.php';
$real_path = '/real/'.$path;

$cfs->getPath($path)->willReturn($real_path);
$cfs->getRealPath($path)->willReturn($real_path);

$cfs->load($real_path)->shouldBeCalled();
$this->autoload('foo_bar_baz')->shouldReturn(true);
}

function it_returns_false_when_failing_to_load_a_class($cfs)
{
$cfs->getPath('classes/foo.php')->willReturn(false);
$cfs->getRealPath('classes/foo.php')->willReturn(false);

$this->autoload('foo')->shouldReturn(false);
}
Expand Down
10 changes: 5 additions & 5 deletions spec/Autoloader/ModulesAutoloaderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function it_prepends_the_classes_directory_and_appends_php_file_extension($cfs)
$path = 'classes/Foo.php';
$real_path = '/real/'.$path;

$cfs->getPath($path)->willReturn($real_path);
$cfs->getRealPath($path)->willReturn($real_path);

$cfs->load($real_path)->shouldBeCalled();
$this->autoload('Foo')->shouldReturn(true);
Expand All @@ -29,7 +29,7 @@ function it_translates_namespace_separators_to_directory_separators($cfs)
$path = 'classes/Foo/Bar/Baz.php';
$real_path = '/real/'.$path;

$cfs->getPath($path)->willReturn($real_path);
$cfs->getRealPath($path)->willReturn($real_path);

$cfs->load($real_path)->shouldBeCalled();
$this->autoload('Foo\Bar\Baz')->shouldReturn(true);
Expand All @@ -40,7 +40,7 @@ function it_ignores_a_prepended_namespace_separator($cfs)
$path = 'classes/Foo.php';
$real_path = '/real/'.$path;

$cfs->getPath($path)->willReturn($real_path);
$cfs->getRealPath($path)->willReturn($real_path);

$cfs->load($real_path)->shouldBeCalled();
$this->autoload('\Foo')->shouldReturn(true);
Expand All @@ -51,15 +51,15 @@ function it_translates_underscores_in_the_class_name_to_directory_separators($cf
$path = 'classes/Name_Space/Foo/Bar/Baz.php';
$real_path = '/real/'.$path;

$cfs->getPath($path)->willReturn($real_path);
$cfs->getRealPath($path)->willReturn($real_path);

$cfs->load($real_path)->shouldBeCalled();
$this->autoload('Name_Space\Foo_Bar_Baz')->shouldReturn(true);
}

function it_returns_false_when_failing_to_find_a_class($cfs)
{
$cfs->getPath('classes/Foo.php')->willReturn(false);
$cfs->getRealPath('classes/Foo.php')->willReturn(false);

$this->autoload('Foo')->shouldReturn(false);
}
Expand Down
24 changes: 12 additions & 12 deletions spec/Filesystem/CascadingFilesystemSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,32 +73,32 @@ function it_exposes_base_paths($cache)
$this->getBasePaths()->shouldEqual($base_paths);
}

function it_gets_a_path_with_highest_precedence($cache)
function it_gets_a_real_file_path($cache)
{
$path = 'src/House.php';
$cache_id = 'getPath_'.$path;
$cache_id = 'getRealPath_'.$path;
$real_path = vfsStream::url('root/dir4/src/House.php');

$cache->fetch($cache_id)->willReturn(false);
$cache->save($cache_id, $real_path)->willReturn(true);

$this->getPath($path)->shouldReturn($real_path);
$this->getRealPath($path)->shouldReturn($real_path);
}

function it_returns_false_when_a_path_is_not_found($cache)
function it_returns_false_when_a_real_file_path_is_not_found($cache)
{
$path = 'nonexistent/file.txt';
$cache_id = 'getPath_'.$path;
$cache_id = 'getRealPath_'.$path;

$cache->fetch($cache_id)->willReturn(false);

$this->getPath($path)->shouldReturn(false);
$this->getRealPath($path)->shouldReturn(false);
}

function it_gets_all_paths($cache)
function it_gets_all_real_file_paths($cache)
{
$path = 'src/House.php';
$cache_id = 'getAllPaths_'.$path;
$cache_id = 'getAllRealPaths_'.$path;
$real_paths = [
vfsStream::url('root/dir4/src/House.php'),
vfsStream::url('root/dir1/src/House.php'),
Expand All @@ -107,19 +107,19 @@ function it_gets_all_paths($cache)
$cache->fetch($cache_id)->willReturn(false);
$cache->save($cache_id, $real_paths)->willReturn(true);

$this->getAllPaths('src/House.php')->shouldReturn($real_paths);
$this->getAllRealPaths($path)->shouldReturn($real_paths);
}

function it_returns_an_empty_array_when_no_paths_are_found($cache)
function it_returns_an_empty_array_when_no_real_file_paths_are_found($cache)
{
$path = 'nonexistent/file.md';
$cache_id = 'getAllPaths_'.$path;
$cache_id = 'getAllRealPaths_'.$path;
$real_paths = [];

$cache->fetch($cache_id)->willReturn(false);
$cache->save($cache_id, $real_paths)->willReturn(true);

$this->getAllPaths($path)->shouldReturn($real_paths);
$this->getAllRealPaths($path)->shouldReturn($real_paths);
}

function it_lists_files()
Expand Down
2 changes: 1 addition & 1 deletion spec/Initializer/ModulesInitializerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function it_initializes_the_modules($cfs)
'third/path',
];

$cfs->getAllPaths('init.php')->willReturn($paths);
$cfs->getAllRealPaths('init.php')->willReturn($paths);

foreach ($paths as $path) {
$cfs->load($path)->shouldBecalled(1);
Expand Down
2 changes: 1 addition & 1 deletion src/Autoloader/AbstractModulesAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function translateUnderscores($class_name)
protected function loadClass($file_path)
{
// Get real file path
$real_path = $this->cfs->getPath($file_path);
$real_path = $this->cfs->getRealPath($file_path);

// Load the file if class exists
if ($real_path) {
Expand Down
39 changes: 19 additions & 20 deletions src/Filesystem/CascadingFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Doctrine\Common\Cache\Cache;

/**
* The cascading filesystem.
* A filesystem which is formed from multiple directories being virtually merged
* together. Files in latter defined directories take precedence when merging.
*/
class CascadingFilesystem
{
Expand Down Expand Up @@ -59,16 +60,15 @@ public function getBasePaths()
}

/**
* Finds a file in the cascading filesystem which matches the path and has
* the highest precedence.
* Gets the file's real path from its virtual path.
*
* @param string $relative_path Path to a file
* @return string Real file path
* @param string $path Virtual path to file
* @return string|false Real absolute path to the file
*/
public function getPath($relative_path)
public function getRealPath($path)
{
// Generate cache key
$cache_key = 'getPath_'.$relative_path;
$cache_key = 'getRealPath_'.$path;

// Return cached result if it exists
if (($cached_data = $this->cache->fetch($cache_key)) !== false) {
Expand All @@ -77,31 +77,30 @@ public function getPath($relative_path)

// Search base paths for matching path
foreach (array_reverse($this->base_paths) as $base_path) {
$absolute_path = $base_path.$relative_path;
$real_path = $base_path.$path;

// If file was found
if (is_file($absolute_path)) {
if (is_file($real_path)) {
// Cache the file path
$this->cache->save($cache_key, $absolute_path);
$this->cache->save($cache_key, $real_path);

return $absolute_path;
return $real_path;
}
}

return false;
}

/**
* Finds all real file paths in the cascading filesystem which match the
* path.
* Gets all of the real file paths from their virtual path.
*
* @param string $relative_path Path to a file
* @return array All real file paths found, ordered by precedence descending
* @param string $path Virtual path to file
* @return array All real file paths ordered by precedence descending
*/
public function getAllPaths($relative_path)
public function getAllRealPaths($path)
{
// Generate cache key
$cache_key = 'getAllPaths_'.$relative_path;
$cache_key = 'getAllRealPaths_'.$path;

// Return cached result if it exists
if (($cached_data = $this->cache->fetch($cache_key)) !== false) {
Expand All @@ -112,11 +111,11 @@ public function getAllPaths($relative_path)
$found = [];

foreach (array_reverse($this->base_paths) as $base_path) {
$absolute_path = $base_path.$relative_path;
$real_path = $base_path.$path;

// Add to array if file exists
if (is_file($absolute_path)) {
$found[] = $absolute_path;
if (is_file($real_path)) {
$found[] = $real_path;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Initializer/ModulesInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(CascadingFilesystem $cfs)
public function initialize()
{
// Get all initialization file locations
$init_file_paths = $this->cfs->getAllPaths($this->init_file_path);
$init_file_paths = $this->cfs->getAllRealPaths($this->init_file_path);

// Load initialization files
foreach ($init_file_paths as $path) {
Expand Down