Skip to content

Commit

Permalink
Introduce TypeGuesserInterface
Browse files Browse the repository at this point in the history
This will guess the field description type from its properties.
  • Loading branch information
franmomu committed Feb 16, 2021
1 parent 72adc50 commit f52897b
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/FieldDescription/TypeGuesserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\FieldDescription;

use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Symfony\Component\Form\Guess\TypeGuess;

interface TypeGuesserInterface
{
public function guess(FieldDescriptionInterface $fieldDescription): TypeGuess;
}
69 changes: 69 additions & 0 deletions src/Request/AdminFetcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Request;

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\Pool;
use Symfony\Component\HttpFoundation\Request;

final class AdminFetcher
{
/**
* @var Pool
*/
private $pool;

public function __construct(Pool $pool)
{
$this->pool = $pool;
}

public function get(Request $request): AdminInterface
{
$adminCode = $request->get('_sonata_admin');

if (null === $adminCode) {
// NEXT_MAJOR: Change \RuntimeException by \InvalidArgumentException in the next line.
throw new \RuntimeException(sprintf(
'There is no `_sonata_admin` defined for the controller `%s` and the current route `%s`.',
static::class,
$request->get('_route')
));
}

$admin = $this->pool->getAdminByAdminCode($adminCode);

if (false === $admin) {
throw new \RuntimeException(sprintf(
'Unable to find the admin class related to the current controller (%s).',
static::class
));
}

$rootAdmin = $admin;

while ($rootAdmin->isChild()) {
$rootAdmin->setCurrentChild(true);
$rootAdmin = $rootAdmin->getParent();
}

$rootAdmin->setRequest($request);

if ($request->get('uniqid')) {
$admin->setUniqid($request->get('uniqid'));
}

return $admin;
}
}
49 changes: 49 additions & 0 deletions src/Request/ParamConverter/AdminParamConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Request\ParamConverter;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Request\AdminFetcher;
use Symfony\Component\HttpFoundation\Request;

final class AdminParamConverter implements ParamConverterInterface
{
/**
* @var AdminFetcher
*/
private $adminFetcher;

public function __construct(AdminFetcher $adminFetcher)
{
$this->adminFetcher = $adminFetcher;
}

public function apply(Request $request, ParamConverter $configuration): bool
{
$param = $configuration->getName();

$admin = $this->adminFetcher->get($request);

$request->attributes->set($param, $admin);

return true;
}

public function supports(ParamConverter $configuration): bool
{
return is_subclass_of($configuration->getClass(), AdminInterface::class);
}
}

0 comments on commit f52897b

Please sign in to comment.