Skip to content

Commit

Permalink
Merge pull request #28 from LIN3S/feature/multiple-persistence-strategy
Browse files Browse the repository at this point in the history
Feature/multiple persistence strategy
  • Loading branch information
benatespina authored Sep 27, 2017
2 parents 623ecae + b5b7847 commit c6894af
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 236 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ To get the diff for a specific change, go to https://github.com/LIN3S/AdminBundl
To get the diff between two versions, go to https://github.com/LIN3S/AdminBundle/compare/v0.4.0...v0.5.0

* 0.6.0
* Made AdminBundle compatible with multiple persistence strategies; for now is compatible with DoctrineORM and PDO.
* Added sticky behaviour to the form right sidebar.
* Made JavaScript and CSS code compatible with IE11.
* Imported `parsleyjs` module in the js entry file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,24 @@
use LIN3S\AdminBundle\Configuration\Model\ListField;
use LIN3S\AdminBundle\Configuration\Model\ListFilter;
use LIN3S\AdminBundle\Registry\ServiceRegistry;
use LIN3S\AdminBundle\Repository\QueryBuilder;

final class EntityConfigurationFactory
{
private $config;
private $actions;
private $listFields;
private $listFilters;
private $queryBuilder;

public function __construct(
$config,
ServiceRegistry $actions,
ServiceRegistry $listFields,
ServiceRegistry $listFilters,
QueryBuilder $queryBuilder
ServiceRegistry $listFilters
) {
$this->config = $config['entities'];
$this->actions = $actions;
$this->listFields = $listFields;
$this->listFilters = $listFilters;
$this->queryBuilder = $queryBuilder;
}

public function createFor($entity)
Expand All @@ -52,7 +48,7 @@ public function createFor($entity)
$this->listFieldsForEntity($entity),
$this->listFiltersForEntity($entity),
$entityConfig['list']['global_actions'],
$this->queryBuilder,
$entityConfig['repository_service_id'],
$entityConfig['name'],
$entityConfig['list']['amount_per_page'],
$entityConfig['list']['order_by']
Expand Down
136 changes: 32 additions & 104 deletions src/LIN3S/AdminBundle/Configuration/Model/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,109 +11,26 @@

namespace LIN3S\AdminBundle\Configuration\Model;

use LIN3S\AdminBundle\Repository\QueryBuilder;
use LIN3S\AdminBundle\Repository\AdminRepository;

/**
* Entity configuration.
*
* @author Gorka Laucirica <[email protected]>
*/
final class Entity
{
/**
* Collection of actions.
*
* @var array
*/
protected $actions;

/**
* The entity name.
*
* @var string
*/
protected $name;

/**
* The class name.
*
* @var string
*/
protected $className;

/**
* The list actions.
*
* @var array
*/
protected $listActions;

/**
* The list fields.
*
* @var array
*/
protected $listFields;

/**
* The list filters.
*
* @var array
*/
protected $listFilters;

/**
* The entities per page.
*
* @var int
*/
protected $listEntitiesPerPage;

/**
* The list global actions.
*
* @var array
*/
protected $listGlobalActions;

/**
* The list order by default.
*
* @var array
*/
protected $listOrderByDefault;

/**
* The entity name, for visualization purposes.
*
* It contains the singular and plural cases.
*
* @var array
*/
private $actions;
private $name;
private $className;
private $listActions;
private $listFields;
private $listFilters;
private $listEntitiesPerPage;
private $listGlobalActions;
private $listOrderByDefault;
private $printNames;
private $repositoryServiceId;
private $repository;

/**
* The query builder.
*
* @var QueryBuilder
*/
protected $queryBuilder;

/**
* EntityConfiguration constructor.
*
* @param string $name The entity name
* @param string $className The class name
* @param array $actions Collection of actions
* @param array $listActions List actions
* @param array $listFields List fields
* @param array $listFilters List filters
* @param array $listGlobalActions List global actions
* @param QueryBuilder $queryBuilder The query builder
* @param array $printNames The entity name, for visualization purposes
* @param int $listEntitiesPerPage The number of entities per page
* @param array $listOrderByDefault The order by default
*/
public function __construct(
$name,
$className,
Expand All @@ -122,7 +39,7 @@ public function __construct(
array $listFields = [],
array $listFilters = [],
array $listGlobalActions = [],
QueryBuilder $queryBuilder,
$repositoryServiceId,
array $printNames,
$listEntitiesPerPage,
array $listOrderByDefault = []
Expand All @@ -135,7 +52,7 @@ public function __construct(
$this->actions = $actions;
$this->listActions = $listActions;
$this->listGlobalActions = $listGlobalActions;
$this->queryBuilder = $queryBuilder;
$this->repositoryServiceId = $repositoryServiceId;
$this->printNames = $printNames;
$this->listEntitiesPerPage = $listEntitiesPerPage;
$this->listOrderByDefault = $listOrderByDefault;
Expand Down Expand Up @@ -365,13 +282,24 @@ public function printNames()
return $this->printNames;
}

/**
* Gets the query builder.
*
* @return QueryBuilder
*/
public function queryBuilder()
public function repositoryServiceId()
{
return $this->repositoryServiceId;
}

public function repository()
{
if (!$this->repository instanceof AdminRepository) {
throw new \Exception(
sprintf('The repository is not loaded yet')
);
}

return $this->repository;
}

public function loadRepository(AdminRepository $repository)
{
return $this->queryBuilder;
$this->repository = $repository;
}
}
21 changes: 14 additions & 7 deletions src/LIN3S/AdminBundle/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ class AdminController extends Controller
/**
* List action.
*
* @param string $entity The entity name
* @param Request $request The request
* @param string $entity The entity name
* @param Request $request The request
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function listAction($entity, Request $request)
{
$entityConfig = $this->get('lin3s_admin.configuration.factory.entity')->createFor($entity);
$entities = $this->get('lin3s_admin.repository')->findByRequest($request, $entityConfig);
$totalCount = $this->get('lin3s_admin.repository')->countAll($request, $entityConfig);
$repository = $this->get($entityConfig->repositoryServiceId());
$entities = $repository->findByRequest($request, $entityConfig);
$totalCount = $repository->countAll($request, $entityConfig);

return $this->render('@Lin3sAdmin/Admin/list.html.twig', [
'entities' => $entities,
Expand All @@ -56,9 +57,11 @@ public function customAction($entity, $action, $id = null, Request $request)
{
$entityConfig = $this->get('lin3s_admin.configuration.factory.entity')->createFor($entity);
$entityObject = null;
$repository = $this->get($entityConfig->repositoryServiceId());
$entityConfig->loadRepository($repository);

if ($id) {
$manager = $this->getDoctrine()->getRepository($entityConfig->className());
$entityObject = $manager->find($id);
$entityObject = $repository->find($entityConfig, $id);
}
if ($id && !$entityObject) {
throw $this->createNotFoundException(
Expand All @@ -73,7 +76,11 @@ public function customAction($entity, $action, $id = null, Request $request)
$callableAction = $entityConfig->getAction($action);
} catch (\Exception $e) {
throw $this->createNotFoundException(
sprintf('Action "%s" for entity "%s" not found, did you registered it in the config?', $action, $entity)
sprintf(
'Action "%s" for entity "%s" not found, did you registered it in the config?',
$action,
$entity
)
);
}

Expand Down
3 changes: 3 additions & 0 deletions src/LIN3S/AdminBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public function getConfigTreeBuilder()
->arrayNode('entities')->requiresAtLeastOneElement()
->prototype('array')
->children()
->scalarNode('repository_service_id')
->defaultValue('lin3s_admin.doctrine_repository')
->end()
->arrayNode('name')
->children()
->scalarNode('singular')->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function render($entity, $options, Entity $configuration)

$url = $this->urlGenerator->generate('lin3s_admin_custom', [
'entity' => $configuration->name(),
'id' => $entity->id(),
'id' => is_array($entity)? $entity['id'] : $entity->id(),
'action' => $action->name(),
]);

Expand Down
14 changes: 9 additions & 5 deletions src/LIN3S/AdminBundle/Extension/ListField/DateListFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ public function render($entity, $options, Entity $configuration)
throw new \InvalidArgumentException('Field to be rendered must be passed as string');
}

$properties = explode('.', $options['field']);

$value = $entity;
foreach ($properties as $property) {
$value = $value->$property();
if (is_array($entity)) {
$value = new \DateTimeImmutable($entity[$options['field']]);
} else {
$properties = explode('.', $options['field']);
$value = $entity;
foreach ($properties as $property) {
$value = $value->$property();
}
}

if (null === $value) {
return $this->translator->trans('lin3s_admin.list.field_type.date.not_available');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ public function render($entity, $options, Entity $configuration)
if (!isset($options['field'])) {
throw new \InvalidArgumentException('Field to be rendered must be passed as string');
}
$properties = explode('.', $options['field']);

$value = $entity;
foreach ($properties as $property) {
$value = $value->$property();
if (is_array($entity)) {
$value = $entity[$options['field']];
} else {
$properties = explode('.', $options['field']);
$value = $entity;
foreach ($properties as $property) {
$value = $value->$property();
}
}

return $this->translator->trans($value);
Expand Down
43 changes: 5 additions & 38 deletions src/LIN3S/AdminBundle/Repository/AdminRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,15 @@
use Symfony\Component\HttpFoundation\Request;

/**
* Admin repository.
*
* @author Gorka Laucirica <[email protected]>
*/
class AdminRepository
interface AdminRepository
{
/**
* Finds the result about request criteria and given entity configuration.
*
* @param Request $request The request
* @param Entity $config The entity configuration
*
* @return array
*/
public function findByRequest(Request $request, Entity $config)
{
$queryBuilder = $config->queryBuilder()->generate($request, $config);

$postPerPage = $config->listEntitiesPerPage();

$offset = ($request->get('page', 1) - 1) * $postPerPage;
$limit = $postPerPage;

$queryBuilder->setFirstResult($offset);
$queryBuilder->setMaxResults($limit);
public function find(Entity $config, $id);

return $queryBuilder->getQuery()->getResult();
}
public function remove($entity);

/**
* Counts all the result about request criteria and given entity configuration.
*
* @param Request $request The request
* @param Entity $config The entity configuration
*
* @return int
*/
public function countAll(Request $request, Entity $config)
{
$queryBuilder = $config->queryBuilder()->generate($request, $config);
$queryBuilder->select($queryBuilder->expr()->count('a'));
public function findByRequest(Request $request, Entity $config);

return count($queryBuilder->getQuery()->getScalarResult());
}
public function countAll(Request $request, Entity $config);
}
Loading

0 comments on commit c6894af

Please sign in to comment.