Skip to content

Commit

Permalink
Merge branch 'release/5.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
pfilsx committed Dec 15, 2019
2 parents d4e15cc + 48be518 commit f561d04
Show file tree
Hide file tree
Showing 23 changed files with 209 additions and 72 deletions.
29 changes: 15 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"name": "pfilsx/data-grid-bundle",
"type": "symfony-bundle",
"license": "MIT",
"description": "Symfony 4 Data Grid rendering bundle.",
"description": "Provides a Data Grid tables for your Symfony project.",
"keywords": [
"datagrid",
"symfony4",
"symfony5",
"bundle",
"data-table",
"pagination",
Expand All @@ -22,23 +23,23 @@
"minimum-stability": "stable",
"require": {
"php": ">=7.1",
"symfony/framework-bundle": "^4.0",
"symfony/orm-pack": "^1.0",
"symfony/twig-bundle": "^4.0",
"symfony/dependency-injection": "^4.0",
"symfony/config": "^4.0",
"symfony/http-foundation": "^4.0",
"symfony/http-kernel": "^4.0",
"symfony/routing": "^4.0",
"symfony/asset": "^4.0",
"symfony/yaml": "^4.0",
"symfony/templating": "^4.0"
"symfony/framework-bundle": "^4.0 || ^5.0",
"symfony/orm-pack": "*",
"symfony/twig-bundle": "^4.0 || ^5.0",
"symfony/dependency-injection": "^4.0 || ^5.0",
"symfony/config": "^4.0 || ^5.0",
"symfony/http-foundation": "^4.0 || ^5.0",
"symfony/http-kernel": "^4.0 || ^5.0",
"symfony/routing": "^4.0 || ^5.0",
"symfony/asset": "^4.0 || ^5.0",
"symfony/yaml": "^4.0 || ^5.0",
"symfony/templating": "^4.0 || ^5.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0",
"phpunit/php-code-coverage": "^7.0",
"symfony/maker-bundle": "^1.0",
"symfony/translation": "^4.0"
"symfony/maker-bundle": "*",
"symfony/translation": "^4.0 || ^5.0"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 5 additions & 0 deletions src/DataGridBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
namespace Pfilsx\DataGrid;


use Pfilsx\DataGrid\DependencyInjection\Compiler\GridPass;
use Pfilsx\DataGrid\Grid\GridTypeInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

Expand All @@ -12,5 +15,7 @@ class DataGridBundle extends Bundle
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new GridPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 0);
$container->registerForAutoconfiguration(GridTypeInterface::class)->addTag('data_grid.type');
}
}
49 changes: 49 additions & 0 deletions src/DependencyInjection/Compiler/GridPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php


namespace Pfilsx\DataGrid\DependencyInjection\Compiler;


use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class GridPass implements CompilerPassInterface
{
private $gridExtensionService;
private $gridTypeTag;

public function __construct(string $gridExtensionService = 'data_grid.extension', string $gridTypeTag = 'data_grid.type')
{
$this->gridExtensionService = $gridExtensionService;
$this->gridTypeTag = $gridTypeTag;
}

public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition($this->gridExtensionService)) {
return;
}

$definition = $container->getDefinition($this->gridExtensionService);
$definition->replaceArgument(0, $this->processGridTypes($container));
}

private function processGridTypes(ContainerBuilder $container)
{
// Get service locator argument
$servicesMap = [];
$namespaces = [];

// Builds an array with fully-qualified type class names as keys and service IDs as values
foreach ($container->findTaggedServiceIds($this->gridTypeTag, true) as $serviceId => $tag) {
// Add form type service to the service locator
$serviceDefinition = $container->getDefinition($serviceId);
$servicesMap[$formType = $serviceDefinition->getClass()] = new Reference($serviceId);
$namespaces[substr($formType, 0, strrpos($formType, '\\'))] = true;
}

return ServiceLocatorTagPass::register($container, $servicesMap);
}
}
35 changes: 35 additions & 0 deletions src/Extension/DependencyInjection/DependencyInjectionExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php


namespace Pfilsx\DataGrid\Extension\DependencyInjection;


use InvalidArgumentException;
use Symfony\Component\DependencyInjection\ContainerInterface;

class DependencyInjectionExtension
{
/**
* @var ContainerInterface
*/
private $typeContainer;

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

public function getType($name)
{
if (!$this->typeContainer->has($name)) {
throw new InvalidArgumentException(sprintf('The field type "%s" is not registered in the service container.', $name));
}

return $this->typeContainer->get($name);
}

public function hasType($name)
{
return $this->typeContainer->has($name);
}
}
18 changes: 2 additions & 16 deletions src/Grid/AbstractGridType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Pfilsx\DataGrid\DataGridServiceContainer;

abstract class AbstractGridType
abstract class AbstractGridType implements GridTypeInterface
{
const ACTION_COLUMN = 'Pfilsx\DataGrid\Grid\Columns\ActionColumn';
const BOOLEAN_COLUMN = 'Pfilsx\DataGrid\Grid\Columns\BooleanColumn';
Expand All @@ -22,27 +22,13 @@ abstract class AbstractGridType
const FILTER_DATE = 'Pfilsx\DataGrid\Grid\Filters\DateFilter';
const FILTER_CUSTOM = 'Pfilsx\DataGrid\Grid\Filters\CustomFilter';

/**
* @var DataGridServiceContainer
*/
protected $container;
/**
* @var array
*/
protected $params;

/**
* AbstractGridType constructor.
* @param DataGridServiceContainer $container
* @param array $params
*/
public function __construct(DataGridServiceContainer $container, array $params = [])
public function setParams(array $params)
{
$this->container = $container;
$this->params = $params;
}

public abstract function buildGrid(DataGridBuilderInterface $builder): void;

public abstract function handleFilters(DataGridFiltersBuilderInterface $builder, array $filters): void;
}
4 changes: 2 additions & 2 deletions src/Grid/Columns/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Pfilsx\DataGrid\DataGridServiceContainer;
use Pfilsx\DataGrid\Grid\Filters\AbstractFilter;
use Twig\Template;
use Twig\TemplateWrapper;

abstract class AbstractColumn
{
Expand All @@ -27,7 +27,7 @@ abstract class AbstractColumn
protected $isVisible = true;

/**
* @var Template
* @var TemplateWrapper
*/
protected $template;
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Grid/Columns/DataColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function hasFilter()
public function getHeadContent()
{
$label = $this->getLabel();
if (($translator = $this->container->getTranslator()) !== null) {
if (!empty($label) && ($translator = $this->container->getTranslator()) !== null) {
$label = $translator->trans($label, [], $this->translationDomain);
}
return ucfirst($label);
Expand Down
7 changes: 4 additions & 3 deletions src/Grid/DataGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Pfilsx\DataGrid\Grid\Columns\AbstractColumn;
use Pfilsx\DataGrid\Grid\Providers\DataProviderInterface;
use Twig\Template;
use Twig\TemplateWrapper;

/**
* Class DataGrid
Expand All @@ -20,7 +21,7 @@
class DataGrid implements DataGridInterface
{
/**
* @var Template
* @var TemplateWrapper
*/
protected $template;
/**
Expand Down Expand Up @@ -141,7 +142,7 @@ public function getData()
}

/**
* @return Template
* @return TemplateWrapper
* @internal
*/
public function getTemplate()
Expand Down Expand Up @@ -193,7 +194,7 @@ public function createView(): DataGridView
*/
protected function setTemplate(string $path)
{
$this->template = $this->container->getTwig()->loadTemplate($path);
$this->template = $this->container->getTwig()->load($path);
foreach ($this->builder->getColumns() as $column) {
$column->setTemplate($this->template);
}
Expand Down
54 changes: 43 additions & 11 deletions src/Grid/DataGridFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Pfilsx\DataGrid\Config\ConfigurationContainerInterface;
use InvalidArgumentException;
use Pfilsx\DataGrid\DataGridServiceContainer;
use Pfilsx\DataGrid\Extension\DependencyInjection\DependencyInjectionExtension;
use Pfilsx\DataGrid\Grid\Providers\DataProvider;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -20,28 +21,27 @@ class DataGridFactory implements DataGridFactoryInterface
* @var Request
*/
protected $request;
/**
* @var DataGridFiltersBuilder
*/
protected $filterBuilder;
/**
* @var AbstractGridType
*/
protected $gridType;

/**
* @var ConfigurationContainerInterface
*/
protected $defaultConfiguration;
/**
* @var DependencyInjectionExtension
*/
protected $extension;
/**
* @var array
*/
protected $queryParams;
protected $types;


public function __construct(DataGridServiceContainer $container, ConfigurationContainerInterface $configs)
public function __construct(DataGridServiceContainer $container, ConfigurationContainerInterface $configs, DependencyInjectionExtension $extension)
{
$this->container = $container;
$this->request = $container->getRequest()->getCurrentRequest();
$this->defaultConfiguration = $configs;
$this->extension = $extension;
}


Expand All @@ -53,9 +53,41 @@ public function createGrid(string $gridType, $dataSource, array $params = []): D
$provider = DataProvider::create($dataSource, $this->container->getDoctrine());

/** @var AbstractGridType $type */
$gridType = new $gridType($this->container, $params);
$gridType = $this->getType($gridType);
$gridType->setParams($params);


return new DataGrid($gridType, $provider, $this->defaultConfiguration, $this->container);
}

/**
* @param string $name
* @return AbstractGridType
*/
private function getType(string $name)
{
if (!isset($this->types[$name])) {
$type = null;

if ($this->extension->hasType($name)) {
$type = $this->extension->getType($name);
}

if (!$type) {
// Support fully-qualified class names
if (!class_exists($name)) {
throw new InvalidArgumentException(sprintf('Could not load type "%s": class does not exist.', $name));
}
if (!is_subclass_of($name, AbstractGridType::class)) {
throw new InvalidArgumentException(sprintf('Could not load type "%s": class does not extend "Pfilsx\DataGrid\AbstractGridType class".', $name));
}

$type = new $name();
}

$this->types[$name] = $type;
}

return $this->types[$name];
}
}
3 changes: 2 additions & 1 deletion src/Grid/DataGridFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

use Pfilsx\DataGrid\Config\ConfigurationContainerInterface;
use Pfilsx\DataGrid\DataGridServiceContainer;
use Pfilsx\DataGrid\Extension\DependencyInjection\DependencyInjectionExtension;

interface DataGridFactoryInterface
{
public function __construct(DataGridServiceContainer $container, ConfigurationContainerInterface $configs);
public function __construct(DataGridServiceContainer $container, ConfigurationContainerInterface $configs, DependencyInjectionExtension $extension);

public function createGrid(string $gridType, $dataSource, array $params = []): DataGridInterface;
}
6 changes: 3 additions & 3 deletions src/Grid/Filters/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


use Pfilsx\DataGrid\DataGridServiceContainer;
use Twig\Template;
use Twig\TemplateWrapper;

abstract class AbstractFilter
{
Expand All @@ -14,7 +14,7 @@ abstract class AbstractFilter
*/
protected $container;
/**
* @var Template|null
* @var TemplateWrapper|null
*/
protected $template;
/**
Expand All @@ -38,7 +38,7 @@ public function getTemplate()
return $this->template;
}

public function setTemplate(?Template $template)
public function setTemplate(?TemplateWrapper $template)
{
$this->template = $template;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Grid/GridTypeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php


namespace Pfilsx\DataGrid\Grid;


interface GridTypeInterface
{
public function buildGrid(DataGridBuilderInterface $builder): void;

public function handleFilters(DataGridFiltersBuilderInterface $builder, array $filters): void;
}
Loading

0 comments on commit f561d04

Please sign in to comment.