Skip to content
This repository has been archived by the owner on Jan 16, 2018. It is now read-only.

Commit

Permalink
refactor #45 Make Datagrid configuration immutable (sstok)
Browse files Browse the repository at this point in the history
This PR was merged into the master branch.

Discussion
----------

|Q            |A  |
|---          |---|
|Bug Fix?     |no |
|New Feature? |no |
|BC Breaks?   |ye |
|Deprecations?|no |
|Fixed Tickets|   |

This is the last refactoring pull-request (for now), the datagrid system is
trimmed down immensely to make integration easier, execution faster.
And code complexity lower.

In the end the datagrid has a clear focus on what it does, and can be used for.
Input data processing is no longer a part of the Datagrid, but should be done before
the Datagrid is populated with data (which will be possible with integration adapters).

With this pr the following changed:

 * The (default) Datagrid no longer allows to change the columns (you can only set them in the constructor),
   adding/removing or replacing columns on a datagrid is no longer possible.

 * Setting data is only possible once now, calling `setData` twice will result in
   an exception.

 * The event-dispatcher for updating the DatagridView is replaced with a
   single view-builder callable (use decoration to chain them).

~~*UPGRADE instructions needs to be updated before this pr can be merged 😇*~~

Commits
-------

c051ca5 Make Datagrid configuration immutable
  • Loading branch information
sstok committed Feb 6, 2016
2 parents fc10ea2 + c051ca5 commit 2011802
Show file tree
Hide file tree
Showing 19 changed files with 212 additions and 409 deletions.
19 changes: 12 additions & 7 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ and clean-ups.
* Data passed to `Datagrid::setData()` must be an `array` or `Traversable` object,
pre/post data filtering is no longer supported.

As a result of this the `PRE_SET_DATA` and `POST_SET_DATA` constants on `DatagridEvents`
are removed.
* The `DatagridEvents` class is removed as no events are dispatched anymore.

* Data can only be set once on a datagrid, calling `Datagrid::setData()` will throw
an exception.

* The `Datagrid` class no longer allows to change the registered columns
(all must be provided in the constructor).

It's advised to use `DatagridBuilder` if you need to allow changing the columns.

### Columns

Expand Down Expand Up @@ -250,7 +257,7 @@ and clean-ups.
All view classes have public properties and can be extended when needed.

* Rows are now initialized when the DatagridView is created, and not when the row
iterated.
gets iterated.

* Views are no longer aware of the object that created them, meaning it's no longer possible
to directly get the Datagrid or Column object from a view.
Expand All @@ -260,10 +267,8 @@ and clean-ups.
* When a DatagridView is created it's no longer possible to change the column order
or it's cells. Existing rows can be removed but not added or replaced.

* The `DatagridEvents::POST_BUILD_VIEW` constant is renamed to `DatagridEvents::BUILD_VIEW`,
and a `DatagridViewEvent` will be dispatched instead of `DatagridEvent`.

* The `DatagridEvents::PRE_BUILD_VIEW` constant is removed.
* Updating the `DatagridView` class is changed to use a callable instead of looping
through event listeners.

## Upgrade FROM 0.5 to 0.6

Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
],
"require": {
"php": "~5.5|~7.0",
"symfony/event-dispatcher": "~2.3|~3.0",
"symfony/property-access": "~2.8|~3.0",
"symfony/options-resolver": "~2.7.7|~3.0",
"symfony/intl": "~2.3|~3.0"
Expand Down
20 changes: 0 additions & 20 deletions src/AbstractDatagridExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@ public function getTypeExtensions($type)
return isset($this->typesExtensions[$type]) ? $this->typesExtensions[$type] : [];
}

/**
* {@inheritdoc}
*/
public function registerListeners(DatagridInterface $datagrid)
{
}

/**
* If extension needs to provide new column types this function
* should be overloaded in child class and return an array of ColumnTypeInterface
Expand All @@ -108,19 +101,6 @@ protected function loadTypes()
return [];
}

/**
* If extension needs to load event subscribers this method should be overloaded in
* child class and return array event subscribers.
*
* @return array
*
* @codeCoverageIgnore
*/
protected function loadSubscribers()
{
return [];
}

/**
* If extension needs to provide new column types this function
* should be overloaded in child class and return array of ColumnTypeInterface
Expand Down
103 changes: 29 additions & 74 deletions src/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@

use Rollerworks\Component\Datagrid\Column\ColumnInterface;
use Rollerworks\Component\Datagrid\Exception\BadMethodCallException;
use Rollerworks\Component\Datagrid\Exception\DatagridException;
use Rollerworks\Component\Datagrid\Exception\InvalidArgumentException;
use Rollerworks\Component\Datagrid\Exception\UnexpectedTypeException;
use Rollerworks\Component\Datagrid\Exception\UnknownColumnException;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Default Datagrid implementation.
Expand All @@ -27,7 +25,6 @@
* to create a new Datagrid.
*
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
* @author FSi sp. z o.o. <info@fsi.pl>
*/
class Datagrid implements DatagridInterface
{
Expand All @@ -51,21 +48,35 @@ class Datagrid implements DatagridInterface
private $columns = [];

/**
* EventDispatcher for data and view creation.
*
* @var EventDispatcher
* @var callable
*/
private $dispatcher;
private $viewBuilder;

/**
* Constructor.
*
* @param string $name
* @param string $name Name of the datagrid.
* @param ColumnInterface[] $columns Columns of the datagrid
* @param callable|null $viewBuilder A callable view builder.
* Use the decorator pattern to chain multiple.
*/
public function __construct($name)
public function __construct($name, array $columns, callable $viewBuilder = null)
{
$this->name = $name;
$this->dispatcher = new EventDispatcher();
$this->viewBuilder = $viewBuilder;

foreach ($columns as $column) {
if (!$column instanceof ColumnInterface) {
throw new InvalidArgumentException(
sprintf(
'All columns passed to Datagrid::__construct() must of instances of %s.',
ColumnInterface::class
)
);
}

$this->columns[$column->getName()] = $column;
}
}

/**
Expand All @@ -76,44 +87,6 @@ public function getName()
return $this->name;
}

/**
* {@inheritdoc}
*/
public function addEventListener($eventName, callable $listener, $priority = 0)
{
$this->dispatcher->addListener($eventName, $listener, $priority);

return $this;
}

/**
* {@inheritdoc}
*/
public function addEventSubscriber(EventSubscriberInterface $subscriber)
{
$this->dispatcher->addSubscriber($subscriber);

return $this;
}

/**
* {@inheritdoc}
*/
public function addColumn(ColumnInterface $column)
{
$name = $column->getName();

if (isset($this->columns[$name])) {
throw new DatagridException(
sprintf('A column with name "%s" is already registered on the datagrid', $name)
);
}

$this->columns[$name] = $column;

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -159,32 +132,12 @@ public function hasColumnType($type)
/**
* {@inheritdoc}
*/
public function removeColumn($name)
public function setData($data)
{
if (!isset($this->columns[$name])) {
throw new UnknownColumnException($name, $this);
if (null !== $this->data) {
throw new BadMethodCallException('Datagrid::setData() can only be called once.');
}

unset($this->columns[$name]);

return $this;
}

/**
* {@inheritdoc}
*/
public function clearColumns()
{
$this->columns = [];

return $this;
}

/**
* {@inheritdoc}
*/
public function setData($data)
{
if (!is_array($data) && !$data instanceof \Traversable) {
throw new UnexpectedTypeException($data, ['array', 'Traversable']);
}
Expand Down Expand Up @@ -215,8 +168,10 @@ public function createView()

$view = new DatagridView($this);

$event = new DatagridViewEvent($this, $view);
$this->dispatcher->dispatch(DatagridEvents::BUILD_VIEW, $event);
if (null !== $this->viewBuilder) {
$builder = $this->viewBuilder;
$builder($view);
}

return $view;
}
Expand Down
6 changes: 1 addition & 5 deletions src/DatagridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ public function getDatagrid()
);
}

$datagrid = new Datagrid($this->name);

foreach ($this->unresolvedColumns as $name => $column) {
$this->columns[$name] = $this->factory->createColumn(
$name,
Expand All @@ -159,9 +157,7 @@ public function getDatagrid()
unset($this->unresolvedColumns[$name]);
}

foreach ($this->columns as $column) {
$datagrid->addColumn($column);
}
$datagrid = new Datagrid($this->name, $this->columns);

$this->locked = true;

Expand Down
26 changes: 0 additions & 26 deletions src/DatagridEvents.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/DatagridExtensionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
*/
interface DatagridExtensionInterface
{
/**
* Register event listeners.
*
* @param DatagridInterface $datagrid
*/
public function registerListeners(DatagridInterface $datagrid);

/**
* Check if extension has column type of $type.
*
Expand Down
4 changes: 2 additions & 2 deletions src/DatagridFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function __construct(ColumnTypeRegistryInterface $registry)
/**
* {@inheritdoc}
*/
public function createDatagrid($name)
public function createDatagrid($name, array $columns)
{
return new Datagrid($name);
return new Datagrid($name, $columns);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/DatagridFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ public function createColumn($name, $type, array $options = []);
/**
* Create a new DatagridInterface instance with a unique name.
*
* @param string $name
* @param string $name Name of the datagrid.
* @param ColumnInterface[] $columns Columns of the datagrid
*
* @return DatagridInterface
*/
public function createDatagrid($name);
public function createDatagrid($name, array $columns);

/**
* Create a new DatagridBuilderInterface instance.
Expand Down
Loading

0 comments on commit 2011802

Please sign in to comment.