makerlabs/PagerBundle — pull request #11 by brentc (2012-03-27 01:00:57+00:00)
Title: Configuration, Dependency Injection services, and some usabilities tweaks

A significant overhaul of the pager bundle to facilitate using the pager via DI, as well as some usability improvements. All changes and new features should be BC compatible.

#### Adapter/PagerAdapterInterface:
* Added a `countResults()` method to the interface which returns the the number of results for an optionally provided offset and limit. This method now powers the `getTotalResults()` methods in both concrete implementations

#### Adapter/DoctrineOrmAdapter:
* Removed the redundant default value of null ( '= null') from the totalItems property.
* Renamed the `$query` property to `$queryBuilder` for clarity.
* Made the $queryBuilder (formerly query) constructor argument optional and added new `set/getQueryBuilder()` methods (DI Improvements)
* Made the adapter implement `\Countable` via `count()` method which returns totalResults
* Added optional offset and limit parameters to `getCountQuery()`
* Implemented `countResults()` to call `getCountQuery()` and cache results per `$offset` and `$limit` arguments.
* Changed `getTotalResults() to use the new `countResults()` method
* Added `get/setHydrationMode()` (DI Improvements)

#### Adapter/ArrayAdapter
* Removed the redundant default value of null ( '= null') from the totalItems property.
* Made the array constructor argument optional and added new `set/getArray()` methods (DI Improvements)
* Implemented `countResults()`
* Changed `getTotalResults()` to use the new `countResults()` method

#### Pager:
* Implemented `\Countable` and `\IteratorAggregate` to provide simpler pager usage:
* `count($pager)` / `{% results|length %}` - Returns the number of results in the current page
* `foreach($pager as $result)` / `{% for result in results %}` -- Iterates through the items in the current page of the pager
* Made the `$adapter` constructor argument optional and added new `set/getAdapter()` methods (DI Improvements)
* Fixed whitespacing around `$maxPages` property
* Added `getPageCount()` method. I would recommend a check on this method be the preferred way to check if results should show the paginator (`isPaginable()` is an unusual name and not immediately clear what it's checking):
`{% if results.getPageCount() > 1 %} {{ paginate(result, …) }} {% endif %}`
* Moved min-page handling from `setPage($page)` to `getPage()` and removed the call to `setPage(...)` inside `setLimit(…)` to allow for DI usage (setting the limit on instantiation was causing a call to `setPage()` and the `getLastPage()` call within was triggering an error when the adapter didn't yet have a queryBuilder).
* Changed all calls to `$this->page` to `$this->getPage()` to support the later-in-life min-page handling mentioned above.

#### Resources/config/adapters.xml
* Added DI definitions for both adapters: `maker_labs_pager.adapter.doctrine_orm`, `makers_labs_pager.adapter.array`

#### Resources/config/services.xml
* Added DI definition for `maker_labs_pager.pager` with a null adapter parameter and defaults for limit and max_pages as provided by new configuration options (see below)
* Reconfigured service definition for `makerlabs.twig.extension.pager` (see below)
* Added template config parameter as argument to `makerlabs.templating.helper.pager` definition.
* Changed tempting argument for `makerlabs.templating.helper.pager` to be the default templating engine, instead of the php-only engine.
* ***NOTE:*** All new service terms use the more-properly normalized 'maker_labs_pager' rather than the previous 'makerlabs_pager' prefix (which was left in-place wherever it was already used). This better matches the Symfony style of bundle name inflection.

#### DependencyInjection/Configuration
* Added configuration options under the maker_labs_pager root node: limit, max_pages, and template (DI Improvements). All options are optional with defaults that match the previously class-hardcoded defaults.

#### DependencyInjection/MakerLabsPagerExtension
* Added configuration handling
* Dynamically clones the `maker_labs_pager.pager` service into 2 adapter-specific services (with the same configuration directives as the generic service): `maker_labs_pager.doctrine_orm_pager` and `maker_labs_pager.array_pager`.

#### Twig/Extension/PagerExtension
* Now proxying calls to `Templating/Helper/PagerHelper` to reduce code duplication.
* Passes through the default template as defined in configuration, or if omitted, `MakerLabsPagerBundle:Pager:pagination.html.twig`

#### Templating/Helper/PagerHelper
* Accepts a default template from configuration, or a call-time template (which might be the PagerExtension twig template), or if neither, uses `MakerLabsPagerBundle:Pager:pagination.html.php`.