Skip to content

Commit

Permalink
Merge pull request #106 from lmikelionis/origin/s_config_update
Browse files Browse the repository at this point in the history
Elasticsearch manager parametrized for ongr_settings.settings_provider in compiler pass
  • Loading branch information
saimaz committed Feb 26, 2015
2 parents c9bbd18 + 4850af7 commit d799625
Show file tree
Hide file tree
Showing 26 changed files with 254 additions and 581 deletions.
56 changes: 4 additions & 52 deletions Controller/SettingsListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@

namespace ONGR\SettingsBundle\Controller;

use ONGR\ElasticsearchBundle\ORM\Manager;
use ONGR\FilterManagerBundle\Filters\ViewData;
use ONGR\FilterManagerBundle\Filters\Widget\Choice\SingleTermChoice;
use ONGR\FilterManagerBundle\Filters\Widget\Pager\Pager;
use ONGR\FilterManagerBundle\Filters\Widget\Search\MatchSearch;
use ONGR\FilterManagerBundle\Filters\Widget\Sort\Sort;
use ONGR\FilterManagerBundle\Search\FiltersContainer;
use ONGR\FilterManagerBundle\Search\FiltersManager;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -54,53 +47,12 @@ public function listAction(Request $request)
*/
protected function getListData(Request $request)
{
/** @var Manager $manager */
$manager = $this->container->get('es.manager');

/** @var FiltersContainer $container */
$container = new FiltersContainer();

/** @var Pager $pager */
$pager = new Pager();
$pager->setRequestField('page');
$pager->setCountPerPage(15);
$container->set('pager', $pager);

/** @var Sort $sort */
$sort = new Sort();
$sort->setRequestField('sort');
$choices = [
'nameAsc' => ['label' => 'Name Asc', 'field' => 'name', 'order' => 'asc', 'default' => true],
'nameDesc' => ['label' => 'Name Desc', 'field' => 'name', 'order' => 'desc', 'default' => false],
];
$sort->setChoices($choices);
$container->set('sort', $sort);

/** @var MatchSearch $search */
$search = new MatchSearch();
if ($request->query->has('q')) {
$queryString = $request->query->get('q');
if (!empty($queryString)) {
$search->setRequestField('q');
$search->setField('name');
}
}
$container->set('search', $search);

/** @var SingleTermChoice $profile */
$profile = new SingleTermChoice();
$profile->setRequestField('profile');
$profile->setField('profile');
$container->set('profile', $profile);

/** @var FiltersManager $fm */
$fm = new FiltersManager($container, $manager->getRepository('ONGRSettingsBundle:Setting'));
$fmr = $fm->execute($request);
$filterManager = $this->get('ongr_settings.filters_manager')->execute($request);

return [
'data' => iterator_to_array($fmr->getResult()),
'filters' => $fmr->getFilters(),
'routeParams' => $fmr->getUrlParameters(),
'data' => iterator_to_array($filterManager->getResult()),
'filters' => $filterManager->getFilters(),
'routeParams' => $filterManager->getUrlParameters(),
];
}
}
20 changes: 19 additions & 1 deletion DependencyInjection/Compiler/ProviderPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,29 @@ function ($a, $b) {
protected function generateProvider(ContainerBuilder $container, $profile)
{
$id = "ongr_settings.dynamic_provider.{$profile}";

$managerName = $this->getElasticManagerFromRepository($container);

$provider = new Definition($container->getParameter('ongr_settings.settings_provider.class'), [$profile]);
$provider->addMethodCall('setManager', [new Reference('es.manager')]);
$provider->addMethodCall('setManager', [new Reference($managerName)]);
$provider->addTag('ongr_settings.settings_provider', ['profile' => $profile]);
$container->setDefinition($id, $provider);

return $id;
}

/**
* Gets Elasticsearch manager name from repository name.
*
* @param ContainerBuilder $container
*
* @return string
*/
private function getElasticManagerFromRepository(ContainerBuilder $container)
{
$repositoryName = $container->getParameter('ongr_settings.connection.repository');
$managerName = substr($repositoryName, 0, strrpos($repositoryName, '.'));

return $managerName;
}
}
4 changes: 4 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public function getConfigTreeBuilder()
->defaultValue('ongr-settings')
->info('Index name for settings')
->end()
->scalarNode('repository')
->defaultValue('es.manager.settings.setting')
->info('Elasticsearch repository for settings')
->end()
->scalarNode('host')
->info('Address of your settings database')
->defaultValue('127.0.0.1')
Expand Down
35 changes: 35 additions & 0 deletions DependencyInjection/ONGRSettingsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
Expand All @@ -33,6 +35,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('ongr_settings.connection.index_name', $config['connection']['index_name']);
$container->setParameter('ongr_settings.connection.port', $config['connection']['port']);
$container->setParameter('ongr_settings.connection.host', $config['connection']['host']);
$container->setParameter('ongr_settings.connection.repository', $config['connection']['repository']);

// Set profiles.
$container->setParameter('ongr_settings.settings_container.profiles', $config['profiles']);
Expand All @@ -45,11 +48,15 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services/general_settings.yml');
$loader->load('services/pair_storage.yml');

$loader->load('filters_container.yml');

if (isset($config['admin_user'])) {
$this->loadPersonalSettings($config['admin_user'], $container);
}

$this->injectPersonalSettings($container);

$this->setFiltersManager($container);
}

/**
Expand All @@ -64,6 +71,34 @@ protected function loadPersonalSettings($config, ContainerBuilder $containerBuil
$containerBuilder->setParameter('ongr_settings.settings.settings', $config['settings']);
}

/**
* Sets parameters for settings provider.
*
* @param array $config
* @param ContainerBuilder $containerBuilder
*/
protected function loadSettingsProvider($config, ContainerBuilder $containerBuilder)
{
$containerBuilder->setParameter('ongr_settings.settings.categories', $config['categories']);
}

/**
* Sets parameters for filter manager.
*
* @param ContainerBuilder $container
*/
protected function setFiltersManager(ContainerBuilder $container)
{
$definition = new Definition(
'ONGR\FilterManagerBundle\Search\FiltersManager',
[
new Reference('ongr_settings.filters_container'),
new Reference($container->getParameter('ongr_settings.connection.repository')),
]
);
$container->setDefinition('ongr_settings.filters_manager', $definition);
}

/**
* Injects additional General User settings to service container.
*
Expand Down
48 changes: 9 additions & 39 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ In your shell enter the following:

.. code-block:: bash
composer require ongr/SettingsBundle "~0.1"
composer require ongr/settings-bundle "~0.1"
..
Expand All @@ -62,8 +62,8 @@ Then register SettingsBundle and it dependant bundles in ``AppKernel.php``:
new Tedivm\StashBundle\TedivmStashBundle(),
new ONGR\CookiesBundle\ONGRCookiesBundle(),
new ONGR\ElasticsearchBundle\ONGRElasticsearchBundle(),
new ONGR\FilterManagerBundle\ONGRFilterManagerBundle(),
new ONGR\SettingsBundle\ONGRSettingsBundle(),
new ONGR\PagerBundle\ONGRPagerBundle(),
];
}
Expand Down Expand Up @@ -117,53 +117,23 @@ You should add an entry to your ``config.yml`` you should add an entry:
mappings:
- ONGRSettingsBundle
...
..
Using this config, console command below will create an Elasticsearch index called ``settings``
with 2 shards and 0 replicas, after running the console command mentioned above.

If you are planing to use SettingsBundle outside an
`ongr.io <http://ongr.io/>`_ platform you should also add following configuration to your ``config.yml``:
In case if you wish to use different Elasticsearch connection options, you can configure manager used in SettingsBundle
with following ``config.yml`` entry:

.. code-block:: yaml
parameters:
ongr_settings.settings_provider_es_manager: es.manager.settings
services:
ongr_settings.settings_manager:
class: %ongr_settings.settings_manager.class%
arguments:
- @translator
- @event_dispatcher
- @es.manager.settings
ongr_settings.listener.cookie_profile:
class: %ongr_settings.listener.cookie_profile.class%
calls:
- [ setSettingsContainer, [ @ongr_settings.settings_container ] ]
- [ setPersonalSettingsManager, [ @ongr_settings.settings.personal_settings_manager ] ]
- [ setManager, [ @es.manager.settings ] ]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
ongr_settings.profiles_manager:
class: %ongr_settings.profiles_manager.class%
arguments:
- @es.manager.settings
ongr_settings.settings_provider:
class: %ongr_settings.profiles_manager.class%
arguments:
- @es.manager.settings
ongr_settings.pair_storage:
class: %ongr_settings.pair_storage.class%
arguments:
- @es.manager.settings
ongr_settings:
connection:
repository: es.manager.default.setting
..
This example shows how you can configure settings repository, that is access in manager called ``default``.

Second - new index in Elasticsearch should be created.
This can be done by running a command in console:
Expand Down
64 changes: 64 additions & 0 deletions Resources/config/filters_container.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
parameters:
ongr_settings.filter.pager.class: ONGR\FilterManagerBundle\Filters\Widget\Pager\Pager
ongr_settings.filter.pager.request_field: page
ongr_settings.filter.pager.count_per_page: 15
ongr_settings.filter.pager.max_pages: 99

ongr_settings.filter.sort.class: ONGR\FilterManagerBundle\Filters\Widget\Sort\Sort
ongr_settings.filter.sort.request_field: sort
ongr_settings.filter.sort.choices:
nameAsc:
label: "Name Asc"
field: "name"
order: "asc"
default: true
nameDesc:
label: "Name Desc"
field: "name"
order: "desc"
default: false

ongr_settings.filter.match_search.class: ONGR\FilterManagerBundle\Filters\Widget\Search\MatchSearch
ongr_settings.filter.match_search.request_field: q
ongr_settings.filter.match_search.field: "name"

ongr_settings.filter.single_term.class: ONGR\FilterManagerBundle\Filters\Widget\Choice\SingleTermChoice
ongr_settings.filter.single_term.profile.request_field: profile
ongr_settings.filter.single_term.profile.field: profile


ongr_settings.filters_container.class: ONGR\FilterManagerBundle\Search\FiltersContainer

services:
ongr_settings.filter.pager:
class: "%ongr_settings.filter.pager.class%"
calls:
- ["setRequestField", ["%ongr_settings.filter.pager.request_field%"]]
- ["setCountPerPage", ["%ongr_settings.filter.pager.count_per_page%"]]
- ["setMaxPages", ["%ongr_settings.filter.pager.max_pages%"]]

ongr_settings.filter.sort:
class: "%ongr_settings.filter.sort.class%"
calls:
- ["setRequestField", ["%ongr_settings.filter.sort.request_field%"]]
- ["setChoices", ["%ongr_settings.filter.sort.choices%"]]

ongr_settings.filter.match_search:
class: "%ongr_settings.filter.match_search.class%"
calls:
- ["setRequestField", ["%ongr_settings.filter.match_search.request_field%"]]
- ["setField", ["%ongr_settings.filter.match_search.field%"]]

ongr_settings.filter.single_term.profile:
class: "%ongr_settings.filter.single_term.class%"
calls:
- ["setRequestField", ["%ongr_settings.filter.single_term.profile.request_field%"]]
- ["setField", ["%ongr_settings.filter.single_term.profile.field%"]]

ongr_settings.filters_container:
class: "%ongr_settings.filters_container.class%"
calls:
- ["set", ["pager", "@ongr_settings.filter.pager"]]
- ["set", ["sort", "@ongr_settings.filter.sort"]]
- ["set", ["search", "@ongr_settings.filter.match_search"]]
- ["set", ["profile", "@ongr_settings.filter.single_term.profile"]]
6 changes: 3 additions & 3 deletions Resources/config/services/general_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
arguments:
- @translator
- @event_dispatcher
- @es.manager
- @es.manager.settings

ongr_settings.settings_container:
class: %ongr_settings.settings_container.class%
Expand All @@ -39,11 +39,11 @@ services:
calls:
- [ setSettingsContainer, [ @ongr_settings.settings_container ] ]
- [ setPersonalSettingsManager, [ @ongr_settings.settings.personal_settings_manager ] ]
- [ setManager, [ @es.manager ] ]
- [ setManager, [ @es.manager.settings ] ]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

ongr_settings.profiles_manager:
class: %ongr_settings.profiles_manager.class%
arguments:
- @es.manager
- @es.manager.settings
2 changes: 1 addition & 1 deletion Resources/config/services/pair_storage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ services:
ongr_settings.pair_storage:
class: %ongr_settings.pair_storage.class%
arguments:
- @es.manager
- @es.manager.settings
Loading

0 comments on commit d799625

Please sign in to comment.