-
Notifications
You must be signed in to change notification settings - Fork 29
Massive refactoring #73
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2013 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Adapter; | ||
|
||
use Symfony\Cmf\Component\Routing\RouteObjectInterface; | ||
|
||
/** | ||
* Adapters will (eventually) abstract all database operations | ||
* with the aim of enabling other providers such as ORM. | ||
* | ||
* @author Daniel Leech <[email protected]> | ||
*/ | ||
interface AdapterInterface | ||
{ | ||
/** | ||
* Return locales for object | ||
* | ||
* @return array | ||
*/ | ||
public function getLocales($object); | ||
|
||
/** | ||
* Translate the given object into the given locale | ||
* | ||
* @param object $object | ||
* @param string $locale e.g. fr, en, de, be, etc. | ||
*/ | ||
public function translateObject($object, $locale); | ||
|
||
/** | ||
* Create a new auto route at the given path | ||
* with the given document as the content. | ||
* | ||
* @param string $path | ||
* @param object $document | ||
* | ||
* @return Route new route document | ||
*/ | ||
public function createRoute($path, $document); | ||
|
||
/** | ||
* Return the canonical name for the given class, this is | ||
* required as somethimes an ORM may return a proxy class. | ||
* | ||
* @return string | ||
*/ | ||
public function getRealClassName($className); | ||
|
||
/** | ||
* Return true if the content associated with the route | ||
* and the given content object are the same. | ||
* | ||
* @param RouteObjectInterface | ||
* @param object | ||
*/ | ||
public function compareRouteContent(RouteObjectInterface $route, $contentObject); | ||
|
||
/** | ||
* Attempt to find a route with the given URL | ||
* | ||
* @param string $url | ||
* | ||
* @return null|Symfony\Cmf\Component\Routing\RouteObjectInterface | ||
*/ | ||
public function findRouteForUrl($url); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2013 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Adapter; | ||
|
||
use Doctrine\ODM\PHPCR\DocumentManager; | ||
use Doctrine\ODM\PHPCR\Document\Generic; | ||
use Doctrine\Common\Util\ClassUtils; | ||
use Symfony\Cmf\Component\Routing\RouteObjectInterface; | ||
use PHPCR\Util\NodeHelper; | ||
use Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute; | ||
|
||
/** | ||
* Abstraction adapter for PHPCR-ODM | ||
* | ||
* This class will eventually encapsulate all of the PHPCR-ODM | ||
* specific logic to enable support for multiple backends. | ||
*/ | ||
class PhpcrOdmAdapter implements AdapterInterface | ||
{ | ||
protected $dm; | ||
protected $baseRoutePath; | ||
|
||
public function __construct(DocumentManager $dm, $routeBasePath) | ||
{ | ||
$this->dm = $dm; | ||
$this->baseRoutePath = $routeBasePath; | ||
} | ||
|
||
public function getLocales($contentDocument) | ||
{ | ||
if ($this->dm->isDocumentTranslatable($contentDocument)) { | ||
return $this->dm->getLocalesFor($contentDocument); | ||
} | ||
|
||
return array(); | ||
} | ||
|
||
public function translateObject($contentDocument, $locale) | ||
{ | ||
$meta = $this->dm->getMetadataFactory()->getMetadataFor(get_class($contentDocument)); | ||
$contentDocument = $this->dm->findTranslation($meta->getName(), $meta->getIdentifierValue($contentDocument), $locale); | ||
|
||
return $contentDocument; | ||
} | ||
|
||
public function createRoute($path, $contentDocument) | ||
{ | ||
$pathElements = explode('/', $path); | ||
$headName = array_pop($pathElements); | ||
$parentPath = implode('/', $pathElements); | ||
|
||
// bypass the ODM ... but changes will still only be | ||
// persisted when the PHPCR session is saved in the ODMs flush(). | ||
NodeHelper::createPath($this->dm->getPhpcrSession(), $parentPath); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This static call is a pain because I have to mock both a PHPCR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, whenever you try to cheat you get caught sooner or later :-( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created: phpcr/phpcr-utils#106 |
||
|
||
$autoRouteParent = $this->dm->find(null, $parentPath); | ||
|
||
if (!$autoRouteParent) { | ||
throw new \RuntimeException(sprintf( | ||
'Hmph, could not find parent path "%s", this really should not have happened.', | ||
$parentPath | ||
)); | ||
} | ||
|
||
$headRoute = new AutoRoute(); | ||
$headRoute->setContent($contentDocument); | ||
$headRoute->setName($headName); | ||
$headRoute->setParent($autoRouteParent); | ||
|
||
return $headRoute; | ||
} | ||
|
||
public function getRealClassName($className) | ||
{ | ||
return ClassUtils::getRealClass($className); | ||
} | ||
|
||
public function compareRouteContent(RouteObjectInterface $route, $contentDocument) | ||
{ | ||
if ($route->getContent() === $contentDocument) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this method would be needed or not -- it depends on if the method to get the routes associated content would differ from one adapter to another. |
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function getReferringRoutes($contentDocument) | ||
{ | ||
return $this->dm->getReferrers($contentDocument, null, null, null, 'Symfony\Cmf\Component\Routing\RouteObjectInterface'); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function findRouteForUrl($url) | ||
{ | ||
return $this->dm->find(null, $this->baseRoutePath . $url); | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wouterj we will need to add the route base path to the configuration -- but it would only apply to PHPCR-ODM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, let's add it :)