Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Proof of concept for Columns as a Service #903

Draft
wants to merge 24 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
aef5b93
Very very early stage proof of concept of Columns as a Service
stephanvierkant Jul 3, 2019
7f18b5a
iterable
stephanvierkant Jul 16, 2019
2d095fa
wrong use statement
stephanvierkant Jul 17, 2019
85e3c4b
Support objects as column types to maintain backwards compatibility
stephanvierkant Jul 17, 2019
3a1ffbe
remove RewindableGenerator from typehinting since it's marked as inte…
stephanvierkant Jul 17, 2019
fd1421a
Merge remote-tracking branch 'upstream/master' into columns-as-a-service
stephanvierkant Aug 22, 2019
b11f84f
columnbuilder update
stephanvierkant Aug 22, 2019
fb2b2bd
DatatableFactory can be autowired
stephanvierkant Aug 22, 2019
547528d
LinkColumn fixed
stephanvierkant Aug 22, 2019
e01a06d
Merge branch 'master' into columns-as-a-service
stephanvierkant Aug 26, 2019
ab5220a
php-cs-fixer
stephanvierkant Aug 26, 2019
7eb9547
Merge remote-tracking branch 'upstream/master' into columns-as-a-service
stephanvierkant Sep 2, 2019
2f3a00a
Merge branch 'master' into columns-as-a-service
stephanvierkant Apr 6, 2020
1d66575
fix
stephanvierkant Apr 6, 2020
eba9e2d
Merge remote-tracking branch 'upstream/master' into columns-as-a-service
stephanvierkant Oct 20, 2020
9189cab
Merge branch 'master' of github.com:stwe/DatatablesBundle into column…
stephanvierkant Nov 26, 2021
44cfe0a
php 8.1 fix
stephanvierkant Nov 30, 2021
b2450a5
Merge branch 'stwe:master' into columns-as-a-service
stephanvierkant Jan 15, 2022
b9df98e
Update composer.json
stephanvierkant Jan 15, 2022
8f16674
PHP Deprecated: Creation of dynamic property Sg\DatatablesBundle\Res…
stephanvierkant Feb 17, 2023
2077465
Merge remote-tracking branch 'origin/columns-as-a-service' into colum…
stephanvierkant Feb 17, 2023
3835505
update
stephanvierkant Dec 19, 2023
bba15cd
allow symfony 7
stephanvierkant Dec 19, 2023
661d4fe
TreeBuilder as return type
stephanvierkant Feb 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Datatable/AbstractDatatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public function __construct(
$translator,
RouterInterface $router,
EntityManagerInterface $em,
Environment $twig
Environment $twig,
iterable $columnTypes
) {
$this->validateName();

Expand All @@ -177,7 +178,7 @@ public function __construct(
$this->twig = $twig;

$metadata = $em->getClassMetadata($this->getEntity());
$this->columnBuilder = new ColumnBuilder($metadata, $twig, $router, $this->getName(), $em);
$this->columnBuilder = new ColumnBuilder($metadata, $twig, $this->getName(), $em, $columnTypes);

$this->ajax = new Ajax();
$this->options = new Options();
Expand Down
8 changes: 0 additions & 8 deletions Datatable/Column/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,6 @@ abstract class AbstractColumn implements ColumnInterface
*/
protected $twig;

/**
* The Router.
* Is set in the ColumnBuilder.
*
* @var RouterInterface
*/
protected $router;

/**
* The position in the Columns array.
* Is set in the ColumnBuilder.
Expand Down
34 changes: 23 additions & 11 deletions Datatable/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ class ColumnBuilder
*/
private $twig;

/**
* The router.
*
* @var RouterInterface
*/
private $router;

/**
* The name of the associated Datatable.
*
Expand Down Expand Up @@ -85,21 +78,26 @@ class ColumnBuilder
*/
private $entityClassName;

/**
* @var iterable
*/
private $columnTypes;

/**
* @param string $datatableName
*/
public function __construct(ClassMetadata $metadata, Environment $twig, RouterInterface $router, $datatableName, EntityManagerInterface $em)
public function __construct(ClassMetadata $metadata, Environment $twig, $datatableName, EntityManagerInterface $em, iterable $columnTypes)
{
$this->metadata = $metadata;
$this->twig = $twig;
$this->router = $router;
$this->datatableName = $datatableName;
$this->em = $em;

$this->columns = [];
$this->columnNames = [];
$this->uniqueColumns = [];
$this->entityClassName = $metadata->getName();
$this->columnTypes = $columnTypes;
}

//-------------------------------------------------
Expand All @@ -118,7 +116,22 @@ public function __construct(ClassMetadata $metadata, Environment $twig, RouterIn
*/
public function add($dql, $class, array $options = [])
{
$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 and will be removed in 2.0. Use a class name (FQCN) instead.'), E_USER_DEPRECATED);
} 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);
Expand Down Expand Up @@ -269,7 +282,6 @@ private function setEnvironmentProperties(AbstractColumn $column)
$column->setDatatableName($this->datatableName);
$column->setEntityClassName($this->entityClassName);
$column->setTwig($this->twig);
$column->setRouter($this->router);

return $this;
}
Expand Down
11 changes: 11 additions & 0 deletions Datatable/Column/LinkColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sg\DatatablesBundle\Datatable\Filter\TextFilter;
use Sg\DatatablesBundle\Datatable\Helper;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\RouterInterface;

class LinkColumn extends AbstractColumn
{
Expand Down Expand Up @@ -68,6 +69,16 @@ class LinkColumn extends AbstractColumn
*/
protected $email;

/**
* @var RouterInterface
*/
private $router;

public function __construct(RouterInterface $router)
{
$this->router = $router;
}

//-------------------------------------------------
// ColumnInterface
//-------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions Datatable/Column/NumberColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ private function renderTemplate($data, $pk = null, $path = null)
}

$data = $this->formatter->formatCurrency($data, $this->currency);
} elseif ($data === null) {
$data = null;
} else {
// expected number (int or float), other values will be converted to a numeric value
$data = $this->formatter->format($data);
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Configuration implements ConfigurationInterface
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('sg_datatables');
if (method_exists($treeBuilder, 'getRootNode')) {
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/SgDatatablesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sg\DatatablesBundle\DependencyInjection;

use Sg\DatatablesBundle\Datatable\Column\ColumnInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
Expand All @@ -31,6 +32,9 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services.yml');

$container->setParameter('sg_datatables.datatable.query', $config['datatable']['query']);
$container->registerForAutoconfiguration(ColumnInterface::class)
->addTag('sg_datatables.column')
;
}

/**
Expand Down
47 changes: 30 additions & 17 deletions Resources/config/services.yml
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'
Copy link
Contributor

Choose a reason for hiding this comment

The 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).

Expand All @@ -15,20 +19,29 @@ 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\DatatablesBundle\Datatable\DatatableFactory:
public: true

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:
class: Sg\DatatablesBundle\Datatable\DatatableFactory
alias: Sg\DatatablesBundle\Datatable\DatatableFactory
public: true
arguments:
- '@security.authorization_checker'
- '@security.token_storage'
- '@translator'
- '@router'
- '@doctrine.orm.entity_manager'
- '@twig'
3 changes: 2 additions & 1 deletion Resources/doc/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ Doctrine Query when creating response for DataTable.

``` php
<?php
use Sg\DatatablesBundle\Response\DatatableResponse;
// ...
if ($isAjax) {
$responseService = $this->get('sg_datatables.response');
$responseService = $this->get(DatatableResponse::class);
$responseService->setDatatable($datatable);

$datatableQueryBuilder = $responseService->getDatatableQueryBuilder();
Expand Down
15 changes: 10 additions & 5 deletions Resources/doc/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,19 @@ When declaring datatable "by hand" as extending `AbstractDatatable` class watch
# app/config/services.yml

services:
app.datatable.post:
class: AppBundle\Datatables\PostDatatable
parent: sg_datatables.datatable.abstract
_defaults:
autoconfigure: true
autowire: true
AppBundle\Datatables\PostDatatable:
bind:
$columnTypes: !tagged sg_datatables.column
```

### Step 3: The Controller actions

``` php
use Sg\DatatablesBundle\Datatable\DatatableFactory;
use Sg\DatatablesBundle\Response\DatatableResponse;
/**
* Lists all Post entities.
*
Expand All @@ -398,11 +403,11 @@ public function indexAction(Request $request)

// or use the DatatableFactory
/** @var DatatableInterface $datatable */
$datatable = $this->get('sg_datatables.factory')->create(PostDatatable::class);
$datatable = $this->get(DatatableFactory::class)->create(PostDatatable::class);
$datatable->buildDatatable();

if ($isAjax) {
$responseService = $this->get('sg_datatables.response');
$responseService = $this->get(DatatableResponse::class);
$responseService->setDatatable($datatable);
$responseService->getDatatableQueryBuilder();

Expand Down
4 changes: 3 additions & 1 deletion Resources/doc/query.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ class Post
Now you can view all posts created by `root`. The additional `where statement` now works like a filter.

``` php
use Sg\DatatablesBundle\Response\DatatableResponse;

public function indexAction(Request $request)
{
// ...

if ($request->isXmlHttpRequest()) {
$responseService = $this->get('sg_datatables.response');
$responseService = $this->get(DatatableResponse::class);
$responseService->setDatatable($datatable);

$datatableQueryBuilder = $responseService->getDatatableQueryBuilder();
Expand Down
1 change: 1 addition & 0 deletions Response/DatatableQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class DatatableQueryBuilder
//-------------------------------------------------
// Ctor. && Init column arrays
//-------------------------------------------------
private $columnNames;

/**
* @throws Exception
Expand Down
24 changes: 12 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
"php": ">=7.2|>=8.0",
"doctrine/orm": "^2.5",
"friendsofsymfony/jsrouting-bundle": "^2.0|^3.0",
"symfony/config": "^4.4|^5.4|^6.0",
"symfony/dependency-injection": "^4.4|^5.4|^6.0",
"symfony/http-foundation": "^4.4|^5.4|^6.0",
"symfony/http-kernel": "^4.4|^5.4|^6.0",
"symfony/framework-bundle": "^4.4|^5.4|^6.0",
"symfony/options-resolver": "^4.4|^5.4|^6.0",
"symfony/property-access": "^4.4|^5.4|^6.0",
"symfony/routing": "^4.4|^5.4|^6.0",
"symfony/security-core": "^4.4|^5.4|^6.0",
"symfony/translation": "^4.4|^5.4|^6.0",
"symfony/config": "^4.4|^5.4|^6.0|^7.0",
"symfony/dependency-injection": "^4.4|^5.4|^6.0|^7.0",
"symfony/http-foundation": "^4.4|^5.4|^6.0|^7.0",
"symfony/http-kernel": "^4.4|^5.4|^6.0|^7.0",
"symfony/framework-bundle": "^4.4|^5.4|^6.0|^7.0",
"symfony/options-resolver": "^4.4|^5.4|^6.0|^7.0",
"symfony/property-access": "^4.4|^5.4|^6.0|^7.0",
"symfony/routing": "^4.4|^5.4|^6.0|^7.0",
"symfony/security-core": "^4.4|^5.4|^6.0|^7.0",
"symfony/translation": "^4.4|^5.4|^6.0|^7.0",
"twig/twig": "^2.9|^3.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9.5",
"friendsofphp/php-cs-fixer": "^3.6"
"phpunit/phpunit": "^7.5|^8.5",
"friendsofphp/php-cs-fixer": "^2.15"
},
"autoload": {
"psr-4": { "Sg\\DatatablesBundle\\": "" }
Expand Down