-
Notifications
You must be signed in to change notification settings - Fork 240
Proof of concept for Columns as a Service #903
base: master
Are you sure you want to change the base?
Changes from 5 commits
aef5b93
7f18b5a
2d095fa
85e3c4b
3a1ffbe
fd1421a
b11f84f
fb2b2bd
547528d
e01a06d
ab5220a
7eb9547
2f3a00a
1d66575
eba9e2d
9189cab
44cfe0a
b2450a5
b9df98e
8f16674
2077465
3835505
bba15cd
661d4fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,17 @@ | |
|
||
namespace Sg\DatatablesBundle\Datatable\Column; | ||
|
||
use Sg\DatatablesBundle\Datatable\Factory; | ||
|
||
use Doctrine\Common\Persistence\Mapping\ClassMetadata; | ||
use Doctrine\ORM\Mapping\MappingException; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Sg\DatatablesBundle\Datatable\Factory; | ||
use Twig_Environment; | ||
use Exception; | ||
use function array_key_exists; | ||
use function sprintf; | ||
use function trigger_error; | ||
use const E_USER_DEPRECATED; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can also be removed, because it's a global constant |
||
use function get_class; | ||
|
||
/** | ||
* Class ColumnBuilder | ||
|
@@ -83,9 +87,12 @@ class ColumnBuilder | |
*/ | ||
private $entityClassName; | ||
|
||
//------------------------------------------------- | ||
// Ctor. | ||
//------------------------------------------------- | ||
/** | ||
* Column services | ||
* | ||
* @var \Symfony\Component\DependencyInjection\Argument\RewindableGenerator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change to iterable |
||
*/ | ||
private $columnTypes; | ||
|
||
/** | ||
* ColumnBuilder constructor. | ||
|
@@ -94,8 +101,9 @@ class ColumnBuilder | |
* @param Twig_Environment $twig | ||
* @param string $datatableName | ||
* @param EntityManagerInterface $em | ||
* @param iterable $columnTypes | ||
*/ | ||
public function __construct(ClassMetadata $metadata, Twig_Environment $twig, $datatableName, EntityManagerInterface $em) | ||
public function __construct(ClassMetadata $metadata, Twig_Environment $twig, $datatableName, EntityManagerInterface $em, iterable $columnTypes) | ||
{ | ||
$this->metadata = $metadata; | ||
$this->twig = $twig; | ||
|
@@ -106,6 +114,7 @@ public function __construct(ClassMetadata $metadata, Twig_Environment $twig, $da | |
$this->columnNames = array(); | ||
$this->uniqueColumns = array(); | ||
$this->entityClassName = $metadata->getName(); | ||
$this->columnTypes = $columnTypes; | ||
} | ||
|
||
//------------------------------------------------- | ||
|
@@ -124,7 +133,22 @@ public function __construct(ClassMetadata $metadata, Twig_Environment $twig, $da | |
*/ | ||
public function add($dql, $class, array $options = array()) | ||
{ | ||
$column = Factory::create($class, ColumnInterface::class); | ||
if (is_object($class)) { | ||
$column = Factory::create($class, ColumnInterface::class); | ||
@trigger_error(sprintf('Using an object as column type is deprecated since 1.3 and will be removed in 2.0. Use a class name (FQCN) instead.'), E_USER_DEPRECATED); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this going to be releases as a 1.3 version or could we add this to the 1.2 release |
||
} else { | ||
$columns = []; | ||
foreach ($this->columnTypes as $column) { | ||
$columns[get_class($column)] = $column; | ||
} | ||
|
||
if (! array_key_exists($class, $columns)) { | ||
throw new \RuntimeException(sprintf('Column %s is not a service', $class)); | ||
} | ||
|
||
$column = clone $columns[$class]; | ||
} | ||
|
||
$column->initOptions(); | ||
|
||
$this->handleDqlProperties($dql, $options, $column); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
services: | ||
sg_datatables.twig.extension: | ||
class: Sg\DatatablesBundle\Twig\DatatableTwigExtension | ||
public: false | ||
tags: | ||
- { name: twig.extension } | ||
_defaults: | ||
autoconfigure: true | ||
autowire: true | ||
|
||
sg_datatables.datatable.abstract: | ||
class: Sg\DatatablesBundle\Datatable\AbstractDatatable | ||
_instanceof: | ||
Sg\DatatablesBundle\Datatable\Column\Column: | ||
tags: ['sg_datatables.column'] | ||
|
||
Sg\DatatablesBundle\Datatable\Column\: | ||
resource: '../../Datatable/Column/*' | ||
|
||
Sg\DatatablesBundle\Datatable\AbstractDatatable: | ||
abstract: true | ||
arguments: | ||
- '@security.authorization_checker' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to specify service arguments because they can be autowired (or is this needed because of the latest argument for the colums, if so, we could reverse order of arguments). |
||
|
@@ -15,15 +19,14 @@ services: | |
- '@router' | ||
- '@doctrine.orm.entity_manager' | ||
- '@twig' | ||
- ! tagged sg_datatables.column | ||
|
||
sg_datatables.response: | ||
class: Sg\DatatablesBundle\Response\DatatableResponse | ||
Sg\DatatablesBundle\Response\DatatableResponse: | ||
public: true | ||
arguments: | ||
- '@request_stack' | ||
|
||
sg_datatables.factory: | ||
class: Sg\DatatablesBundle\Datatable\DatatableFactory | ||
Sg\DatatablesBundle\Datatable\DatatableFactory: | ||
public: true | ||
arguments: | ||
- '@security.authorization_checker' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to specify service arguments because they can be autowired |
||
|
@@ -32,3 +35,20 @@ services: | |
- '@router' | ||
- '@doctrine.orm.entity_manager' | ||
- '@twig' | ||
|
||
Sg\DatatablesBundle\Twig\DatatableTwigExtension: ~ | ||
|
||
# aliases for backwards compatibility, will be removed in 2.0 | ||
sg_datatables.twig.extension: | ||
alias: Sg\DatatablesBundle\Twig\DatatableTwigExtension | ||
|
||
sg_datatables.datatable.abstract: | ||
alias: Sg\DatatablesBundle\Datatable\AbstractDatatable | ||
|
||
sg_datatables.response: | ||
alias: Sg\DatatablesBundle\Response\DatatableResponse | ||
public: true | ||
|
||
sg_datatables.factory: | ||
alias: Sg\DatatablesBundle\Datatable\DatatableFactory | ||
public: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion, remove the
use function ....;
lines because they don't have any advantageThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove this. Let's discuss this in #912.