-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This will guess the field description type from its properties.
- Loading branch information
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |