Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Apr 5, 2021
1 parent 5131c90 commit 73fa85d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 80 deletions.
5 changes: 2 additions & 3 deletions src/Action/SetObjectFieldValueAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ public function __invoke(Request $request): JsonResponse
$rootObject = $object;

// If property path has more than 1 element, take the last object in order to validate it
if ($propertyPath->getLength() > 1) {
$parent = $propertyPath->getParent();
\assert(null !== $parent);
$parent = $propertyPath->getParent();
if (null !== $parent) {
$object = $this->propertyAccessor->getValue($object, $parent);

$elements = $propertyPath->getElements();
Expand Down
137 changes: 79 additions & 58 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,6 @@ abstract class AbstractAdmin extends AbstractTaggedAdmin implements AdminInterfa
*/
protected $menu;

/**
* @var array<string, bool>
*/
protected $loaded = [
'routes' => false,
'tab_menu' => false,
'show' => false,
'list' => false,
'form' => false,
'datagrid' => false,
];

/**
* @var string[]
*/
Expand Down Expand Up @@ -362,6 +350,18 @@ abstract class AbstractAdmin extends AbstractTaggedAdmin implements AdminInterfa
*/
private $showTabs = [];

/**
* @var array<string, bool>
*/
private $loaded = [
'routes' => false,
'tab_menu' => false,
'show' => false,
'list' => false,
'form' => false,
'datagrid' => false,
];

public function getExportFormats(): array
{
return [
Expand Down Expand Up @@ -520,7 +520,11 @@ final public function getFilterParameters(): array
}
}

if (!isset($parameters['_per_page']) || !$this->determinedPerPageValue($parameters['_per_page'])) {
if (
!isset($parameters['_per_page'])
|| !\is_int($parameters['_per_page'])
|| !$this->determinedPerPageValue($parameters['_per_page'])
) {
$parameters['_per_page'] = $this->getMaxPerPage();
}

Expand Down Expand Up @@ -787,10 +791,12 @@ final public function getBatchActions(): array

final public function getRoutes(): RouteCollectionInterface
{
$this->buildRoutes();
\assert(null !== $this->routes);
$routes = $this->buildRoutes();
if (null === $routes) {
throw new \LogicException('Cannot access routes during the building process.');
}

return $this->routes;
return $routes;
}

public function getRouterIdParameter(): string
Expand Down Expand Up @@ -961,20 +967,22 @@ final public function getObject($id): ?object

final public function getForm(): FormInterface
{
$this->buildForm();

\assert(null !== $this->form);
$form = $this->buildForm();
if (null === $form) {
throw new \LogicException('Cannot access form during the building process.');
}

return $this->form;
return $form;
}

final public function getList(): FieldDescriptionCollection
{
$this->buildList();

\assert(null !== $this->list);
$list = $this->buildList();
if (null === $list) {
throw new \LogicException('Cannot access list during the building process.');
}

return $this->list;
return $list;
}

final public function createQuery(): ProxyQueryInterface
Expand All @@ -991,11 +999,12 @@ final public function createQuery(): ProxyQueryInterface

final public function getDatagrid(): DatagridInterface
{
$this->buildDatagrid();

\assert(null !== $this->datagrid);
$datagrid = $this->buildDatagrid();
if (null === $datagrid) {
throw new \LogicException('Cannot access datagrid during the building process.');
}

return $this->datagrid;
return $datagrid;
}

final public function getSideMenu(string $action, ?AdminInterface $childAdmin = null): ItemInterface
Expand All @@ -1004,10 +1013,12 @@ final public function getSideMenu(string $action, ?AdminInterface $childAdmin =
return $this->getParent()->getSideMenu($action, $this);
}

$this->buildTabMenu($action, $childAdmin);
\assert(null !== $this->menu);
$menu = $this->buildTabMenu($action, $childAdmin);
if (null === $menu) {
throw new \LogicException('Cannot access menu during the building process.');
}

return $this->menu;
return $menu;
}

final public function getRootCode(): string
Expand Down Expand Up @@ -1627,10 +1638,12 @@ public function id(object $model): ?string

final public function getShow(): FieldDescriptionCollection
{
$this->buildShow();
\assert(null !== $this->show);
$show = $this->buildShow();
if (null === $show) {
throw new \LogicException('Cannot access show during the building process.');
}

return $this->show;
return $show;
}

final public function setFormTheme(array $formTheme): void
Expand Down Expand Up @@ -1695,12 +1708,10 @@ public function getPerPageOptions(): array

/**
* Returns true if the per page value is allowed, false otherwise.
*
* @param mixed $perPage
*/
final public function determinedPerPageValue($perPage): bool
final public function determinedPerPageValue(int $perPage): bool
{
return \is_int($perPage) && \in_array($perPage, $this->getPerPageOptions(), true);
return \in_array($perPage, $this->getPerPageOptions(), true);
}

final public function isAclEnabled(): bool
Expand Down Expand Up @@ -2287,10 +2298,10 @@ final protected function appendParentObject(object $object): void
}
}

private function buildDatagrid(): void
private function buildDatagrid(): ?DatagridInterface
{
if ($this->loaded['datagrid']) {
return;
return $this->datagrid;
}

$this->loaded['datagrid'] = true;
Expand Down Expand Up @@ -2342,12 +2353,14 @@ private function buildDatagrid(): void
foreach ($this->getExtensions() as $extension) {
$extension->configureDatagridFilters($mapper);
}

return $this->datagrid;
}

private function buildShow(): void
private function buildShow(): ?FieldDescriptionCollection
{
if ($this->loaded['show']) {
return;
return $this->show;
}

$this->loaded['show'] = true;
Expand All @@ -2360,12 +2373,14 @@ private function buildShow(): void
foreach ($this->getExtensions() as $extension) {
$extension->configureShowFields($mapper);
}

return $this->show;
}

private function buildList(): void
private function buildList(): ?FieldDescriptionCollection
{
if ($this->loaded['list']) {
return;
return $this->list;
}

$this->loaded['list'] = true;
Expand Down Expand Up @@ -2410,23 +2425,27 @@ private function buildList(): void

$mapper->add($fieldDescription, ListMapper::TYPE_SELECT);
}

return $this->list;
}

private function buildForm(): void
private function buildForm(): ?FormInterface
{
if ($this->loaded['form']) {
return;
return $this->form;
}

$this->loaded['form'] = true;

$this->form = $this->getFormBuilder()->getForm();

return $this->form;
}

private function buildRoutes(): void
private function buildRoutes(): ?RouteCollectionInterface
{
if ($this->loaded['routes']) {
return;
return $this->routes;
}

$this->loaded['routes'] = true;
Expand All @@ -2445,34 +2464,36 @@ private function buildRoutes(): void
foreach ($this->getExtensions() as $extension) {
$extension->configureRoutes($this, $this->routes);
}

return $this->routes;
}

/**
* @phpstan-param AdminInterface<object>|null $childAdmin
*/
private function buildTabMenu(string $action, ?AdminInterface $childAdmin = null): void
private function buildTabMenu(string $action, ?AdminInterface $childAdmin = null): ?ItemInterface
{
if ($this->loaded['tab_menu']) {
return;
return $this->menu;
}

$this->loaded['tab_menu'] = true;

$menu = $this->getMenuFactory()->createItem('root');
$menu->setChildrenAttribute('class', 'nav navbar-nav');
$menu->setExtra('translation_domain', $this->getTranslationDomain());
$this->menu = $this->getMenuFactory()->createItem('root');
$this->menu->setChildrenAttribute('class', 'nav navbar-nav');
$this->menu->setExtra('translation_domain', $this->getTranslationDomain());

// Prevents BC break with KnpMenuBundle v1.x
if (method_exists($menu, 'setCurrentUri')) {
$menu->setCurrentUri($this->getRequest()->getBaseUrl().$this->getRequest()->getPathInfo());
if (method_exists($this->menu, 'setCurrentUri')) {
$this->menu->setCurrentUri($this->getRequest()->getBaseUrl().$this->getRequest()->getPathInfo());
}

$this->configureTabMenu($menu, $action, $childAdmin);
$this->configureTabMenu($this->menu, $action, $childAdmin);

foreach ($this->getExtensions() as $extension) {
$extension->configureTabMenu($this, $menu, $action, $childAdmin);
$extension->configureTabMenu($this, $this->menu, $action, $childAdmin);
}

$this->menu = $menu;
return $this->menu;
}
}
Loading

0 comments on commit 73fa85d

Please sign in to comment.