This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Sulu\Component\DocumentManager; | ||
|
||
/** | ||
* The path builder provides a way to create paths from templates | ||
*/ | ||
class PathBuilder | ||
{ | ||
/** | ||
* @var PathSegmentRegistry | ||
*/ | ||
private $registry; | ||
|
||
/** | ||
* @param PathSegmentRegistry $registry | ||
*/ | ||
public function __construct(PathSegmentRegistry $registry) | ||
{ | ||
$this->registry = $registry; | ||
} | ||
|
||
/** | ||
* Build a path from an array of path segments. | ||
* | ||
* Segments demarcated by "%" characters will be interpreted as path | ||
* segment *names* and their value will be resolved from the PathSegmentRegistry. | ||
* | ||
* Other segments will be interpreted literally. | ||
* | ||
* The following: | ||
* | ||
* ```` | ||
* $path = $pathBuilder->build(array('%base%', 'hello', '%articles%')); | ||
* ```` | ||
* | ||
* Will yield `/cms/hello/articleDirectory` where `%base%` is "cms" and | ||
* `%articles` is "articleDirectory" | ||
* | ||
* @see Sulu\Component\DocumentManager\PathSegmentRegistry | ||
* | ||
* @param array $segments | ||
* @return string | ||
*/ | ||
public function build($segments) | ||
{ | ||
$results = array(); | ||
foreach ($segments as $segment) { | ||
$result = $this->buildSegment($segment); | ||
|
||
if (null === $result) { | ||
continue; | ||
} | ||
|
||
$results[] = $result; | ||
} | ||
|
||
return '/' . implode('/', $results); | ||
} | ||
|
||
/** | ||
* @param string $segment | ||
*/ | ||
private function buildSegment($segment) | ||
{ | ||
if (empty($segment) || $segment == '/') { | ||
return null; | ||
} | ||
|
||
if (substr($segment, 0, 1) == '%') { | ||
if (substr($segment, -1) == '%') { | ||
return $this->registry->getPathSegment(substr($segment, 1, -1)); | ||
} | ||
} | ||
|
||
return $segment; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Sulu\Component\DocumentManager\Tests\Bench; | ||
|
||
use Sulu\Component\DocumentManager\PathBuilder; | ||
use Sulu\Component\DocumentManager\PathSegmentRegistry; | ||
use PhpBench\Benchmark; | ||
|
||
class PathBuilderBench implements Benchmark | ||
{ | ||
private $pathBuilder; | ||
|
||
public function setUp() | ||
{ | ||
$registry = new PathSegmentRegistry(array( | ||
'one' => 'hello', | ||
'two' => 'goodbye', | ||
)); | ||
$this->pathBuilder = new PathBuilder($registry); | ||
} | ||
/** | ||
* @description Build path 1000 times | ||
* @revs 1000 | ||
* @beforeMethod setUp | ||
* @paramProvider provideElements | ||
*/ | ||
public function benchBuild($iteration) | ||
{ | ||
$this->pathBuilder->build($iteration->getParameter('elements')); | ||
} | ||
|
||
public function provideElements() | ||
{ | ||
return array( | ||
array( | ||
'elements' => array('one', 'two', 'three') | ||
), | ||
array( | ||
'elements' => array('%one', '%two%', 'three'), | ||
), | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Sulu\Component\DocumentManager\Tests\Unit; | ||
|
||
use Sulu\Component\DocumentManager\PathSegmentRegistry; | ||
use Sulu\Component\DocumentManager\PathBuilder; | ||
use PhpBench\Benchmark; | ||
|
||
class PathBuilderTest extends \PHPUnit_Framework_TestCase implements Benchmark | ||
{ | ||
private $pathBuilder; | ||
|
||
public function setUp() | ||
{ | ||
$pathRegistry = new PathSegmentRegistry(array( | ||
'one' => 'one', | ||
'two' => 'two', | ||
)); | ||
$this->pathBuilder = new PathBuilder($pathRegistry); | ||
} | ||
|
||
/** | ||
* @description Build path 1000 times | ||
* @revs 1000 | ||
* @beforeMethod setUp | ||
*/ | ||
public function benchBuild() | ||
{ | ||
$this->pathBuilder->build(array('%one%', '%two%', 'four')); | ||
} | ||
|
||
/** | ||
* It should build a path | ||
* Using a combination of tokens and literal values | ||
*/ | ||
public function testBuild() | ||
{ | ||
$result = $this->pathBuilder->build(array('%one%', '%two%', 'four')); | ||
$this->assertEquals('/one/two/four', $result); | ||
} | ||
|
||
/** | ||
* It should build "/" for an empty array | ||
*/ | ||
public function testBuildEmpty() | ||
{ | ||
$this->assertEquals('/', $this->pathBuilder->build(array())); | ||
} | ||
|
||
/** | ||
* It should build "/" for an array with "/" | ||
*/ | ||
public function testBuildSingleSlash() | ||
{ | ||
$this->assertEquals('/', $this->pathBuilder->build(array('/'))); | ||
} | ||
|
||
/** | ||
* It should replace "//" with "/" | ||
*/ | ||
public function testBuildNoDoubleSlash() | ||
{ | ||
$this->assertEquals('/hello/world', $this->pathBuilder->build(array('hello', '', '', 'world'))); | ||
} | ||
} |