119 lines
4.1 KiB
PHP
119 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace Trinity\Bundle\ContentManagerBundle\Routing;
|
|
|
|
use Symfony\Component\Config\Loader\Loader;
|
|
use Symfony\Component\Routing\RouteCollection;
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
|
|
use Trinity\Bundle\ContentManagerBundle\Model\NavQuery;
|
|
use Trinity\Bundle\ContentManagerBundle\Model\PageableInterface;
|
|
|
|
class NodeLoader extends Loader
|
|
{
|
|
/**
|
|
* Loads a resource.
|
|
*
|
|
* @param mixed $resource The resource
|
|
* @param string $type The resource type
|
|
* Deprecated method
|
|
*/
|
|
public function load($resource, $type = null)
|
|
{
|
|
try {
|
|
$routes = new RouteCollection();
|
|
|
|
$nodes = \PropelQuery::from($resource)
|
|
->orderByTreeLeft()
|
|
->find();
|
|
|
|
$navCount = NavQuery::create()->groupByCulture()->count();
|
|
|
|
foreach ($nodes as $node) {
|
|
$defaults = $node->getCleanedDefaultParams();
|
|
$defaults['_controller'] = $node->getController() ? $node->getController(
|
|
) : 'TrinityContentManagerBundle:Page:catchAll';
|
|
$defaults['_node'] = $node->getId();
|
|
$requirements = $node->getCleanedRequirements();
|
|
|
|
$id = $node->getId();
|
|
$routeName = $node->getRealRouteName() ? $node->getRealRouteName() : sprintf('cms_node_%d', $id);
|
|
|
|
if ($node->getNodeAlias()) {
|
|
$p = $node;
|
|
$node = $node->getNodeRelatedByNodeAlias();
|
|
}
|
|
|
|
$url = $node->getUrl();
|
|
$page = $node->getPage();
|
|
$controller = $node->getController() ? $node->getController() : 'TrinityContentManagerBundle:Page:catchAll';
|
|
|
|
if ($node->getNodeAlias() && !$node->getNodeAlias()->getPage()) {
|
|
continue;
|
|
}
|
|
|
|
if ($page && !$node->getController()) {
|
|
$configuration = $page->getConfiguration();
|
|
$controller = $configuration->getDefaultController() ? $configuration->getDefaultController(
|
|
) : $controller;
|
|
}
|
|
|
|
$route = new NodeRoute($url, $defaults, $requirements);
|
|
$route->addNode($id);
|
|
$route->setController($controller);
|
|
|
|
if (!empty($page) && $page instanceof PageableInterface) {
|
|
$route->addPage();
|
|
}
|
|
|
|
if ($nav = $node->getNav()) {
|
|
$route->addDefaults(array('_locale' => $nav->getCulture()));
|
|
|
|
if ($navCount === 1) {
|
|
$route->removeLocale();
|
|
} else {
|
|
$route->addLocale($nav->getCulture());
|
|
}
|
|
|
|
if (!$nav->isRegexDomain()) {
|
|
$route->setHost($nav->getDomain());
|
|
} elseif ($nav->transformDomainRegex()) {
|
|
$route
|
|
->setHost($nav->getHost())
|
|
->addRequirements($nav->getHostRequirements())
|
|
;
|
|
}
|
|
}
|
|
|
|
// $node->setUrl( $route->getPath() )->save();
|
|
|
|
if ($exist = $routes->get($routeName)) {
|
|
if ($exist instanceof NodeRoute) {
|
|
$exist->addNode($id);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
$routes->add($routeName, $route);
|
|
}
|
|
} catch (\PropelException $e) {
|
|
throw new ResourceNotFoundException('Unable to find or load nodes. Please check your database access. ' . $e->getMessage(
|
|
));
|
|
}
|
|
|
|
return $routes;
|
|
}
|
|
|
|
/**
|
|
* Returns true if this class supports the given resource.
|
|
*
|
|
* @param mixed $resource A resource
|
|
* @param string $type The resource type
|
|
*
|
|
* @return Boolean true if this class supports the given resource, false otherwise
|
|
*/
|
|
public function supports($resource, $type = null)
|
|
{
|
|
return 'cms_node' === $type;
|
|
}
|
|
}
|