From f5c81ba27f1c95fe7c560a5c7ddae14bf8a22c0d Mon Sep 17 00:00:00 2001 From: Cheren Date: Wed, 14 Dec 2016 21:33:10 +0300 Subject: [PATCH 1/3] Add result flag for process result --- src/Path.php | 20 +++++++++++++++++++- tests/PathTest.php | 33 +++++++++++++++++++++++++++++++++ tests/performanceTest.php | 7 +++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/Path.php b/src/Path.php index 01f54b2..ed3106c 100644 --- a/src/Path.php +++ b/src/Path.php @@ -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 + */ + public $isReal = true; + /** * Holds paths list. * @@ -548,7 +555,7 @@ protected function _resolvePaths($alias) $path = $this->_cleanPath($originalPath); } - $result[] = realpath($path); + $result[] = $this->_getCurrentPath($path); } $result = array_filter($result); // remove empty @@ -557,6 +564,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 diff --git a/tests/PathTest.php b/tests/PathTest.php index 4bbde79..6758458 100644 --- a/tests/PathTest.php +++ b/tests/PathTest.php @@ -74,6 +74,13 @@ public function setUp() ); } + public function tearDown() + { + parent::tearDown(); + $fs = new Filesystem(); + $fs->remove($this->_root); + } + /** * @expectedException \JBZoo\Path\Exception */ @@ -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', [$symLink, $symOrigDir]); + + isNotNull($path->get('by-flag:file-2.txt')); + + $path->isReal = false; + isNotNull($path->get('by-flag:file-2.txt')); + + $fs->remove(array($symOrigDir, $symLink)); + } } diff --git a/tests/performanceTest.php b/tests/performanceTest.php index b91a8df..00fd100 100644 --- a/tests/performanceTest.php +++ b/tests/performanceTest.php @@ -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(); From 6b3f8e788ec736b14d0e603b569f115321a77af0 Mon Sep 17 00:00:00 2001 From: Cheren Date: Wed, 14 Dec 2016 21:48:11 +0300 Subject: [PATCH 2/3] Add test support PHP 5.3 --- tests/PathTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PathTest.php b/tests/PathTest.php index 6758458..c40f7ea 100644 --- a/tests/PathTest.php +++ b/tests/PathTest.php @@ -733,7 +733,7 @@ public function testPathByFlagIsReal() $fs->symlink($symLink, $symOrigDir . '/link', true); - $path->set('by-flag', [$symLink, $symOrigDir]); + $path->set('by-flag', array($symLink, $symOrigDir)); isNotNull($path->get('by-flag:file-2.txt')); From 3fba5dfaff4bf0b3b05d86966c6c20cb3c2f3554 Mon Sep 17 00:00:00 2001 From: Cheren Date: Thu, 15 Dec 2016 14:28:25 +0300 Subject: [PATCH 3/3] Fix: set real/relative flag use new method --- src/Path.php | 15 +++++++++++++-- tests/PathTest.php | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Path.php b/src/Path.php index ed3106c..d72af7f 100644 --- a/src/Path.php +++ b/src/Path.php @@ -58,7 +58,7 @@ class Path * * @var string */ - public $isReal = true; + protected $_isReal = true; /** * Holds paths list. @@ -259,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. * @@ -572,7 +583,7 @@ protected function _resolvePaths($alias) */ protected function _getCurrentPath($path) { - return ($this->isReal) ? realpath($path) : $path; + return ($this->_isReal) ? realpath($path) : $path; } /** diff --git a/tests/PathTest.php b/tests/PathTest.php index c40f7ea..51ca14f 100644 --- a/tests/PathTest.php +++ b/tests/PathTest.php @@ -737,7 +737,7 @@ public function testPathByFlagIsReal() isNotNull($path->get('by-flag:file-2.txt')); - $path->isReal = false; + $path->setRealPathFlag(false); isNotNull($path->get('by-flag:file-2.txt')); $fs->remove(array($symOrigDir, $symLink));