This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored to support multiple nodes
- Loading branch information
Showing
6 changed files
with
88 additions
and
309 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 |
---|---|---|
|
@@ -12,21 +12,20 @@ | |
namespace Symfony\Cmf\Component\Resource\Repository; | ||
|
||
use DTL\Glob\FinderInterface; | ||
use Puli\Repository\Api\ChangeStream\VersionList; | ||
use Puli\Repository\Api\NoVersionFoundException; | ||
use Puli\Repository\Api\ResourceNotFoundException; | ||
use Puli\Repository\Api\ResourceRepository; | ||
use Puli\Repository\Api\UnsupportedLanguageException; | ||
use Puli\Repository\Resource\Collection\ArrayResourceCollection; | ||
use Webmozart\Assert\Assert; | ||
use Webmozart\PathUtil\Path; | ||
use Puli\Repository\AbstractRepository; | ||
use Symfony\Cmf\Component\Resource\Repository\Api\EditableRepository; | ||
|
||
/** | ||
* Abstract repository for both PHPCR and PHPCR-ODM repositories. | ||
* | ||
* @author Daniel Leech <[email protected]> | ||
*/ | ||
abstract class AbstractPhpcrRepository implements ResourceRepository, CmfEditableRepository | ||
abstract class AbstractPhpcrRepository extends AbstractRepository implements ResourceRepository, EditableRepository | ||
{ | ||
/** | ||
* Base path from which to serve nodes / nodes. | ||
|
@@ -73,6 +72,39 @@ public function find($query, $language = 'glob') | |
return $this->buildCollection($nodes); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function remove($query, $language = 'glob') | ||
{ | ||
$this->failUnlessGlob($language); | ||
Assert::notEq('', trim($query, '/'), 'The root directory cannot be deleted.'); | ||
$nodes = $this->finder->find($this->resolvePath($query)); | ||
|
||
// delegate remove nodes to the implementation | ||
$this->removeNodes($nodes); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function move($sourceQuery, $targetPath, $language = 'glob') | ||
{ | ||
$this->failUnlessGlob($language); | ||
Assert::notEq('', trim($sourceQuery, '/'), 'The root directory cannot be moved.'); | ||
$nodes = $this->finder->find($this->resolvePath($query)); | ||
|
||
$this->moveNodes($nodes, $sourceQuery, $targetPath); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clear() | ||
{ | ||
throw new \BadMethodCallException('Clear not supported'); | ||
} | ||
|
||
/** | ||
* Return the path with the basePath prefix | ||
* if it has been set. | ||
|
@@ -83,8 +115,7 @@ public function find($query, $language = 'glob') | |
*/ | ||
protected function resolvePath($path) | ||
{ | ||
Assert::stringNotEmpty($path, 'The path must be a non-empty string. Got: %s'); | ||
Assert::startsWith($path, '/', 'The path %s is not absolute.'); | ||
$path = $this->sanitizePath($path); | ||
|
||
if ($this->basePath) { | ||
$path = $this->basePath.$path; | ||
|
@@ -117,49 +148,16 @@ protected function unresolvePath($path) | |
abstract protected function buildCollection(array $nodes); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getVersions($path) | ||
{ | ||
try { | ||
return new VersionList($path, [$this->get($path)]); | ||
} catch (ResourceNotFoundException $e) { | ||
throw NoVersionFoundException::forPath($path, $e); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function remove($query, $language = 'glob') | ||
{ | ||
$this->failUnlessGlob($language); | ||
|
||
Assert::startsWith($query, '/', 'The target path %s is not absolute.'); | ||
Assert::notEq('', trim($query, '/'), 'The root directory cannot be deleted.'); | ||
$resolvedPath = $this->resolvePath($query); | ||
|
||
return $this->removeResource($resolvedPath); | ||
} | ||
|
||
/** | ||
* Validate a language is usable to search in repositories. | ||
* Rmeove the given nodes. | ||
* | ||
* @param string $language | ||
* @param NodeInterface[] | ||
*/ | ||
protected function failUnlessGlob($language) | ||
{ | ||
if ('glob' !== $language) { | ||
throw UnsupportedLanguageException::forLanguage($language); | ||
} | ||
} | ||
abstract protected function removeNodes($nodes); | ||
|
||
/** | ||
* Will finally remove the resource. | ||
* | ||
* @param string $sourcePath | ||
* Move the given nodes. | ||
* | ||
* @return int | ||
* @param NodeInterface[] | ||
*/ | ||
abstract protected function removeResource($sourcePath); | ||
abstract protected function moveNodes($nodes, $sourceQuery, $targetPath); | ||
} |
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 |
---|---|---|
|
@@ -9,30 +9,28 @@ | |
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Component\Resource\Repository; | ||
namespace Symfony\Cmf\Component\Resource\Repository\Api; | ||
|
||
use InvalidArgumentException; | ||
use Puli\Repository\Api\EditableRepository; | ||
use Puli\Repository\Api\UnsupportedLanguageException; | ||
|
||
/** | ||
* CMF own interface to add the move() method. | ||
* Extends the Puli editable repository to implement the as-of-yet not | ||
* implemented features. | ||
* | ||
* @author Maximilian Berghoff <[email protected]> | ||
*/ | ||
interface CmfEditableRepository extends EditableRepository | ||
interface EditableRepository extends EditableRepository | ||
{ | ||
/** | ||
* Moves a resource inside the repository. | ||
* Move all resources found by $sourceQuery to the target (parent) path. | ||
* | ||
* @param string $sourceQuery The Path of the current document. | ||
* @param string $targetPath The parent path of the destination. | ||
* @param string $sourceQuery | ||
* @param string $targetPath | ||
* @param string $language | ||
* | ||
* @return int | ||
* | ||
* @throws InvalidArgumentException If the sourceQuery is invalid. | ||
* @throws InvalidArgumentException If the resource can not be moved to the targetPath. | ||
* @throws UnsupportedLanguageException If the language is not supported. | ||
*/ | ||
public function move($sourceQuery, $targetPath, $language = 'glob'); | ||
|
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
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
Oops, something went wrong.