Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 8 additions & 1 deletion Resources/config/pagerfanta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,17 @@
<argument type="service" id="translator" />
</service>

<service id="white_october_pagerfanta.route_generator_creator" class="WhiteOctober\PagerfantaBundle\Routing\PagerfantaRouteGeneratorCreator">
<argument type="service" id="router" />
</service>

<!-- Twig Extension -->
<service id="twig.extension.pagerfanta" class="WhiteOctober\PagerfantaBundle\Twig\PagerfantaExtension" public="false">
<tag name="twig.extension" />
<argument type="service" id="service_container" />
<argument type="service" id="white_october_pagerfanta.route_generator_creator" />
<argument type="service" id="white_october_pagerfanta.view_factory" />
<argument>%white_october_pagerfanta.default_view%</argument>
<argument type="service" id="service_container" /> <!-- hack for not being able to inject the request -->
</service>

<service id="pagerfanta.convert_not_valid_max_per_page_to_not_found_listener" class="WhiteOctober\PagerfantaBundle\EventListener\ConvertNotValidMaxPerPageToNotFoundListener">
Expand Down
31 changes: 31 additions & 0 deletions Routing/PagerfantaRouteGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace WhiteOctober\PagerfantaBundle\Routing;

use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class PagerfantaRouteGenerator
{
private $urlGenerator;

private $routeName;
private $routeParams;
private $pageParam;

public function __construct(UrlGeneratorInterface $urlGenerator, $routeName, $routeParams, $pageParam)
{
$this->urlGenerator = $urlGenerator;
$this->routeName = $routeName;
$this->routeParams = $routeParams;
$this->pageParam = $pageParam;
}

public function __invoke($page)
{
$propertyAccessor = PropertyAccess::getPropertyAccessor();
Copy link
Contributor

Choose a reason for hiding this comment

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

you should create it in the constructor rather than on each invocation.

$propertyAccessor->setValue($this->routeParams, $this->pageParam, $page);

return $this->urlGenerator->generate($this->routeName, $this->routeParams);
}
}
29 changes: 29 additions & 0 deletions Routing/PagerfantaRouteGeneratorCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Pagerfanta package.
*
* (c) Pablo Díez <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace WhiteOctober\PagerfantaBundle\Routing;

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class PagerfantaRouteGeneratorCreator
{
private $urlGenerator;

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

public function create($routeName, $routeParams, $pageParam)
{
return new PagerfantaRouteGenerator($this->urlGenerator, $routeName, $routeParams, $pageParam);
}
}
35 changes: 35 additions & 0 deletions Tests/Routing/PagerfantaRouteGeneratorCreatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace WhiteOctober\PagerfantaBundle\Tests\Routing;

use Mockery;
use WhiteOctober\PagerfantaBundle\Routing\PagerfantaRouteGenerator;
use WhiteOctober\PagerfantaBundle\Routing\PagerfantaRouteGeneratorCreator;
use Mockery\MockInterface;

class PagerfantaRouteGeneratorCreatorTest extends \PHPUnit_Framework_TestCase
{
/** @var MockInterface */
private $urlGenerator;

/** @var PagerfantaRouteGeneratorCreator */
private $creator;

protected function setUp()
{
$this->urlGenerator = Mockery::mock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');

$this->creator = new PagerfantaRouteGeneratorCreator($this->urlGenerator);
}

public function testGenerate()
{
$routeName = 'foo';
$routeParams = array('a' => '1');
$pageParameter = '[page]';

$result = new PagerfantaRouteGenerator($this->urlGenerator, $routeName, $routeParams, $pageParameter);

$this->assertEquals($result, $this->creator->create($routeName, $routeParams, $pageParameter));
}
}
27 changes: 27 additions & 0 deletions Tests/Routing/PagerfantaRouteGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace WhiteOctober\PagerfantaBundle\Tests\Routing;

use Mockery;
use WhiteOctober\PagerfantaBundle\Routing\PagerfantaRouteGenerator;

class PagerfantaRouteGeneratorTest extends \PHPUnit_Framework_TestCase
{
public function testInvoke()
{
$urlGenerator = Mockery::mock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
$routeName = 'foo';
$routeParams = array('a' => '1');
$pageParameter = '[page]';

$page = 2;
$routeParamsWithPage = array_merge($routeParams, array('page' => $page));

$generator = new PagerfantaRouteGenerator($urlGenerator, $routeName, $routeParams, $pageParameter);

$result = 'blahblahblah';
$urlGenerator->shouldReceive('generate')->with($routeName, $routeParamsWithPage)->once()->andReturn($result);

$this->assertSame($result, $generator($page));
}
}
60 changes: 60 additions & 0 deletions Tests/Twig/PagerfantaExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace WhiteOctober\PagerfantaBundle\Tests\Twig;

use Mockery\MockInterface;
use Mockery;
use Symfony\Component\HttpFoundation\Request;
use WhiteOctober\PagerfantaBundle\Twig\PagerfantaExtension;
use Symfony\Component\DependencyInjection\Container;

class PagerfantaExtensionTest extends \PHPUnit_Framework_TestCase
{
/** @var MockInterface */
private $routeGeneratorCreator;

/** @var MockInterface */
private $viewFactory;

private $defaultView;
/** @var Container */
private $container;

/** @var PagerfantaExtension */
private $extension;

protected function setUp()
{
$this->routeGeneratorCreator = Mockery::mock('WhiteOctober\PagerfantaBundle\Routing\PagerfantaRouteGeneratorCreator');
$this->viewFactory = Mockery::mock('Pagerfanta\View\ViewFactoryInterface');
$this->defaultView = 'foo';
$this->container = new Container();

$this->extension = new PagerfantaExtension($this->routeGeneratorCreator, $this->viewFactory, $this->defaultView, $this->container);
}

public function testRenderPagerfanta()
{
$routeName = 'ups';
$routeParams = array('a' => 1);
$pageParam = '[page]';
$result = 'foobar';

$pagerfanta = Mockery::mock('Pagerfanta\Pagerfanta');

$request = new Request();
$request->attributes->set('_route', $routeName);
$request->attributes->set('_route_params', $routeParams);
$this->container->set('request', $request);

$routeGenerator = new \stdClass();
$this->routeGeneratorCreator->shouldReceive('create')->with($routeName, $routeParams, $pageParam)->once()->andReturn($routeGenerator);

$view = Mockery::mock('Pagerfanta\View\ViewInterface');
$view->shouldReceive('render')->with($pagerfanta, $routeGenerator, array())->once()->andReturn($result);

$this->viewFactory->shouldReceive('get')->with($this->defaultView)->once()->andReturn($view);

$this->assertSame($result, $this->extension->renderPagerfanta($pagerfanta));
}
}
54 changes: 24 additions & 30 deletions Twig/PagerfantaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

namespace WhiteOctober\PagerfantaBundle\Twig;

use Pagerfanta\View\ViewFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\HttpFoundation\Request;
use Pagerfanta\PagerfantaInterface;
use WhiteOctober\PagerfantaBundle\Routing\PagerfantaRouteGeneratorCreator;

/**
* PagerfantaExtension.
Expand All @@ -23,16 +24,17 @@
*/
class PagerfantaExtension extends \Twig_Extension
{
private $routeGeneratorCreator;
private $viewFactory;
private $defaultView;
private $container;

/**
* Constructor.
*
* @param ContainerInterface $container A container.
*/
public function __construct(ContainerInterface $container)
public function __construct(PagerfantaRouteGeneratorCreator $routeGeneratorCreator, ViewFactoryInterface $viewFactory, $defaultView, ContainerInterface $container)
{
$this->container = $container;
$this->routeGeneratorCreator = $routeGeneratorCreator;
$this->viewFactory = $viewFactory;
$this->defaultView = $defaultView;
$this->container = $container; // hack for not being able to inject the request
}

/**
Expand All @@ -57,13 +59,10 @@ public function getFunctions()
*/
public function renderPagerfanta(PagerfantaInterface $pagerfanta, $viewName = null, array $options = array())
{
if (null === $viewName) {
$viewName = $this->container->getParameter('white_october_pagerfanta.default_view');
}

$view = $viewName ?: $this->defaultView;
$routeGenerator = $this->createRouteGenerator($options);

return $this->container->get('white_october_pagerfanta.view_factory')->get($viewName)->render($pagerfanta, $routeGenerator, $options);
return $this->viewFactory->get($view)->render($pagerfanta, $routeGenerator, $options);
}

/**
Expand Down Expand Up @@ -100,16 +99,14 @@ public function getPageUrl(PagerfantaInterface $pagerfanta, $page, array $option
private function createRouteGenerator($options = array())
{
$options = array_replace(array(
'routeName' => null,
'routeParams' => array(),
'pageParameter' => '[page]',
), $options);
'routeName' => null,
'routeParams' => array(),
'pageParameter' => '[page]',
), $options);

$router = $this->container->get('router');
$request = $this->getRequest();

if (null === $options['routeName']) {
$request = $this->container->get('request');

$options['routeName'] = $request->attributes->get('_route');
if ('_internal' === $options['routeName']) {
throw new \Exception('PagerfantaBundle can not guess the route when used in a subrequest');
Expand All @@ -118,16 +115,13 @@ private function createRouteGenerator($options = array())
$options['routeParams'] = array_merge($request->query->all(), $request->attributes->get('_route_params'));
}

$routeName = $options['routeName'];
$routeParams = $options['routeParams'];
$pagePropertyPath = new PropertyPath($options['pageParameter']);

return function($page) use($router, $routeName, $routeParams, $pagePropertyPath) {
$propertyAccessor = PropertyAccess::getPropertyAccessor();
$propertyAccessor->setValue($routeParams, $pagePropertyPath, $page);
return $this->routeGeneratorCreator->create($options['routeName'], $options['routeParams'], $options['pageParameter']);
}

return $router->generate($routeName, $routeParams);
};
/** @return Request */
private function getRequest()
{
return $this->container->get('request');
}

/**
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
},
"require-dev": {
"symfony/symfony": "~2.2",
"phpunit/phpunit": "~3.7"
"phpunit/phpunit": "~3.7",
"mockery/mockery": "~0.8"
},
"autoload": {
"psr-0": { "WhiteOctober\\PagerfantaBundle": "" }
Expand Down