Skip to content

Commit

Permalink
Enhancement: Add more flexibility by not decorating default controller (
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert authored May 16, 2022
1 parent 98927a1 commit 47124ac
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ composer.lock

.php-version
.build/

###> intellij IDE ###
/.idea
sonata-multiupload-bundle.iml
###> intellij IDE ###
5 changes: 5 additions & 0 deletions src/Admin/MultiUploadAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SilasJoisten\Sonata\MultiUploadBundle\Admin;

use SilasJoisten\Sonata\MultiUploadBundle\Controller\CreateController;
use SilasJoisten\Sonata\MultiUploadBundle\Controller\MultiUploadController;
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
use Sonata\AdminBundle\Admin\AdminInterface;
Expand All @@ -16,5 +17,9 @@ public function configureRoutes(AdminInterface $admin, RouteCollectionInterface
$collection->add('multi_upload', 'multi-upload', [
'_controller' => sprintf('%s::multiUpload', MultiUploadController::class),
]);

$collection
->get('create')
->setDefault('_controller', sprintf('%s::createAction', CreateController::class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @phpstan-extends CRUDController<\Sonata\MediaBundle\Model\MediaInterface>
*/
final class MediaAdminController extends CRUDController
final class CreateController extends CRUDController
{
public function __construct(
private Pool $pool,
Expand Down
8 changes: 3 additions & 5 deletions src/Controller/MultiUploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use SilasJoisten\Sonata\MultiUploadBundle\Form\MultiUploadType;
use Sonata\AdminBundle\Controller\CRUDController;
use Sonata\Doctrine\Model\ManagerInterface;
use Sonata\MediaBundle\Admin\ORM\MediaAdmin;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
use Sonata\MediaBundle\Provider\Pool;
Expand All @@ -22,7 +21,6 @@ final class MultiUploadController extends CRUDController
public function __construct(
private FormFactoryInterface $formFactory,
private ManagerInterface $mediaManager,
private MediaAdmin $mediaAdmin,
private Pool $mediaProviderPool,
private Responder $responder,
private int $maxUploadSize,
Expand All @@ -32,7 +30,7 @@ public function __construct(

public function multiUpload(Request $request): Response
{
$this->mediaAdmin->checkAccess('create');
$this->admin->checkAccess('create');

$providerName = $request->query->get('provider');
$context = $request->query->get('context', 'default');
Expand Down Expand Up @@ -61,7 +59,7 @@ public function multiUpload(Request $request): Response
return $this->responder->json([
'status' => 'ok',
'path' => $provider->generatePublicUrl($media, MediaProviderInterface::FORMAT_ADMIN),
'edit' => $this->mediaAdmin->generateUrl('edit', ['id' => $media->getId()]),
'edit' => $this->admin->generateUrl('edit', ['id' => $media->getId()]),
'id' => $media->getId(),
]);
}
Expand All @@ -70,7 +68,7 @@ private function createMultiUploadForm(MediaProviderInterface $provider, string
{
return $this->formFactory->create(MultiUploadType::class, null, [
'data_class' => $this->mediaManager->getClass(),
'action' => $this->mediaAdmin->generateUrl('multi_upload', ['provider' => $provider->getName()]),
'action' => $this->admin->generateUrl('multi_upload', ['provider' => $provider->getName()]),
'provider' => $provider->getName(),
'context' => $context,
]);
Expand Down
4 changes: 1 addition & 3 deletions src/Resources/config/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ services:
SilasJoisten\Sonata\MultiUploadBundle\Controller\MultiUploadController:
arguments:
$mediaManager: '@sonata.media.manager.media'
$mediaAdmin: '@sonata.media.admin.media'
$mediaProviderPool: '@sonata.media.pool'
$maxUploadSize: '%sonata_multi_upload.max_upload_filesize%'
$redirectTo: '%sonata_multi_upload.redirect_to%'
Expand All @@ -12,8 +11,7 @@ services:
autowire: true
public: true

SilasJoisten\Sonata\MultiUploadBundle\Controller\MediaAdminController:
decorates: 'sonata.media.controller.media.admin'
SilasJoisten\Sonata\MultiUploadBundle\Controller\CreateController:
arguments: ['@sonata.media.pool']
public: true
tags: [ 'container.service_subscriber' ]
Expand Down

0 comments on commit 47124ac

Please sign in to comment.