From da399743ccf4992e47a9b1e0cadee84b663331ba Mon Sep 17 00:00:00 2001 From: KoKsPfLaNzE Date: Wed, 9 Mar 2016 22:38:43 +0100 Subject: [PATCH 1/6] update for zend-mvc 2.7 updated composer.json for min zend-mvc 2.7, also added the zend-json for data-ranges removed Zend\ServiceManager\ServiceLocatorAwareInterface parts from Datagrid-Service and ViewHelpers, for the `Column\Formatter\GenerateLink` i added a old fallback for version 1 changed some unit tests =( --- composer.json | 8 +- config/module.config.php | 76 ++++++++++++++----- .../Column/Formatter/GenerateLink.php | 54 ++++++------- src/ZfcDatagrid/Datagrid.php | 32 +++++--- .../BootstrapTable/View/Helper/TableRow.php | 30 +++++--- .../Renderer/JqGrid/View/Helper/Columns.php | 27 +++---- src/ZfcDatagrid/Service/DatagridFactory.php | 4 +- .../Column/Formatter/GenerateLinkTest.php | 23 +++++- tests/ZfcDatagridTest/DatagridTest.php | 12 --- .../View/Helper/TableRowTest.php | 1 - .../JqGrid/View/Helper/ColumnsTest.php | 35 +-------- .../Service/DatagridFactoryTest.php | 19 +++++ 12 files changed, 185 insertions(+), 136 deletions(-) diff --git a/composer.json b/composer.json index ccae979b..1a65ec78 100644 --- a/composer.json +++ b/composer.json @@ -45,8 +45,9 @@ "zendframework/zend-console": "~2.5", "zendframework/zend-db": "~2.5", "zendframework/zend-i18n": "~2.5", - "zendframework/zend-servicemanager": "~2.5", - "zendframework/zend-text": "~2.5" + "zendframework/zend-servicemanager": "~2.7", + "zendframework/zend-text": "~2.5", + "zendframework/zend-json": "~2.5" }, "suggest" : { @@ -56,7 +57,8 @@ "phpoffice/phpexcel": "~1.7", "tecnick.com/tcpdf": "~6.0", - "zendframework/zend-text": "~2.5" + "zendframework/zend-text": "~2.5", + "zendframework/zend-json": "to support date ranges" }, "autoload": { diff --git a/config/module.config.php b/config/module.config.php index 6b36adf6..10dd6def 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -1,6 +1,11 @@ [ 'settings' => [ @@ -198,38 +203,71 @@ 'service_manager' => [ - 'invokables' => [ + 'factories' => [ + Service\DatagridManager::class => Service\DatagridManagerFactory::class, + Datagrid::class => Service\DatagridFactory::class, + + 'zfcDatagrid_dbAdapter' => Service\ZendDbAdapterFactory::class, // HTML renderer - 'zfcDatagrid.renderer.bootstrapTable' => 'ZfcDatagrid\Renderer\BootstrapTable\Renderer', - 'zfcDatagrid.renderer.jqgrid' => 'ZfcDatagrid\Renderer\JqGrid\Renderer', + Renderer\BootstrapTable\Renderer::class => InvokableFactory::class, + Renderer\JqGrid\Renderer::class => InvokableFactory::class, // CLI renderer - 'zfcDatagrid.renderer.zendTable' => 'ZfcDatagrid\Renderer\ZendTable\Renderer', + Renderer\ZendTable\Renderer::class => InvokableFactory::class, // Export renderer - 'zfcDatagrid.renderer.printHtml' => 'ZfcDatagrid\Renderer\PrintHtml\Renderer', - 'zfcDatagrid.renderer.PHPExcel' => 'ZfcDatagrid\Renderer\PHPExcel\Renderer', - 'zfcDatagrid.renderer.TCPDF' => 'ZfcDatagrid\Renderer\TCPDF\Renderer', - 'zfcDatagrid.renderer.csv' => 'ZfcDatagrid\Renderer\Csv\Renderer', + Renderer\PrintHtml\Renderer::class => InvokableFactory::class, + Renderer\PHPExcel\Renderer::class => InvokableFactory::class, + Renderer\TCPDF\Renderer::class => InvokableFactory::class, + Renderer\Csv\Renderer::class => InvokableFactory::class, ], - 'factories' => [ - 'ZfcDatagridManager' => 'ZfcDatagrid\Service\DatagridManagerFactory', - 'ZfcDatagrid\Datagrid' => 'ZfcDatagrid\Service\DatagridFactory', + 'aliases' => [ + 'zfcDatagrid' => Datagrid::class, + 'ZfcDatagridManager' => Service\DatagridManager::class, - 'zfcDatagrid_dbAdapter' => 'ZfcDatagrid\Service\ZendDbAdapterFactory', - ], + // HTML renderer + 'zfcDatagrid.renderer.bootstrapTable' => Renderer\BootstrapTable\Renderer::class, + 'zfcDatagrid.renderer.jqgrid' => Renderer\JqGrid\Renderer::class, - 'aliases' => [ - 'zfcDatagrid' => 'ZfcDatagrid\Datagrid', + // CLI renderer + 'zfcDatagrid.renderer.zendTable' => Renderer\ZendTable\Renderer::class, + + // Export renderer + 'zfcDatagrid.renderer.printHtml' => Renderer\PrintHtml\Renderer::class, + 'zfcDatagrid.renderer.PHPExcel' => Renderer\PHPExcel\Renderer::class, + 'zfcDatagrid.renderer.TCPDF' => Renderer\TCPDF\Renderer::class, + 'zfcDatagrid.renderer.csv' => Renderer\Csv\Renderer::class, ], ], 'view_helpers' => [ - 'invokables' => [ - 'bootstrapTableRow' => 'ZfcDatagrid\Renderer\BootstrapTable\View\Helper\TableRow', - 'jqgridColumns' => 'ZfcDatagrid\Renderer\JqGrid\View\Helper\Columns', + 'aliases' => [ + 'bootstrapTableRow' => Renderer\BootstrapTable\View\Helper\TableRow::class, + 'jqgridColumns' => Renderer\JqGrid\View\Helper\Columns::class, + ], + 'factories' => [ + Renderer\BootstrapTable\View\Helper\TableRow::class => function($sm) { + /** @var $sm \Zend\ServiceManager\ServiceLocatorInterface */ + $tableRow = new Renderer\BootstrapTable\View\Helper\TableRow(); + if($sm->has('translator')){ + /** @noinspection PhpParamsInspection */ + $tableRow->setTranslator($sm->get('translator')); + } + + return $tableRow; + }, + Renderer\JqGrid\View\Helper\Columns::class => function($sm) { + /** @var $sm \Zend\ServiceManager\ServiceLocatorInterface */ + $tableRow = new Renderer\JqGrid\View\Helper\Columns(); + if($sm->has('translator')){ + /** @noinspection PhpParamsInspection */ + $tableRow->setTranslator($sm->get('translator')); + } + + return $tableRow; + }, ], ], diff --git a/src/ZfcDatagrid/Column/Formatter/GenerateLink.php b/src/ZfcDatagrid/Column/Formatter/GenerateLink.php index bf40ac8e..90438303 100644 --- a/src/ZfcDatagrid/Column/Formatter/GenerateLink.php +++ b/src/ZfcDatagrid/Column/Formatter/GenerateLink.php @@ -1,11 +1,11 @@ setServiceManager($sm); + /** + * old fallback that should be removed in 2.0 + * TODO remove in 2.0 + */ + if (!$viewRenderer instanceof RendererInterface) { + $viewRenderer = $viewRenderer->get('ViewRenderer'); + } + + $this->setViewRenderer($viewRenderer); $this->setRoute($route); $this->setRouteParams($params); $this->setRouteKey($key); @@ -61,32 +67,22 @@ public function getFormattedValue(AbstractColumn $column) } /** - * Set service manager - * @param ServiceManager $serviceManager - */ - public function setServiceManager(ServiceManager $serviceManager) - { - $this->serviceManager = $serviceManager; - } - - /** - * @return ServiceManager + * @return \Zend\View\Renderer\PhpRenderer */ - public function getServiceManager() + public function getViewRenderer() { - return $this->serviceManager; + return $this->viewRenderer; } /** - * @return \Zend\View\Renderer\PhpRenderer + * @param \Zend\View\Renderer\PhpRenderer $viewRenderer + * @return self */ - public function getViewRenderer() + public function setViewRenderer($viewRenderer) { - if (! $this->viewRenderer) { - $this->viewRenderer = $this->getServiceManager()->get('ViewRenderer'); - } + $this->viewRenderer = $viewRenderer; - return $this->viewRenderer; + return $this; } /** diff --git a/src/ZfcDatagrid/Datagrid.php b/src/ZfcDatagrid/Datagrid.php index ed04e022..d40d2bde 100644 --- a/src/ZfcDatagrid/Datagrid.php +++ b/src/ZfcDatagrid/Datagrid.php @@ -11,18 +11,14 @@ use Zend\I18n\Translator\Translator; use Zend\Mvc\MvcEvent; use Zend\Paginator\Paginator; -use Zend\ServiceManager\ServiceLocatorAwareInterface; -use Zend\ServiceManager\ServiceLocatorAwareTrait; use Zend\Session\Container as SessionContainer; use Zend\Stdlib\ResponseInterface; use Zend\View\Model\JsonModel; use Zend\View\Model\ViewModel; use ZfcDatagrid\Column\Style; -class Datagrid implements ServiceLocatorAwareInterface +class Datagrid { - use ServiceLocatorAwareTrait; - /** * * @var array @@ -205,6 +201,12 @@ class Datagrid implements ServiceLocatorAwareInterface */ protected $forceRenderer; + /** + * + * @var Renderer\AbstractRenderer + */ + private $rendererService; + /** * @var array */ @@ -868,11 +870,8 @@ public function getRendererName() public function getRenderer() { if (null === $this->renderer) { - $rendererName = 'zfcDatagrid.renderer.' . $this->getRendererName(); - - if ($this->getServiceLocator()->has($rendererName) === true) { - /* @var $renderer \ZfcDatagrid\Renderer\AbstractRenderer */ - $renderer = $this->getServiceLocator()->get($rendererName); + if (isset($this->rendererService)) { + $renderer = $this->rendererService; if (! $renderer instanceof Renderer\AbstractRenderer) { throw new \Exception('Renderer service must implement "ZfcDatagrid\Renderer\AbstractRenderer"'); } @@ -894,7 +893,7 @@ public function getRenderer() $this->renderer = $renderer; } else { - throw new \Exception(sprintf('Renderer service was not found, please register it: "%s"', $rendererName)); + throw new \Exception(sprintf('Renderer service was not found, please register it: "zfcDatagrid.renderer.%s"', $this->getRendererName())); } } @@ -1183,4 +1182,15 @@ public function isHtmlInitReponse() return false; } + + /** + * @param Renderer\AbstractRenderer $rendererService + * @return self + */ + public function setRendererService(Renderer\AbstractRenderer $rendererService) + { + $this->rendererService = $rendererService; + + return $this; + } } diff --git a/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRow.php b/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRow.php index 30f58415..3e96acf1 100644 --- a/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRow.php +++ b/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRow.php @@ -1,8 +1,6 @@ translator = $translator; + + return $this; + } /** * - * @param string $name + * @param string $message * @return string */ - private function translate($name) + private function translate($message) { - if ($this->getServiceLocator()->has('translator') === true) { - return $this->getServiceLocator() - ->get('translator') - ->translate($name); + if (null === $this->translator) { + return $message; } - return $name; + return $this->translator->translate($message); } /** diff --git a/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php b/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php index 1b3ecae4..8e2487f0 100644 --- a/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php +++ b/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php @@ -1,8 +1,6 @@ \' + cellvalue + \'\';'; - use ServiceLocatorAwareTrait; + /** + * @param false|null|\Zend\I18n\Translator\Translator $translator + * @return self + */ + public function setTranslator($translator) + { + $this->translator = $translator; + + return $this; + } /** * @@ -31,18 +38,8 @@ class Columns extends AbstractHelper implements ServiceLocatorAwareInterface */ private function translate($message) { - if (false === $this->translator) { - return $message; - } - if (null === $this->translator) { - if ($this->getServiceLocator()->has('translator')) { - $this->translator = $this->getServiceLocator()->get('translator'); - } else { - $this->translator = false; - - return $message; - } + return $message; } return $this->translator->translate($message); diff --git a/src/ZfcDatagrid/Service/DatagridFactory.php b/src/ZfcDatagrid/Service/DatagridFactory.php index b8747615..c2b57d76 100644 --- a/src/ZfcDatagrid/Service/DatagridFactory.php +++ b/src/ZfcDatagrid/Service/DatagridFactory.php @@ -17,7 +17,7 @@ public function createService(ServiceLocatorInterface $sm) { $config = $sm->get('config'); - if (! isset($config['ZfcDatagrid'])) { + if (!isset($config['ZfcDatagrid'])) { throw new InvalidArgumentException('Config key "ZfcDatagrid" is missing'); } @@ -30,6 +30,8 @@ public function createService(ServiceLocatorInterface $sm) if ($sm->has('translator') === true) { $grid->setTranslator($sm->get('translator')); } + /** @noinspection PhpParamsInspection */ + $grid->setRendererService($sm->get('zfcDatagrid.renderer.' . $grid->getRendererName())); $grid->init(); return $grid; diff --git a/tests/ZfcDatagridTest/Column/Formatter/GenerateLinkTest.php b/tests/ZfcDatagridTest/Column/Formatter/GenerateLinkTest.php index cd87a21b..0595bcac 100644 --- a/tests/ZfcDatagridTest/Column/Formatter/GenerateLinkTest.php +++ b/tests/ZfcDatagridTest/Column/Formatter/GenerateLinkTest.php @@ -12,9 +12,28 @@ class GenerateLinkTest extends \PHPUnit_Framework_TestCase { public function testConstructor() { - $generateLink = new GenerateLink(new ServiceManager(), 'route'); + /** @var \Zend\View\Renderer\PhpRenderer $phpRenderer */ + $phpRenderer = $this->getMockBuilder('Zend\View\Renderer\PhpRenderer') + ->disableOriginalConstructor() + ->getMock(); + + $generateLink = new GenerateLink($phpRenderer, 'route'); + + $this->assertEquals('route', $generateLink->getRoute()); + $this->assertEmpty($generateLink->getRouteKey()); + $this->assertEmpty($generateLink->getRouteParams()); + } + public function testConstructorFallBackVersion() + { + $phpRenderer = $this->getMockBuilder('Zend\View\Renderer\PhpRenderer') + ->disableOriginalConstructor() + ->getMock(); + + $sm = new ServiceManager(); + $sm->setService('ViewRenderer', $phpRenderer); + + $generateLink = new GenerateLink($sm, 'route'); - $this->assertInstanceOf('Zend\ServiceManager\ServiceManager', $generateLink->getServiceManager()); $this->assertEquals('route', $generateLink->getRoute()); $this->assertEmpty($generateLink->getRouteKey()); $this->assertEmpty($generateLink->getRouteParams()); diff --git a/tests/ZfcDatagridTest/DatagridTest.php b/tests/ZfcDatagridTest/DatagridTest.php index 00a9d0b0..f1fe29ed 100644 --- a/tests/ZfcDatagridTest/DatagridTest.php +++ b/tests/ZfcDatagridTest/DatagridTest.php @@ -39,12 +39,10 @@ public function setUp() $mvcEvent->expects($this->any()) ->method('getRequest') ->will($this->returnValue($this->getMock('Zend\Http\PhpEnvironment\Request'))); - $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceManager'); $this->grid = new Datagrid(); $this->grid->setOptions($this->config); $this->grid->setMvcEvent($mvcEvent); - $this->grid->setServiceLocator($serviceLocator); } public function testInit() @@ -94,16 +92,6 @@ public function testCacheId() $this->assertEquals('myCacheId', $this->grid->getCacheId()); } - public function testServiceLocator() - { - $this->assertInstanceOf('Zend\ServiceManager\ServiceManager', $this->grid->getServiceLocator()); - - $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceManager'); - $this->grid->setServiceLocator($serviceLocator); - $this->assertInstanceOf('Zend\ServiceManager\ServiceManager', $this->grid->getServiceLocator()); - $this->assertEquals($serviceLocator, $this->grid->getServiceLocator()); - } - public function testMvcEvent() { $this->assertInstanceOf('Zend\Mvc\MvcEvent', $this->grid->getMvcEvent()); diff --git a/tests/ZfcDatagridTest/Renderer/BootstrapTable/View/Helper/TableRowTest.php b/tests/ZfcDatagridTest/Renderer/BootstrapTable/View/Helper/TableRowTest.php index 00030cb5..b173c887 100644 --- a/tests/ZfcDatagridTest/Renderer/BootstrapTable/View/Helper/TableRowTest.php +++ b/tests/ZfcDatagridTest/Renderer/BootstrapTable/View/Helper/TableRowTest.php @@ -176,7 +176,6 @@ public function testAction() $rowData['action'] = ''; $helper = new TableRow(); - $helper->setServiceLocator($this->serviceLocator); // must be instanceof Column\Select... $myCol = new Column\Select('myCol'); diff --git a/tests/ZfcDatagridTest/Renderer/JqGrid/View/Helper/ColumnsTest.php b/tests/ZfcDatagridTest/Renderer/JqGrid/View/Helper/ColumnsTest.php index 294043b7..6ede97f6 100644 --- a/tests/ZfcDatagridTest/Renderer/JqGrid/View/Helper/ColumnsTest.php +++ b/tests/ZfcDatagridTest/Renderer/JqGrid/View/Helper/ColumnsTest.php @@ -23,13 +23,7 @@ class ColumnsTest extends PHPUnit_Framework_TestCase public function setUp() { - $sm2 = $this->getMock('Zend\ServiceManager\ServiceManager'); - - $sm = $this->getMock('Zend\View\HelperPluginManager', [], [], '', false); - $sm->expects($this->any()) - ->method('getServiceLocator') - ->will($this->returnValue($sm2)); - $this->sm = $sm; + $this->sm = $this->getMock('Zend\View\HelperPluginManager', [], [], '', false); $myCol = $this->getMockForAbstractClass('ZfcDatagrid\Column\AbstractColumn'); $myCol->setUniqueId('myCol'); @@ -37,19 +31,9 @@ public function setUp() $this->myCol = $myCol; } - public function testServiceLocator() - { - $helper = new Helper\Columns(); - - $helper->setServiceLocator($this->sm); - $this->assertInstanceOf('Zend\ServiceManager\ServiceLocatorInterface', $helper->getServiceLocator()); - $this->assertSame($this->sm, $helper->getServiceLocator()); - } - public function testSimple() { $helper = new Helper\Columns(); - $helper->setServiceLocator($this->sm); $cols = [ clone $this->myCol, @@ -64,7 +48,6 @@ public function testSimple() public function testStyleBold() { $helper = new Helper\Columns(); - $helper->setServiceLocator($this->sm); $col1 = clone $this->myCol; $col1->addStyle(new Style\Bold()); @@ -81,7 +64,6 @@ public function testStyleBold() public function testStyleItalic() { $helper = new Helper\Columns(); - $helper->setServiceLocator($this->sm); $col1 = clone $this->myCol; $col1->addStyle(new Style\Italic()); @@ -98,7 +80,6 @@ public function testStyleItalic() public function testStyleColor() { $helper = new Helper\Columns(); - $helper->setServiceLocator($this->sm); $col1 = clone $this->myCol; $col1->addStyle(new Style\Color(Style\Color::$RED)); @@ -115,7 +96,6 @@ public function testStyleColor() public function testStyleBackgroundColor() { $helper = new Helper\Columns(); - $helper->setServiceLocator($this->sm); $col1 = clone $this->myCol; $col1->addStyle(new Style\BackgroundColor(Style\BackgroundColor::$RED)); @@ -133,7 +113,6 @@ public function testStyleException() { $styleMock = $this->getMockForAbstractClass('ZfcDatagrid\Column\Style\AbstractStyle'); $helper = new Helper\Columns(); - $helper->setServiceLocator($this->sm); $col1 = clone $this->myCol; $col1->addStyle($styleMock); @@ -151,7 +130,6 @@ public function testStyleException() public function testStyleByValueEqual() { $helper = new Helper\Columns(); - $helper->setServiceLocator($this->sm); $col1 = clone $this->myCol; @@ -172,7 +150,6 @@ public function testStyleByValueEqual() public function testCSSClassCell() { $helper = new Helper\Columns(); - $helper->setServiceLocator($this->sm); $col1 = clone $this->myCol; $col1->addStyle(new Style\CSSClass('test-class')); @@ -189,7 +166,6 @@ public function testCSSClassCell() public function testStyleByValueNotEqual() { $helper = new Helper\Columns(); - $helper->setServiceLocator($this->sm); $col1 = clone $this->myCol; @@ -210,7 +186,6 @@ public function testStyleByValueNotEqual() public function testStyleByValueNotSupported() { $helper = new Helper\Columns(); - $helper->setServiceLocator($this->sm); $col1 = clone $this->myCol; @@ -232,7 +207,6 @@ public function testTranslate() $sm = $this->getMock('Zend\ServiceManager\ServiceManager', null); $helper = new Helper\Columns(); - $helper->setServiceLocator($sm); $reflection = new \ReflectionClass($helper); $method = $reflection->getMethod('translate'); @@ -245,9 +219,7 @@ public function testTranslate() public function testTranslateWithMockedTranslator() { - /** @var \PHPUnit_Framework_MockObject_MockObject|\Zend\ServiceManager\ServiceManager $sm */ - $sm = $this->getMock('Zend\ServiceManager\ServiceManager', null); - + /** @var \PHPUnit_Framework_MockObject_MockObject|\Zend\I18n\Translator\Translator $translator */ $translator = $this->getMockBuilder('Zend\I18n\Translator\Translator') ->disableOriginalConstructor() ->setMethods(['translate']) @@ -259,9 +231,8 @@ public function testTranslateWithMockedTranslator() ['test', 'default', null, 'translate'], ])); - $sm->setService('translator', $translator); $helper = new Helper\Columns(); - $helper->setServiceLocator($sm); + $helper->setTranslator($translator); $reflection = new \ReflectionClass($helper); $method = $reflection->getMethod('translate'); diff --git a/tests/ZfcDatagridTest/Service/DatagridFactoryTest.php b/tests/ZfcDatagridTest/Service/DatagridFactoryTest.php index d092025a..7654733b 100644 --- a/tests/ZfcDatagridTest/Service/DatagridFactoryTest.php +++ b/tests/ZfcDatagridTest/Service/DatagridFactoryTest.php @@ -10,6 +10,7 @@ */ class DatagridFactoryTest extends PHPUnit_Framework_TestCase { + /** @var array */ private $config = [ 'ZfcDatagrid' => [ 'cache' => [ @@ -17,11 +18,23 @@ class DatagridFactoryTest extends PHPUnit_Framework_TestCase 'name' => 'Filesystem', ], ], + 'generalParameterNames' => [ + 'rendererType' => null, + ], + 'settings' => [ + 'default' => [ + 'renderer' => [ + 'http' => 'bootstrapTable', + ], + ], + ], ], ]; private $applicationMock; + private $rendererServiceMock; + public function setUp() { $mvcEventMock = $this->getMock('Zend\Mvc\MvcEvent'); @@ -30,6 +43,9 @@ public function setUp() $this->applicationMock->expects($this->any()) ->method('getMvcEvent') ->will($this->returnValue($mvcEventMock)); + + $this->rendererServiceMock = $this->getMockBuilder('ZfcDatagrid\Renderer\BootstrapTable\Renderer') + ->getMock(); } public function testCreateServiceException() @@ -48,6 +64,7 @@ public function testCanCreateService() $sm = new ServiceManager(); $sm->setService('config', $this->config); $sm->setService('application', $this->applicationMock); + $sm->setService('zfcDatagrid.renderer.bootstrapTable', $this->rendererServiceMock); $factory = new DatagridFactory(); $grid = $factory->createService($sm); @@ -62,6 +79,7 @@ public function testCanCreateServiceWithTranslator() $sm = new ServiceManager(); $sm->setService('config', $this->config); $sm->setService('application', $this->applicationMock); + $sm->setService('zfcDatagrid.renderer.bootstrapTable', $this->rendererServiceMock); $sm->setService('translator', $translatorMock); $factory = new DatagridFactory(); @@ -78,6 +96,7 @@ public function testCanCreateServiceWithMvcTranslator() $sm = new ServiceManager(); $sm->setService('config', $this->config); $sm->setService('application', $this->applicationMock); + $sm->setService('zfcDatagrid.renderer.bootstrapTable', $this->rendererServiceMock); $sm->setService('translator', $mvcTranslatorMock); $factory = new DatagridFactory(); From 6e3f7d18c90255bb4252ebd63b37f6443d516ccf Mon Sep 17 00:00:00 2001 From: KoKsPfLaNzE Date: Wed, 9 Mar 2016 23:12:04 +0100 Subject: [PATCH 2/6] updated `AbstractDatagrid` --- src/ZfcDatagrid/Service/AbstractDatagrid.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/ZfcDatagrid/Service/AbstractDatagrid.php b/src/ZfcDatagrid/Service/AbstractDatagrid.php index b08b3bdc..133a2b71 100644 --- a/src/ZfcDatagrid/Service/AbstractDatagrid.php +++ b/src/ZfcDatagrid/Service/AbstractDatagrid.php @@ -8,16 +8,12 @@ abstract class AbstractDatagrid extends Datagrid implements FactoryInterface { - private $isResponse = false; - /** * * @return Datagrid */ public function createService(ServiceLocatorInterface $sm) { - $this->setServiceLocator($sm); - $config = $sm->get('config'); if (! isset($config['ZfcDatagrid'])) { @@ -32,6 +28,8 @@ public function createService(ServiceLocatorInterface $sm) if ($sm->has('translator') === true) { parent::setTranslator($sm->get('translator')); } + /** @noinspection PhpParamsInspection */ + parent::setRendererService($sm->get('zfcDatagrid.renderer.' . parent::getRendererName())); parent::init(); return $this; @@ -44,7 +42,7 @@ public function render() { $this->initGrid(); - return parent::render(); + parent::render(); } /** From af9b6a281313612ccf5ac476351467aa83094b65 Mon Sep 17 00:00:00 2001 From: KoKsPfLaNzE Date: Thu, 10 Mar 2016 19:52:28 +0100 Subject: [PATCH 3/6] composer update mvc --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 1a65ec78..eeec30b1 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "require": { "php": "~5.5|~7.0", - "zendframework/zend-mvc": "~2.5", + "zendframework/zend-mvc": "~2.7", "zendframework/zend-modulemanager": "~2.5", "zendframework/zend-session": "~2.5", "zendframework/zend-view": "~2.5", @@ -45,7 +45,7 @@ "zendframework/zend-console": "~2.5", "zendframework/zend-db": "~2.5", "zendframework/zend-i18n": "~2.5", - "zendframework/zend-servicemanager": "~2.7", + "zendframework/zend-servicemanager": "~2.5", "zendframework/zend-text": "~2.5", "zendframework/zend-json": "~2.5" }, From 49f23d3a9eed6ba0007b88d484f40bba62090249 Mon Sep 17 00:00:00 2001 From: KoKsPfLaNzE Date: Mon, 14 Mar 2016 22:17:49 +0100 Subject: [PATCH 4/6] removed `Zend\ServiceManager\Factory\InvokableFactory` so ZfcDatagrid works with zend-mvc and zend-servicemanager 2.5 --- composer.json | 2 +- config/module.config.php | 29 +++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index eeec30b1..f4301f1b 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "require": { "php": "~5.5|~7.0", - "zendframework/zend-mvc": "~2.7", + "zendframework/zend-mvc": "~2.5", "zendframework/zend-modulemanager": "~2.5", "zendframework/zend-session": "~2.5", "zendframework/zend-view": "~2.5", diff --git a/config/module.config.php b/config/module.config.php index 10dd6def..28fe78e6 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -3,7 +3,6 @@ use ZfcDatagrid\Datagrid; use ZfcDatagrid\Renderer; use ZfcDatagrid\Service; -use Zend\ServiceManager\Factory\InvokableFactory; return [ 'ZfcDatagrid' => [ @@ -210,17 +209,31 @@ 'zfcDatagrid_dbAdapter' => Service\ZendDbAdapterFactory::class, // HTML renderer - Renderer\BootstrapTable\Renderer::class => InvokableFactory::class, - Renderer\JqGrid\Renderer::class => InvokableFactory::class, + Renderer\BootstrapTable\Renderer::class => function() { + return new Renderer\BootstrapTable\Renderer(); + }, + Renderer\JqGrid\Renderer::class => function() { + return new Renderer\JqGrid\Renderer(); + }, // CLI renderer - Renderer\ZendTable\Renderer::class => InvokableFactory::class, + Renderer\ZendTable\Renderer::class => function() { + return new Renderer\ZendTable\Renderer(); + }, // Export renderer - Renderer\PrintHtml\Renderer::class => InvokableFactory::class, - Renderer\PHPExcel\Renderer::class => InvokableFactory::class, - Renderer\TCPDF\Renderer::class => InvokableFactory::class, - Renderer\Csv\Renderer::class => InvokableFactory::class, + Renderer\PrintHtml\Renderer::class => function() { + return new Renderer\PrintHtml\Renderer(); + }, + Renderer\PHPExcel\Renderer::class => function() { + return new Renderer\PHPExcel\Renderer(); + }, + Renderer\TCPDF\Renderer::class => function() { + return new Renderer\TCPDF\Renderer(); + }, + Renderer\Csv\Renderer::class => function() { + return new Renderer\Csv\Renderer(); + }, ], 'aliases' => [ From 5f8451da0b745e62a50810fd7787affc2cef71d8 Mon Sep 17 00:00:00 2001 From: KoKsPfLaNzE Date: Tue, 29 Mar 2016 20:15:39 +0200 Subject: [PATCH 5/6] readded ServiceLocatorAwareTrait in ZfcDataGrid\Datagrid [remove parts of the BC] added TableRowFactory + ColumnsFactory for the ViewHelpers [so config cache should now work] also removed the other closure and added InvokableFactory in config update composer.json min zend-mvc 2.7 + zend-servicemanger 2.7 [moved to require] updated unittests --- composer.json | 6 +-- config/module.config.php | 51 ++++--------------- src/ZfcDatagrid/Datagrid.php | 3 ++ .../View/Helper/TableRowFactory.php | 27 ++++++++++ .../JqGrid/View/Helper/ColumnsFactory.php | 27 ++++++++++ src/ZfcDatagrid/Service/AbstractDatagrid.php | 1 + src/ZfcDatagrid/Service/DatagridFactory.php | 1 + .../Column/Formatter/GenerateLinkTest.php | 2 + tests/ZfcDatagridTest/DatagridTest.php | 5 +- 9 files changed, 78 insertions(+), 45 deletions(-) create mode 100644 src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRowFactory.php create mode 100644 src/ZfcDatagrid/Renderer/JqGrid/View/Helper/ColumnsFactory.php diff --git a/composer.json b/composer.json index f4301f1b..74afd014 100644 --- a/composer.json +++ b/composer.json @@ -26,13 +26,14 @@ "require": { "php": "~5.5|~7.0", - "zendframework/zend-mvc": "~2.5", + "zendframework/zend-mvc": "~2.7", "zendframework/zend-modulemanager": "~2.5", "zendframework/zend-session": "~2.5", "zendframework/zend-view": "~2.5", "zendframework/zend-http": "~2.5", "zendframework/zend-paginator": "~2.5", - "zendframework/zend-cache": "~2.5" + "zendframework/zend-cache": "~2.5", + "zendframework/zend-servicemanager": "~2.7" }, "require-dev": { @@ -45,7 +46,6 @@ "zendframework/zend-console": "~2.5", "zendframework/zend-db": "~2.5", "zendframework/zend-i18n": "~2.5", - "zendframework/zend-servicemanager": "~2.5", "zendframework/zend-text": "~2.5", "zendframework/zend-json": "~2.5" }, diff --git a/config/module.config.php b/config/module.config.php index 28fe78e6..acfa9743 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -3,6 +3,7 @@ use ZfcDatagrid\Datagrid; use ZfcDatagrid\Renderer; use ZfcDatagrid\Service; +use Zend\ServiceManager\Factory\InvokableFactory; return [ 'ZfcDatagrid' => [ @@ -209,31 +210,17 @@ 'zfcDatagrid_dbAdapter' => Service\ZendDbAdapterFactory::class, // HTML renderer - Renderer\BootstrapTable\Renderer::class => function() { - return new Renderer\BootstrapTable\Renderer(); - }, - Renderer\JqGrid\Renderer::class => function() { - return new Renderer\JqGrid\Renderer(); - }, + Renderer\BootstrapTable\Renderer::class => InvokableFactory::class, + Renderer\JqGrid\Renderer::class => InvokableFactory::class, // CLI renderer - Renderer\ZendTable\Renderer::class => function() { - return new Renderer\ZendTable\Renderer(); - }, + Renderer\ZendTable\Renderer::class => InvokableFactory::class, // Export renderer - Renderer\PrintHtml\Renderer::class => function() { - return new Renderer\PrintHtml\Renderer(); - }, - Renderer\PHPExcel\Renderer::class => function() { - return new Renderer\PHPExcel\Renderer(); - }, - Renderer\TCPDF\Renderer::class => function() { - return new Renderer\TCPDF\Renderer(); - }, - Renderer\Csv\Renderer::class => function() { - return new Renderer\Csv\Renderer(); - }, + Renderer\PrintHtml\Renderer::class => InvokableFactory::class, + Renderer\PHPExcel\Renderer::class => InvokableFactory::class, + Renderer\TCPDF\Renderer::class => InvokableFactory::class, + Renderer\Csv\Renderer::class => InvokableFactory::class, ], 'aliases' => [ @@ -261,26 +248,8 @@ 'jqgridColumns' => Renderer\JqGrid\View\Helper\Columns::class, ], 'factories' => [ - Renderer\BootstrapTable\View\Helper\TableRow::class => function($sm) { - /** @var $sm \Zend\ServiceManager\ServiceLocatorInterface */ - $tableRow = new Renderer\BootstrapTable\View\Helper\TableRow(); - if($sm->has('translator')){ - /** @noinspection PhpParamsInspection */ - $tableRow->setTranslator($sm->get('translator')); - } - - return $tableRow; - }, - Renderer\JqGrid\View\Helper\Columns::class => function($sm) { - /** @var $sm \Zend\ServiceManager\ServiceLocatorInterface */ - $tableRow = new Renderer\JqGrid\View\Helper\Columns(); - if($sm->has('translator')){ - /** @noinspection PhpParamsInspection */ - $tableRow->setTranslator($sm->get('translator')); - } - - return $tableRow; - }, + Renderer\BootstrapTable\View\Helper\TableRow::class => Renderer\BootstrapTable\View\Helper\TableRowFactory::class, + Renderer\JqGrid\View\Helper\Columns::class => Renderer\JqGrid\View\Helper\ColumnsFactory::class, ], ], diff --git a/src/ZfcDatagrid/Datagrid.php b/src/ZfcDatagrid/Datagrid.php index d40d2bde..6976bef6 100644 --- a/src/ZfcDatagrid/Datagrid.php +++ b/src/ZfcDatagrid/Datagrid.php @@ -11,6 +11,7 @@ use Zend\I18n\Translator\Translator; use Zend\Mvc\MvcEvent; use Zend\Paginator\Paginator; +use Zend\ServiceManager\ServiceLocatorAwareTrait; use Zend\Session\Container as SessionContainer; use Zend\Stdlib\ResponseInterface; use Zend\View\Model\JsonModel; @@ -19,6 +20,8 @@ class Datagrid { + use ServiceLocatorAwareTrait; + /** * * @var array diff --git a/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRowFactory.php b/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRowFactory.php new file mode 100644 index 00000000..00ea9c95 --- /dev/null +++ b/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRowFactory.php @@ -0,0 +1,27 @@ +has('translator')){ + /** @noinspection PhpParamsInspection */ + $tableRow->setTranslator($serviceLocator->get('translator')); + } + + return $tableRow; + } + +} \ No newline at end of file diff --git a/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/ColumnsFactory.php b/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/ColumnsFactory.php new file mode 100644 index 00000000..b6e16dcd --- /dev/null +++ b/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/ColumnsFactory.php @@ -0,0 +1,27 @@ +has('translator')){ + /** @noinspection PhpParamsInspection */ + $tableRow->setTranslator($serviceLocator->get('translator')); + } + + return $tableRow; + } + +} \ No newline at end of file diff --git a/src/ZfcDatagrid/Service/AbstractDatagrid.php b/src/ZfcDatagrid/Service/AbstractDatagrid.php index 133a2b71..c807d12a 100644 --- a/src/ZfcDatagrid/Service/AbstractDatagrid.php +++ b/src/ZfcDatagrid/Service/AbstractDatagrid.php @@ -14,6 +14,7 @@ abstract class AbstractDatagrid extends Datagrid implements FactoryInterface */ public function createService(ServiceLocatorInterface $sm) { + $this->setServiceLocator($sm); $config = $sm->get('config'); if (! isset($config['ZfcDatagrid'])) { diff --git a/src/ZfcDatagrid/Service/DatagridFactory.php b/src/ZfcDatagrid/Service/DatagridFactory.php index c2b57d76..56fa6f03 100644 --- a/src/ZfcDatagrid/Service/DatagridFactory.php +++ b/src/ZfcDatagrid/Service/DatagridFactory.php @@ -25,6 +25,7 @@ public function createService(ServiceLocatorInterface $sm) $application = $sm->get('application'); $grid = new Datagrid(); + $grid->setServiceLocator($sm); $grid->setOptions($config['ZfcDatagrid']); $grid->setMvcEvent($application->getMvcEvent()); if ($sm->has('translator') === true) { diff --git a/tests/ZfcDatagridTest/Column/Formatter/GenerateLinkTest.php b/tests/ZfcDatagridTest/Column/Formatter/GenerateLinkTest.php index 0595bcac..183ae4f7 100644 --- a/tests/ZfcDatagridTest/Column/Formatter/GenerateLinkTest.php +++ b/tests/ZfcDatagridTest/Column/Formatter/GenerateLinkTest.php @@ -41,11 +41,13 @@ public function testConstructorFallBackVersion() public function testGetFormattedValue() { + /** @var \ZfcDatagrid\Column\AbstractColumn $col */ $col = $this->getMockForAbstractClass('ZfcDatagrid\Column\AbstractColumn'); $col->setUniqueId('foo'); $phpRenderer = $this->getMockBuilder('Zend\View\Renderer\PhpRenderer') ->disableOriginalConstructor() + ->setMethods(['url']) ->getMock(); $phpRenderer->expects($this->any()) diff --git a/tests/ZfcDatagridTest/DatagridTest.php b/tests/ZfcDatagridTest/DatagridTest.php index f1fe29ed..6f2993a0 100644 --- a/tests/ZfcDatagridTest/DatagridTest.php +++ b/tests/ZfcDatagridTest/DatagridTest.php @@ -273,7 +273,10 @@ public function testAddColumn() { $this->assertEquals([], $this->grid->getColumns()); - $col = $this->getMockForAbstractClass('ZfcDatagrid\Column\AbstractColumn'); + $col = $this->getMockBuilder('ZfcDatagrid\Column\AbstractColumn') + ->setMethods(['getUniqueId']) + ->getMock(); + $col->expects($this->any()) ->method('getUniqueId') ->will($this->returnValue('myUniqueId')); From cec8bcbfdd9b7a2dd32474eca9a99a095561d4a5 Mon Sep 17 00:00:00 2001 From: KoKsPfLaNzE Date: Tue, 29 Mar 2016 20:25:27 +0200 Subject: [PATCH 6/6] run cs update..... --- .../BootstrapTable/View/Helper/TableRowFactory.php | 10 +++------- .../Renderer/JqGrid/View/Helper/ColumnsFactory.php | 10 +++------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRowFactory.php b/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRowFactory.php index 00ea9c95..07b42255 100644 --- a/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRowFactory.php +++ b/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRowFactory.php @@ -1,27 +1,23 @@ has('translator')){ + if ($serviceLocator->has('translator')) { /** @noinspection PhpParamsInspection */ $tableRow->setTranslator($serviceLocator->get('translator')); } return $tableRow; } - -} \ No newline at end of file +} diff --git a/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/ColumnsFactory.php b/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/ColumnsFactory.php index b6e16dcd..7e728f54 100644 --- a/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/ColumnsFactory.php +++ b/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/ColumnsFactory.php @@ -1,27 +1,23 @@ has('translator')){ + if ($serviceLocator->has('translator')) { /** @noinspection PhpParamsInspection */ $tableRow->setTranslator($serviceLocator->get('translator')); } return $tableRow; } - -} \ No newline at end of file +}