[go: nahoru, domu]

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 5 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
7 changes: 4 additions & 3 deletions Datatable/AbstractDatatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Sg\DatatablesBundle\Datatable;

use Sg\DatatablesBundle\Datatable\Column\ColumnBuilder;

use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Translation\TranslatorInterface;
Expand Down Expand Up @@ -164,6 +163,7 @@ abstract class AbstractDatatable implements DatatableInterface
* @param RouterInterface $router
* @param EntityManagerInterface $em
* @param Twig_Environment $twig
* @param iterable $columnTypes
*
* @throws Exception
*/
Expand All @@ -173,7 +173,8 @@ public function __construct(
TranslatorInterface $translator,
RouterInterface $router,
EntityManagerInterface $em,
Twig_Environment $twig
Twig_Environment $twig,
iterable $columnTypes
) {
$this->validateName();

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

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

$this->ajax = new Ajax();
$this->options = new Options();
Expand Down
38 changes: 31 additions & 7 deletions Datatable/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

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 advantage

Copy link
Collaborator Author

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.

use function sprintf;
use function trigger_error;
use const E_USER_DEPRECATED;
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -83,9 +87,12 @@ class ColumnBuilder
*/
private $entityClassName;

//-------------------------------------------------
// Ctor.
//-------------------------------------------------
/**
* Column services
*
* @var \Symfony\Component\DependencyInjection\Argument\RewindableGenerator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to iterable

*/
private $columnTypes;

/**
* ColumnBuilder constructor.
Expand All @@ -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;
Expand All @@ -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;
}

//-------------------------------------------------
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Expand Down
10 changes: 5 additions & 5 deletions Datatable/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Factory
*/
public static function create($class, $interface)
{
if (empty($class) || !is_string($class) && !$class instanceof $interface) {
if (empty($class) || ( ! is_string($class) && ! $class instanceof $interface)) {
throw new Exception("Factory::create(): String or $interface expected.");
}

Expand All @@ -44,11 +44,11 @@ public static function create($class, $interface)

if (!$instance instanceof $interface) {
throw new Exception("Factory::create(): String or $interface expected.");
} else {
return $instance;
}
} else {
throw new Exception("Factory::create(): $class is not callable.");

return $instance;
}

throw new Exception("Factory::create(): $class is not callable.");
}
}
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\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
Expand All @@ -36,6 +37,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
42 changes: 31 additions & 11 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,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'
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

Expand All @@ -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
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 @@ -365,14 +365,19 @@ class PostDatatable extends AbstractDatatable
# 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 @@ -393,11 +398,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