Skip to content

Commit c6894af

Browse files
authored
Merge pull request #28 from LIN3S/feature/multiple-persistence-strategy
Feature/multiple persistence strategy
2 parents 623ecae + b5b7847 commit c6894af

18 files changed

Lines changed: 258 additions & 236 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ To get the diff for a specific change, go to https://github.com/LIN3S/AdminBundl
66
To get the diff between two versions, go to https://github.com/LIN3S/AdminBundle/compare/v0.4.0...v0.5.0
77

88
* 0.6.0
9+
* Made AdminBundle compatible with multiple persistence strategies; for now is compatible with DoctrineORM and PDO.
910
* Added sticky behaviour to the form right sidebar.
1011
* Made JavaScript and CSS code compatible with IE11.
1112
* Imported `parsleyjs` module in the js entry file.

src/LIN3S/AdminBundle/Configuration/Factory/EntityConfigurationFactory.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,24 @@
1616
use LIN3S\AdminBundle\Configuration\Model\ListField;
1717
use LIN3S\AdminBundle\Configuration\Model\ListFilter;
1818
use LIN3S\AdminBundle\Registry\ServiceRegistry;
19-
use LIN3S\AdminBundle\Repository\QueryBuilder;
2019

2120
final class EntityConfigurationFactory
2221
{
2322
private $config;
2423
private $actions;
2524
private $listFields;
2625
private $listFilters;
27-
private $queryBuilder;
2826

2927
public function __construct(
3028
$config,
3129
ServiceRegistry $actions,
3230
ServiceRegistry $listFields,
33-
ServiceRegistry $listFilters,
34-
QueryBuilder $queryBuilder
31+
ServiceRegistry $listFilters
3532
) {
3633
$this->config = $config['entities'];
3734
$this->actions = $actions;
3835
$this->listFields = $listFields;
3936
$this->listFilters = $listFilters;
40-
$this->queryBuilder = $queryBuilder;
4137
}
4238

4339
public function createFor($entity)
@@ -52,7 +48,7 @@ public function createFor($entity)
5248
$this->listFieldsForEntity($entity),
5349
$this->listFiltersForEntity($entity),
5450
$entityConfig['list']['global_actions'],
55-
$this->queryBuilder,
51+
$entityConfig['repository_service_id'],
5652
$entityConfig['name'],
5753
$entityConfig['list']['amount_per_page'],
5854
$entityConfig['list']['order_by']

src/LIN3S/AdminBundle/Configuration/Model/Entity.php

Lines changed: 32 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -11,109 +11,26 @@
1111

1212
namespace LIN3S\AdminBundle\Configuration\Model;
1313

14-
use LIN3S\AdminBundle\Repository\QueryBuilder;
14+
use LIN3S\AdminBundle\Repository\AdminRepository;
1515

1616
/**
17-
* Entity configuration.
18-
*
1917
* @author Gorka Laucirica <gorka.lauzirika@gmail.com>
2018
*/
2119
final class Entity
2220
{
23-
/**
24-
* Collection of actions.
25-
*
26-
* @var array
27-
*/
28-
protected $actions;
29-
30-
/**
31-
* The entity name.
32-
*
33-
* @var string
34-
*/
35-
protected $name;
36-
37-
/**
38-
* The class name.
39-
*
40-
* @var string
41-
*/
42-
protected $className;
43-
44-
/**
45-
* The list actions.
46-
*
47-
* @var array
48-
*/
49-
protected $listActions;
50-
51-
/**
52-
* The list fields.
53-
*
54-
* @var array
55-
*/
56-
protected $listFields;
57-
58-
/**
59-
* The list filters.
60-
*
61-
* @var array
62-
*/
63-
protected $listFilters;
64-
65-
/**
66-
* The entities per page.
67-
*
68-
* @var int
69-
*/
70-
protected $listEntitiesPerPage;
71-
72-
/**
73-
* The list global actions.
74-
*
75-
* @var array
76-
*/
77-
protected $listGlobalActions;
78-
79-
/**
80-
* The list order by default.
81-
*
82-
* @var array
83-
*/
84-
protected $listOrderByDefault;
85-
86-
/**
87-
* The entity name, for visualization purposes.
88-
*
89-
* It contains the singular and plural cases.
90-
*
91-
* @var array
92-
*/
21+
private $actions;
22+
private $name;
23+
private $className;
24+
private $listActions;
25+
private $listFields;
26+
private $listFilters;
27+
private $listEntitiesPerPage;
28+
private $listGlobalActions;
29+
private $listOrderByDefault;
9330
private $printNames;
31+
private $repositoryServiceId;
32+
private $repository;
9433

95-
/**
96-
* The query builder.
97-
*
98-
* @var QueryBuilder
99-
*/
100-
protected $queryBuilder;
101-
102-
/**
103-
* EntityConfiguration constructor.
104-
*
105-
* @param string $name The entity name
106-
* @param string $className The class name
107-
* @param array $actions Collection of actions
108-
* @param array $listActions List actions
109-
* @param array $listFields List fields
110-
* @param array $listFilters List filters
111-
* @param array $listGlobalActions List global actions
112-
* @param QueryBuilder $queryBuilder The query builder
113-
* @param array $printNames The entity name, for visualization purposes
114-
* @param int $listEntitiesPerPage The number of entities per page
115-
* @param array $listOrderByDefault The order by default
116-
*/
11734
public function __construct(
11835
$name,
11936
$className,
@@ -122,7 +39,7 @@ public function __construct(
12239
array $listFields = [],
12340
array $listFilters = [],
12441
array $listGlobalActions = [],
125-
QueryBuilder $queryBuilder,
42+
$repositoryServiceId,
12643
array $printNames,
12744
$listEntitiesPerPage,
12845
array $listOrderByDefault = []
@@ -135,7 +52,7 @@ public function __construct(
13552
$this->actions = $actions;
13653
$this->listActions = $listActions;
13754
$this->listGlobalActions = $listGlobalActions;
138-
$this->queryBuilder = $queryBuilder;
55+
$this->repositoryServiceId = $repositoryServiceId;
13956
$this->printNames = $printNames;
14057
$this->listEntitiesPerPage = $listEntitiesPerPage;
14158
$this->listOrderByDefault = $listOrderByDefault;
@@ -365,13 +282,24 @@ public function printNames()
365282
return $this->printNames;
366283
}
367284

368-
/**
369-
* Gets the query builder.
370-
*
371-
* @return QueryBuilder
372-
*/
373-
public function queryBuilder()
285+
public function repositoryServiceId()
286+
{
287+
return $this->repositoryServiceId;
288+
}
289+
290+
public function repository()
291+
{
292+
if (!$this->repository instanceof AdminRepository) {
293+
throw new \Exception(
294+
sprintf('The repository is not loaded yet')
295+
);
296+
}
297+
298+
return $this->repository;
299+
}
300+
301+
public function loadRepository(AdminRepository $repository)
374302
{
375-
return $this->queryBuilder;
303+
$this->repository = $repository;
376304
}
377305
}

src/LIN3S/AdminBundle/Controller/AdminController.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ class AdminController extends Controller
2424
/**
2525
* List action.
2626
*
27-
* @param string $entity The entity name
28-
* @param Request $request The request
27+
* @param string $entity The entity name
28+
* @param Request $request The request
2929
*
3030
* @return \Symfony\Component\HttpFoundation\Response
3131
*/
3232
public function listAction($entity, Request $request)
3333
{
3434
$entityConfig = $this->get('lin3s_admin.configuration.factory.entity')->createFor($entity);
35-
$entities = $this->get('lin3s_admin.repository')->findByRequest($request, $entityConfig);
36-
$totalCount = $this->get('lin3s_admin.repository')->countAll($request, $entityConfig);
35+
$repository = $this->get($entityConfig->repositoryServiceId());
36+
$entities = $repository->findByRequest($request, $entityConfig);
37+
$totalCount = $repository->countAll($request, $entityConfig);
3738

3839
return $this->render('@Lin3sAdmin/Admin/list.html.twig', [
3940
'entities' => $entities,
@@ -56,9 +57,11 @@ public function customAction($entity, $action, $id = null, Request $request)
5657
{
5758
$entityConfig = $this->get('lin3s_admin.configuration.factory.entity')->createFor($entity);
5859
$entityObject = null;
60+
$repository = $this->get($entityConfig->repositoryServiceId());
61+
$entityConfig->loadRepository($repository);
62+
5963
if ($id) {
60-
$manager = $this->getDoctrine()->getRepository($entityConfig->className());
61-
$entityObject = $manager->find($id);
64+
$entityObject = $repository->find($entityConfig, $id);
6265
}
6366
if ($id && !$entityObject) {
6467
throw $this->createNotFoundException(
@@ -73,7 +76,11 @@ public function customAction($entity, $action, $id = null, Request $request)
7376
$callableAction = $entityConfig->getAction($action);
7477
} catch (\Exception $e) {
7578
throw $this->createNotFoundException(
76-
sprintf('Action "%s" for entity "%s" not found, did you registered it in the config?', $action, $entity)
79+
sprintf(
80+
'Action "%s" for entity "%s" not found, did you registered it in the config?',
81+
$action,
82+
$entity
83+
)
7784
);
7885
}
7986

src/LIN3S/AdminBundle/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public function getConfigTreeBuilder()
3232
->arrayNode('entities')->requiresAtLeastOneElement()
3333
->prototype('array')
3434
->children()
35+
->scalarNode('repository_service_id')
36+
->defaultValue('lin3s_admin.doctrine_repository')
37+
->end()
3538
->arrayNode('name')
3639
->children()
3740
->scalarNode('singular')->end()

src/LIN3S/AdminBundle/Extension/ListField/ActionsListFieldType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function render($entity, $options, Entity $configuration)
8989

9090
$url = $this->urlGenerator->generate('lin3s_admin_custom', [
9191
'entity' => $configuration->name(),
92-
'id' => $entity->id(),
92+
'id' => is_array($entity)? $entity['id'] : $entity->id(),
9393
'action' => $action->name(),
9494
]);
9595

src/LIN3S/AdminBundle/Extension/ListField/DateListFieldType.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,16 @@ public function render($entity, $options, Entity $configuration)
6060
throw new \InvalidArgumentException('Field to be rendered must be passed as string');
6161
}
6262

63-
$properties = explode('.', $options['field']);
64-
65-
$value = $entity;
66-
foreach ($properties as $property) {
67-
$value = $value->$property();
63+
if (is_array($entity)) {
64+
$value = new \DateTimeImmutable($entity[$options['field']]);
65+
} else {
66+
$properties = explode('.', $options['field']);
67+
$value = $entity;
68+
foreach ($properties as $property) {
69+
$value = $value->$property();
70+
}
6871
}
72+
6973
if (null === $value) {
7074
return $this->translator->trans('lin3s_admin.list.field_type.date.not_available');
7175
}

src/LIN3S/AdminBundle/Extension/ListField/StringListFieldType.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ public function render($entity, $options, Entity $configuration)
4141
if (!isset($options['field'])) {
4242
throw new \InvalidArgumentException('Field to be rendered must be passed as string');
4343
}
44-
$properties = explode('.', $options['field']);
4544

46-
$value = $entity;
47-
foreach ($properties as $property) {
48-
$value = $value->$property();
45+
if (is_array($entity)) {
46+
$value = $entity[$options['field']];
47+
} else {
48+
$properties = explode('.', $options['field']);
49+
$value = $entity;
50+
foreach ($properties as $property) {
51+
$value = $value->$property();
52+
}
4953
}
5054

5155
return $this->translator->trans($value);

src/LIN3S/AdminBundle/Repository/AdminRepository.php

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,15 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616

1717
/**
18-
* Admin repository.
19-
*
2018
* @author Gorka Laucirica <gorka.lauzirika@gmail.com>
2119
*/
22-
class AdminRepository
20+
interface AdminRepository
2321
{
24-
/**
25-
* Finds the result about request criteria and given entity configuration.
26-
*
27-
* @param Request $request The request
28-
* @param Entity $config The entity configuration
29-
*
30-
* @return array
31-
*/
32-
public function findByRequest(Request $request, Entity $config)
33-
{
34-
$queryBuilder = $config->queryBuilder()->generate($request, $config);
35-
36-
$postPerPage = $config->listEntitiesPerPage();
37-
38-
$offset = ($request->get('page', 1) - 1) * $postPerPage;
39-
$limit = $postPerPage;
40-
41-
$queryBuilder->setFirstResult($offset);
42-
$queryBuilder->setMaxResults($limit);
22+
public function find(Entity $config, $id);
4323

44-
return $queryBuilder->getQuery()->getResult();
45-
}
24+
public function remove($entity);
4625

47-
/**
48-
* Counts all the result about request criteria and given entity configuration.
49-
*
50-
* @param Request $request The request
51-
* @param Entity $config The entity configuration
52-
*
53-
* @return int
54-
*/
55-
public function countAll(Request $request, Entity $config)
56-
{
57-
$queryBuilder = $config->queryBuilder()->generate($request, $config);
58-
$queryBuilder->select($queryBuilder->expr()->count('a'));
26+
public function findByRequest(Request $request, Entity $config);
5927

60-
return count($queryBuilder->getQuery()->getScalarResult());
61-
}
28+
public function countAll(Request $request, Entity $config);
6229
}

0 commit comments

Comments
 (0)