Skip to content

Commit

Permalink
Add flag for realpath mode
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Dec 15, 2016
2 parents 65d305d + 3fba5df commit 38122b0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class Path
*/
const MOD_RESET = 'reset';

/**
* Flag of result path (If true, is real path. If false, is relative path).
*
* @var string
*/
protected $_isReal = true;

/**
* Holds paths list.
*
Expand Down Expand Up @@ -252,6 +259,17 @@ public function getRoot()
return $this->_root;
}

/**
* Setup real or relative path flag.
*
* @param bool $isReal
* @return void
*/
public function setRealPathFlag($isReal = true)
{
$this->_isReal = (bool) $isReal;
}

/**
* Check virtual or real path.
*
Expand Down Expand Up @@ -548,7 +566,7 @@ protected function _resolvePaths($alias)
$path = $this->_cleanPath($originalPath);
}

$result[] = realpath($path);
$result[] = $this->_getCurrentPath($path);
}

$result = array_filter($result); // remove empty
Expand All @@ -557,6 +575,17 @@ protected function _resolvePaths($alias)
return $result;
}

/**
* Get current resolve path.
*
* @param string $path
* @return string
*/
protected function _getCurrentPath($path)
{
return ($this->_isReal) ? realpath($path) : $path;
}

/**
* @param $alias
* @return mixed|string
Expand Down
33 changes: 33 additions & 0 deletions tests/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ public function setUp()
);
}

public function tearDown()
{
parent::tearDown();
$fs = new Filesystem();
$fs->remove($this->_root);
}

/**
* @expectedException \JBZoo\Path\Exception
*/
Expand Down Expand Up @@ -709,4 +716,30 @@ public function testRootPreDefinedAlias()

$this->_is(__FILE__, $path->get('root:' . $curFile));
}

public function testPathByFlagIsReal()
{
$path = new Path(PROJECT_ROOT);

$name = mt_rand();
$symOrigDir = $this->_root . DS . $name;
$symLink = __DIR__ . '/link/';

$fs = new Filesystem();
$fs->mkdir($symOrigDir);

$fs->dumpFile($symOrigDir . DS . 'file-1.txt', '');
$fs->dumpFile($symLink . DS . 'file-2.txt', '');

$fs->symlink($symLink, $symOrigDir . '/link', true);

$path->set('by-flag', array($symLink, $symOrigDir));

isNotNull($path->get('by-flag:file-2.txt'));

$path->setRealPathFlag(false);
isNotNull($path->get('by-flag:file-2.txt'));

$fs->remove(array($symOrigDir, $symLink));
}
}
7 changes: 7 additions & 0 deletions tests/performanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public function setUp()
$this->_root = $root;
}

public function tearDown()
{
parent::tearDown();
$fs = new Filesystem();
$fs->remove($this->_root);
}

public function testCompareWithRealpath()
{
$fs = new Filesystem();
Expand Down

0 comments on commit 38122b0

Please sign in to comment.