Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix configuration for Sulu 2.0.0 compatibility #42

Merged
merged 10 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 33 additions & 40 deletions Admin/RedirectAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@

use Sulu\Bundle\AdminBundle\Admin\Admin;
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItemCollection;
use Sulu\Bundle\AdminBundle\Admin\Routing\RouteBuilderFactoryInterface;
use Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderFactoryInterface;
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItem;
use Sulu\Bundle\AdminBundle\Admin\Routing\RouteCollection;
use Sulu\Bundle\AdminBundle\Admin\View\ViewCollection;
use Sulu\Bundle\AdminBundle\Admin\View\ToolbarAction;
use Sulu\Component\Security\Authorization\PermissionTypes;
use Sulu\Component\Security\Authorization\SecurityCheckerInterface;

Expand All @@ -26,16 +27,16 @@ class RedirectAdmin extends Admin
{
const SECURITY_CONTEXT = 'sulu.modules.redirects';

const LIST_ROUTE = 'sulu_redirect.list';
const LIST_VIEW = 'sulu_redirect.list';

const ADD_FORM_ROUTE = 'sulu_redirect.add_form';
const ADD_FORM_VIEW = 'sulu_redirect.add_form';

const EDIT_FORM_ROUTE = 'sulu_redirect.edit_form';
const EDIT_FORM_VIEW = 'sulu_redirect.edit_form';

/**
* @var RouteBuilderFactoryInterface
* @var ViewBuilderFactoryInterface
*/
private $routeBuilderFactory;
private $viewBuilderFactory;

/**
* @var SecurityCheckerInterface
Expand All @@ -44,12 +45,12 @@ class RedirectAdmin extends Admin

/**
* RedirectAdmin constructor.
* @param RouteBuilderFactoryInterface $routeBuilderFactory
* @param ViewBuilderFactoryInterface $viewBuilderFactory
* @param SecurityCheckerInterface $securityChecker
*/
public function __construct(RouteBuilderFactoryInterface $routeBuilderFactory, SecurityCheckerInterface $securityChecker)
public function __construct(ViewBuilderFactoryInterface $viewBuilderFactory, SecurityCheckerInterface $securityChecker)
{
$this->routeBuilderFactory = $routeBuilderFactory;
$this->viewBuilderFactory = $viewBuilderFactory;
$this->securityChecker = $securityChecker;
}

Expand All @@ -58,73 +59,65 @@ public function configureNavigationItems(NavigationItemCollection $navigationIte
if ($this->securityChecker->hasPermission(self::SECURITY_CONTEXT, PermissionTypes::VIEW)) {
$redirect = new NavigationItem('sulu_redirect.title');
$redirect->setPosition(51);
$redirect->setMainRoute(static::LIST_ROUTE);
$redirect->setView(static::LIST_VIEW);

$navigationItemCollection->get(Admin::SETTINGS_NAVIGATION_ITEM)->addChild($redirect);
}
}

/**
* {@inheritdoc}
*/
public function getJsBundleName()
{
return 'suluredirect';
}

public function configureRoutes(RouteCollection $routeCollection): void
public function configureViews(ViewCollection $viewCollection): void
{
$formToolbarActions = [
'sulu_admin.save',
'sulu_admin.delete',
new ToolbarAction('sulu_admin.save'),
new ToolbarAction('sulu_admin.delete'),
];

$listToolbarActions = [
'sulu_admin.add',
'sulu_admin.delete'
new ToolbarAction('sulu_admin.add'),
new ToolbarAction('sulu_admin.delete'),
];

$routeCollection->add(
$this->routeBuilderFactory->createListRouteBuilder(static::LIST_ROUTE, '/redirect-routes')
$viewCollection->add(
$this->viewBuilderFactory->createListViewBuilder(static::LIST_VIEW, '/redirect-routes')
->setResourceKey('redirect_routes')
->setListKey('redirect_routes')
->setTitle('sulu_redirect.title')
->addListAdapters(['table'])
->setAddRoute(static::ADD_FORM_ROUTE)
->setEditRoute(static::EDIT_FORM_ROUTE)
->setAddView(static::ADD_FORM_VIEW)
->setEditView(static::EDIT_FORM_VIEW)
->enableSearching()
->addToolbarActions($listToolbarActions)
);

$routeCollection->add(
$this->routeBuilderFactory->createResourceTabRouteBuilder(static::ADD_FORM_ROUTE, '/redirect-routes/add')
$viewCollection->add(
$this->viewBuilderFactory->createResourceTabViewBuilder(static::ADD_FORM_VIEW, '/redirect-routes/add')
->setResourceKey('redirect_routes')
->setBackRoute(static::LIST_ROUTE)
->setBackView(static::LIST_VIEW)
);

$routeCollection->add(
$this->routeBuilderFactory->createFormRouteBuilder('sulu_redirects.add_form.details', '/details')
$viewCollection->add(
$this->viewBuilderFactory->createFormViewBuilder('sulu_redirects.add_form.details', '/details')
->setResourceKey('redirect_routes')
->setFormKey('redirect_route_details')
->setTabTitle('sulu_admin.details')
->setEditRoute(static::EDIT_FORM_ROUTE)
->setEditView(static::EDIT_FORM_VIEW)
->addToolbarActions($formToolbarActions)
->setParent(static::ADD_FORM_ROUTE)
->setParent(static::ADD_FORM_VIEW)
);

$routeCollection->add($this->routeBuilderFactory->createResourceTabRouteBuilder(static::EDIT_FORM_ROUTE, '/redirect-routes/:id')
$viewCollection->add($this->viewBuilderFactory->createResourceTabViewBuilder(static::EDIT_FORM_VIEW, '/redirect-routes/:id')
->setResourceKey('redirect_routes')
->setBackRoute(static::LIST_ROUTE)
->setBackView(static::LIST_VIEW)
->setTitleProperty('name')
);

$routeCollection->add(
$this->routeBuilderFactory->createFormRouteBuilder('sulu_redirects.edit_form.details', '/details')
$viewCollection->add(
$this->viewBuilderFactory->createFormViewBuilder('sulu_redirects.edit_form.details', '/details')
->setResourceKey('redirect_routes')
->setFormKey('redirect_route_details')
->setTabTitle('sulu_admin.details')
->addToolbarActions($formToolbarActions)
->setParent(static::EDIT_FORM_ROUTE)
->setParent(static::EDIT_FORM_VIEW)
);
}

Expand Down
127 changes: 82 additions & 45 deletions Controller/RedirectRouteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@

namespace Sulu\Bundle\RedirectBundle\Controller;

use Doctrine\ORM\EntityManagerInterface;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Routing\ClassResourceInterface;
use Sulu\Bundle\RedirectBundle\Entity\RedirectRoute;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sulu\Bundle\RedirectBundle\Manager\RedirectRouteManagerInterface;
use Sulu\Bundle\RedirectBundle\Model\RedirectRouteInterface;
use Sulu\Bundle\RedirectBundle\Model\RedirectRouteRepositoryInterface;
use Sulu\Component\Rest\AbstractRestController;
use Sulu\Component\Rest\DoctrineRestHelper;
use Sulu\Component\Rest\Exception\EntityNotFoundException;
use Sulu\Component\Rest\ListBuilder\Doctrine\DoctrineListBuilderFactoryInterface;
use Sulu\Component\Rest\ListBuilder\FieldDescriptorInterface;
use Sulu\Component\Rest\ListBuilder\ListRepresentation;
use Sulu\Component\Rest\RestController;
use Sulu\Component\Rest\ListBuilder\Metadata\FieldDescriptorFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -28,10 +33,66 @@
*
* @RouteResource("redirect-route")
*/
class RedirectRouteController extends RestController implements ClassResourceInterface
class RedirectRouteController extends AbstractRestController implements ClassResourceInterface
{
const RESULT_KEY = 'redirect_routes';

/**
* @var DoctrineRestHelper
*/
protected $restHelper;

/**
* @var DoctrineListBuilderFactoryInterface
*/
protected $factory;

/**
* @var FieldDescriptorFactoryInterface
*/
protected $fieldDescriptor;

/**
* @var EntityManagerInterface
*/
protected $entityManager;

/**
* @var RedirectRouteManagerInterface
*/
protected $redirectRouteManager;

/**
* @var RedirectRouteRepositoryInterface
*/
protected $redirectRouteRepository;

/**
* @var string
*/
protected $tagEntityName;

public function __construct(
ViewHandlerInterface $viewHandler,
DoctrineRestHelper $restHelper,
DoctrineListBuilderFactoryInterface $factory,
FieldDescriptorFactoryInterface $fieldDescriptor,
EntityManagerInterface $entityManager,
RedirectRouteManagerInterface $redirectRouteManager,
RedirectRouteRepositoryInterface $redirectRouteRepository,
string $tagEntityName
)
{
parent::__construct($viewHandler);
$this->restHelper = $restHelper;
$this->factory = $factory;
$this->fieldDescriptor = $fieldDescriptor;
$this->entityManager = $entityManager;
$this->redirectRouteManager = $redirectRouteManager;
$this->redirectRouteRepository = $redirectRouteRepository;
$this->tagEntityName = $tagEntityName;
}

/**
* Returns redirect-routes.
*
Expand All @@ -41,17 +102,11 @@ class RedirectRouteController extends RestController implements ClassResourceInt
*/
public function cgetAction(Request $request)
{
$restHelper = $this->get('sulu_core.doctrine_rest_helper');
$factory = $this->get('sulu_core.doctrine_list_builder_factory');

$tagEntityName = $this->getParameter('sulu.model.redirect_route.class');

/** @var FieldDescriptorInterface[] $fieldDescriptors */
$fieldDescriptors = $this->get('sulu_core.list_builder.field_descriptor_factory')
->getFieldDescriptors('redirect_routes');
$listBuilder = $factory->create($tagEntityName);
$fieldDescriptors = $this->fieldDescriptor->getFieldDescriptors('redirect_routes');
$listBuilder = $this->factory->create($this->tagEntityName);

$restHelper->initializeListBuilder($listBuilder, $fieldDescriptors);
$this->restHelper->initializeListBuilder($listBuilder, $fieldDescriptors);
$results = $listBuilder->execute();

$list = new ListRepresentation(
Expand All @@ -78,8 +133,8 @@ public function postAction(Request $request)
{
$data = $request->request->all();

$redirectRoute= $this->getRedirectRouteManager()->saveByData($data);
$this->get('doctrine.orm.entity_manager')->flush();
$redirectRoute = $this->redirectRouteManager->saveByData($data);
$this->entityManager->flush();

return $this->handleView($this->view($redirectRoute));
}
Expand All @@ -95,9 +150,9 @@ public function postAction(Request $request)
*/
public function getAction($id)
{
$entity = $this->getRedirectRouteRepository()->find($id);
$entity = $this->redirectRouteRepository->find($id);
if (!$entity) {
throw new EntityNotFoundException($this->getParameter('sulu.model.redirect_route.class'), $id);
throw new EntityNotFoundException($this->tagEntityName, $id);
}

return $this->handleView($this->view($entity));
Expand All @@ -116,8 +171,8 @@ public function putAction($id, Request $request)
$data = $request->request->all();
$data['id'] = $id;

$redirectRoute = $this->getRedirectRouteManager()->saveByData($data);
$this->get('doctrine.orm.entity_manager')->flush();
$redirectRoute = $this->redirectRouteManager->saveByData($data);
$this->entityManager->flush();

return $this->handleView($this->view($redirectRoute));
}
Expand All @@ -133,13 +188,14 @@ public function putAction($id, Request $request)
*/
public function deleteAction($id)
{
$redirectRoute = $this->getRedirectRouteRepository()->find($id);
/** @var RedirectRouteInterface|null $redirectRoute */
$redirectRoute = $this->redirectRouteRepository->find($id);
if (!$redirectRoute) {
throw new EntityNotFoundException($this->getParameter('sulu.model.redirect_route.class'), $id);
throw new EntityNotFoundException($this->tagEntityName, $id);
}

$this->getRedirectRouteManager()->delete($redirectRoute);
$this->get('doctrine.orm.entity_manager')->flush();
$this->redirectRouteManager->delete($redirectRoute);
$this->entityManager->flush();

return $this->handleView($this->view());
}
Expand All @@ -153,11 +209,12 @@ public function deleteAction($id)
*/
public function cdeleteAction(Request $request)
{
$repository = $this->getRedirectRouteRepository();
$manager = $this->getRedirectRouteManager();
$repository = $this->redirectRouteRepository;
$manager = $this->redirectRouteManager;

$ids = array_filter(explode(',', $request->query->get('ids', '')));
foreach ($ids as $id) {
/** @var RedirectRouteInterface|null $redirectRoute */
$redirectRoute = $repository->find($id);
if (!$redirectRoute) {
continue;
Expand All @@ -166,28 +223,8 @@ public function cdeleteAction(Request $request)
$manager->delete($redirectRoute);
}

$this->get('doctrine.orm.entity_manager')->flush();
$this->entityManager->flush();

return $this->handleView($this->view());
}

/**
* Returns redirect-route manager.
*
* @return RedirectRouteManagerInterface
*/
protected function getRedirectRouteManager()
{
return $this->get('sulu_redirect.redirect_route_manager');
}

/**
* Returns redirect-route repository.
*
* @return RedirectRouteRepositoryInterface
*/
protected function getRedirectRouteRepository()
{
return $this->get('sulu.repository.redirect_route');
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ and code are not stable yet (pre 1.0).
## Requirements

* Composer
* PHP `^5.5 || ^7.0`
* Sulu `^1.5`
* PHP `^7.2`
* Sulu `^2.0.0`

For detailed requirements see [composer.json](https://github.com/sulu/SuluRedirectBundle/blob/master/composer.json).

Expand Down
9 changes: 0 additions & 9 deletions Resources/config/routing.xml

This file was deleted.

3 changes: 3 additions & 0 deletions Resources/config/routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sulu_redirect.import:
path: /import
defaults: { _controller: sulu_redirect.controller.import:importAction }
2 changes: 1 addition & 1 deletion Resources/config/routing_api.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
sulu_redirect.redirect_route:
type: rest
name_prefix: sulu_redirect.
resource: Sulu\Bundle\RedirectBundle\Controller\RedirectRouteController
resource: sulu_redirect.redirect_route_controller
options:
expose: true
Loading