|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony CMF package. |
| 5 | + * |
| 6 | + * (c) 2011-2013 Symfony CMF |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Adapter; |
| 13 | + |
| 14 | +use Doctrine\ODM\PHPCR\DocumentManager; |
| 15 | +use Doctrine\ODM\PHPCR\Document\Generic; |
| 16 | +use Doctrine\Common\Util\ClassUtils; |
| 17 | +use PHPCR\InvalidItemStateException; |
| 18 | +use Symfony\Cmf\Component\RoutingAuto\Model\AutoRouteInterface; |
| 19 | +use Symfony\Cmf\Component\RoutingAuto\UrlContext; |
| 20 | +use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RedirectRoute; |
| 21 | +use Symfony\Cmf\Component\RoutingAuto\AdapterInterface; |
| 22 | + |
| 23 | +/** |
| 24 | + * Adapter for PHPCR-ODM |
| 25 | + * |
| 26 | + * @author Daniel Leech <[email protected]> |
| 27 | + */ |
| 28 | +class PhpcrOdmAdapter implements AdapterInterface |
| 29 | +{ |
| 30 | + const TAG_NO_MULTILANG = 'no-multilang'; |
| 31 | + |
| 32 | + protected $dm; |
| 33 | + protected $baseRoutePath; |
| 34 | + protected $autoRouteFqcn; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param DocumentManager $dm |
| 38 | + * @param string $routeBasePath Route path for all routes |
| 39 | + * @param string $autoRouteFqcn The FQCN of the AutoRoute document to use |
| 40 | + */ |
| 41 | + public function __construct(DocumentManager $dm, $routeBasePath, $autoRouteFqcn = 'Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute') |
| 42 | + { |
| 43 | + $this->dm = $dm; |
| 44 | + $this->baseRoutePath = $routeBasePath; |
| 45 | + |
| 46 | + $reflection = new \ReflectionClass($autoRouteFqcn); |
| 47 | + if (!$reflection->isSubclassOf('Symfony\Cmf\Component\RoutingAuto\Model\AutoRouteInterface')) { |
| 48 | + throw new \InvalidArgumentException(sprintf('AutoRoute documents have to implement the AutoRouteInterface, "%s" does not.', $autoRouteFqcn)); |
| 49 | + } |
| 50 | + |
| 51 | + $this->autoRouteFqcn = $autoRouteFqcn; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * {@inheritDoc} |
| 56 | + */ |
| 57 | + public function getLocales($contentDocument) |
| 58 | + { |
| 59 | + if ($this->dm->isDocumentTranslatable($contentDocument)) { |
| 60 | + return $this->dm->getLocalesFor($contentDocument); |
| 61 | + } |
| 62 | + |
| 63 | + return array(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * {@inheritDoc} |
| 68 | + */ |
| 69 | + public function translateObject($contentDocument, $locale) |
| 70 | + { |
| 71 | + $meta = $this->dm->getMetadataFactory()->getMetadataFor(get_class($contentDocument)); |
| 72 | + $contentDocument = $this->dm->findTranslation($meta->getName(), $meta->getIdentifierValue($contentDocument), $locale); |
| 73 | + |
| 74 | + return $contentDocument; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * {@inheritDoc} |
| 79 | + */ |
| 80 | + public function generateAutoRouteTag(UrlContext $urlContext) |
| 81 | + { |
| 82 | + return $urlContext->getLocale() ? : self::TAG_NO_MULTILANG; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * {@inheritDoc} |
| 87 | + */ |
| 88 | + public function removeDefunctRoute(AutoRouteInterface $autoRoute, $newRoute) |
| 89 | + { |
| 90 | + $session = $this->dm->getPhpcrSession(); |
| 91 | + try { |
| 92 | + $node = $this->dm->getNodeForDocument($autoRoute); |
| 93 | + $newNode = $this->dm->getNodeForDocument($newRoute); |
| 94 | + } catch (InvalidItemStateException $e) { |
| 95 | + // nothing .. |
| 96 | + } |
| 97 | + |
| 98 | + $session->save(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * {@inheritDoc} |
| 103 | + */ |
| 104 | + public function migrateAutoRouteChildren(AutoRouteInterface $srcAutoRoute, AutoRouteInterface $destAutoRoute) |
| 105 | + { |
| 106 | + $session = $this->dm->getPhpcrSession(); |
| 107 | + $srcAutoRouteNode = $this->dm->getNodeForDocument($srcAutoRoute); |
| 108 | + $destAutoRouteNode = $this->dm->getNodeForDocument($destAutoRoute); |
| 109 | + |
| 110 | + $srcAutoRouteChildren = $srcAutoRouteNode->getNodes(); |
| 111 | + |
| 112 | + foreach ($srcAutoRouteChildren as $srcAutoRouteChild) { |
| 113 | + $session->move($srcAutoRouteChild->getPath(), $destAutoRouteNode->getPath() . '/' . $srcAutoRouteChild->getName()); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * {@inheritDoc} |
| 119 | + */ |
| 120 | + public function removeAutoRoute(AutoRouteInterface $autoRoute) |
| 121 | + { |
| 122 | + $session = $this->dm->getPhpcrSession(); |
| 123 | + $node = $this->dm->getNodeForDocument($autoRoute); |
| 124 | + $session->removeItem($node->getPath()); |
| 125 | + $session->save(); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * {@inheritDoc} |
| 130 | + */ |
| 131 | + public function createAutoRoute($url, $contentDocument, $autoRouteTag) |
| 132 | + { |
| 133 | + $path = $this->baseRoutePath; |
| 134 | + $parentDocument = $this->dm->find(null, $path); |
| 135 | + $segments = preg_split('#/#', $url, null, PREG_SPLIT_NO_EMPTY); |
| 136 | + $headName = array_pop($segments); |
| 137 | + foreach ($segments as $segment) { |
| 138 | + $path .= '/' . $segment; |
| 139 | + $document = $this->dm->find(null, $path); |
| 140 | + |
| 141 | + if (null === $document) { |
| 142 | + $document = new Generic(); |
| 143 | + $document->setParent($parentDocument); |
| 144 | + $document->setNodeName($segment); |
| 145 | + $this->dm->persist($document); |
| 146 | + } |
| 147 | + $parentDocument = $document; |
| 148 | + } |
| 149 | + |
| 150 | + $headRoute = new $this->autoRouteFqcn(); |
| 151 | + $headRoute->setContent($contentDocument); |
| 152 | + $headRoute->setName($headName); |
| 153 | + $headRoute->setParent($document); |
| 154 | + $headRoute->setAutoRouteTag($autoRouteTag); |
| 155 | + |
| 156 | + return $headRoute; |
| 157 | + } |
| 158 | + |
| 159 | + private function buildParentPathForUrl($url) |
| 160 | + { |
| 161 | + |
| 162 | + return $document; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * {@inheritDoc} |
| 167 | + */ |
| 168 | + public function createRedirectRoute(AutoRouteInterface $referringAutoRoute, AutoRouteInterface $newRoute) |
| 169 | + { |
| 170 | + $parentDocument = $referringAutoRoute->getParent(); |
| 171 | + |
| 172 | + $redirectRoute = new RedirectRoute(); |
| 173 | + $redirectRoute->setName($referringAutoRoute->getName()); |
| 174 | + $redirectRoute->setRouteTarget($newRoute); |
| 175 | + $redirectRoute->setParent($parentDocument); |
| 176 | + |
| 177 | + $this->dm->persist($redirectRoute); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * {@inheritDoc} |
| 182 | + */ |
| 183 | + public function getRealClassName($className) |
| 184 | + { |
| 185 | + return ClassUtils::getRealClass($className); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * {@inheritDoc} |
| 190 | + */ |
| 191 | + public function compareAutoRouteContent(AutoRouteInterface $autoRoute, $contentDocument) |
| 192 | + { |
| 193 | + if ($autoRoute->getContent() === $contentDocument) { |
| 194 | + return true; |
| 195 | + } |
| 196 | + |
| 197 | + return false; |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * {@inheritDoc} |
| 202 | + */ |
| 203 | + public function getReferringAutoRoutes($contentDocument) |
| 204 | + { |
| 205 | + return $this->dm->getReferrers($contentDocument, null, null, null, 'Symfony\Cmf\Component\RoutingAuto\Model\AutoRouteInterface'); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * {@inheritDoc} |
| 210 | + */ |
| 211 | + public function findRouteForUrl($url) |
| 212 | + { |
| 213 | + $path = $this->getPathFromUrl($url); |
| 214 | + |
| 215 | + return $this->dm->find(null, $path); |
| 216 | + } |
| 217 | + |
| 218 | + private function getPathFromUrl($url) |
| 219 | + { |
| 220 | + return $this->baseRoutePath . $url; |
| 221 | + } |
| 222 | +} |
0 commit comments