Skip to content

Commit

Permalink
Merge pull request #1320 from formapro-forks/4x-to-master
Browse files Browse the repository at this point in the history
4x to master
  • Loading branch information
makasim committed Dec 8, 2017
2 parents 4e4cd83 + a6c3963 commit 0632969
Show file tree
Hide file tree
Showing 78 changed files with 3,608 additions and 87 deletions.
15 changes: 9 additions & 6 deletions .travis.yml
Expand Up @@ -9,11 +9,11 @@ cache:
matrix:
include:
- php: 5.6
env: ES_VERSION=5.2.1
env: SYMFONY_VERSION='3.4.*' ES_VERSION=5.2.1
- php: 7.0
env: ES_VERSION=5.2.1
env: SYMFONY_VERSION='3.4.*' ES_VERSION=5.2.1
- php: 7.1
env: ES_VERSION=5.2.1
env: SYMFONY_VERSION='3.4.*' ES_VERSION=5.2.1
- php: 5.6
env: COMPOSER_FLAGS="--prefer-lowest --prefer-stable" ES_VERSION=5.2.1
- php: 5.6
Expand All @@ -23,6 +23,7 @@ matrix:
fast_finish: true

install:
- composer self-update
- mkdir /tmp/elasticsearch
- wget -O - https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}.tar.gz | tar xz --directory=/tmp/elasticsearch --strip-components=1
- /tmp/elasticsearch/bin/elasticsearch-plugin install -b mapper-attachments
Expand All @@ -32,9 +33,11 @@ install:

before_script:
- phpenv config-add travis.php.ini
- sh -c 'if [ "$TRAVIS_PHP_VERSION" = "5.6" ]; then echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;'
- sh -c 'if [ "$TRAVIS_PHP_VERSION" = "7.0" ]; then echo "extension = mongodb.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;'
- sh -c 'if [ "$SYMFONY_VERSION" != "" ]; then composer require --dev --no-update symfony/symfony=$SYMFONY_VERSION; fi;'
- if [ "$TRAVIS_PHP_VERSION" = "5.6" ]; then echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;
- if [ "$TRAVIS_PHP_VERSION" = "5.6" ]; then composer require --dev --no-update doctrine/mongodb-odm=^1; fi;
- if [ "$TRAVIS_PHP_VERSION" = "7.0" ]; then echo "extension = mongodb.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;
- if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then echo "extension = mongodb.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;
- if [ "$SYMFONY_VERSION" != "" ]; then composer require --dev --no-update symfony/symfony=$SYMFONY_VERSION; fi;
- if [ "$DEPENDENCIES" = "dev" ]; then perl -pi -e 's/^}$/,"minimum-stability":"dev"}/' composer.json; fi;
- composer update $COMPOSER_FLAGS --prefer-dist

Expand Down
34 changes: 31 additions & 3 deletions Command/PopulateCommand.php
Expand Up @@ -15,6 +15,8 @@
use FOS\ElasticaBundle\Event\TypePopulateEvent;
use FOS\ElasticaBundle\Index\IndexManager;
use FOS\ElasticaBundle\Index\Resetter;
use FOS\ElasticaBundle\Persister\PagerPersisterInterface;
use FOS\ElasticaBundle\Provider\PagerProviderRegistry;
use FOS\ElasticaBundle\Provider\ProviderRegistry;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\ProgressBar;
Expand Down Expand Up @@ -49,13 +51,23 @@ class PopulateCommand extends ContainerAwareCommand
*/
private $providerRegistry;

/**
* @var PagerProviderRegistry
*/
private $pagerProviderRegistry;

/**
* @var PagerPersisterInterface
*/
private $pagerPersister;

/**
* @var Resetter
*/
private $resetter;

/**
* @see Symfony\Component\Console\Command\Command::configure()
* {@inheritdoc}
*/
protected function configure()
{
Expand All @@ -82,6 +94,8 @@ protected function initialize(InputInterface $input, OutputInterface $output)
$this->dispatcher = $this->getContainer()->get('event_dispatcher');
$this->indexManager = $this->getContainer()->get('fos_elastica.index_manager');
$this->providerRegistry = $this->getContainer()->get('fos_elastica.provider_registry');
$this->pagerProviderRegistry = $this->getContainer()->get('fos_elastica.pager_provider_registry');
$this->pagerPersister = $this->getContainer()->get('fos_elastica.pager_persister');
$this->resetter = $this->getContainer()->get('fos_elastica.resetter');
$this->progressClosureBuilder = new ProgressClosureBuilder();

Expand Down Expand Up @@ -190,9 +204,23 @@ private function populateIndexType(OutputInterface $output, $index, $type, $rese
}

$offset = $options['offset'];
$provider = $this->providerRegistry->getProvider($index, $type);
$loggerClosure = $this->progressClosureBuilder->build($output, 'Populating', $index, $type, $offset);
$provider->populate($loggerClosure, $event->getOptions());

if ($this->getContainer()->getParameter('fos_elastica.use_v5_api') || getenv('FOS_ELASTICA_USE_V5_API')) {
$provider = $this->pagerProviderRegistry->getProvider($index, $type);

$pager = $provider->provide($options);

$options['indexName'] = $index;
$options['typeName'] = $type;
$options['batch_size'] = 100;
$options['skip_indexable_check'] = false;

$this->pagerPersister->insert($pager, $options);
} else {
$provider = $this->providerRegistry->getProvider($index, $type);
$provider->populate($loggerClosure, $options);
}

$this->dispatcher->dispatch(TypePopulateEvent::POST_TYPE_POPULATE, $event);

Expand Down
85 changes: 85 additions & 0 deletions DependencyInjection/Compiler/RegisterPagerProvidersPass.php
@@ -0,0 +1,85 @@
<?php

/*
* This file is part of the FOSElasticaBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\ElasticaBundle\DependencyInjection\Compiler;

use FOS\ElasticaBundle\Provider\PagerProviderInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class RegisterPagerProvidersPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('fos_elastica.pager_provider_registry')) {
return;
}

$defaultIndex = $container->getParameter('fos_elastica.default_index');
$registry = $container->getDefinition('fos_elastica.pager_provider_registry');

$registeredProviders = [];
foreach ($container->findTaggedServiceIds('fos_elastica.pager_provider') as $id => $attributes) {
foreach ($attributes as $attribute) {
if (!isset($attribute['type'])) {
throw new \InvalidArgumentException(sprintf('Elastica provider "%s" must specify the "type" attribute.', $id));
}

$index = isset($attribute['index']) ? $attribute['index'] : $defaultIndex;
$type = $attribute['type'];

if (isset($registeredProviders[$index][$type])) {
throw new \InvalidArgumentException(sprintf(
'Cannot register provider "%s". The provider "%s" has been registered for same index "%s" and type "%s"',
$id,
$registeredProviders[$index][$type],
$index,
$type
));
}

$providerDef = $container->getDefinition($id);
if (!$providerDef->getFactory()) {
// You are on your own if you use a factory to create a provider.
// It would fail in runtime if the factory does not return a proper provider.
$this->assertClassImplementsPagerProviderInterface($id, $providerDef->getClass());
}

$registeredProviders[$index][$type] = $id;
}
}

$registry->replaceArgument(0, $registeredProviders);
}

/**
* @param $providerId
* @param $providerClass
*
* @throws \InvalidArgumentException if provider service does not implement PagerProviderInterface
*/
private function assertClassImplementsPagerProviderInterface($providerId, $providerClass)
{
$rc = new \ReflectionClass($providerClass);

if (!$rc->implementsInterface(PagerProviderInterface::class)) {
throw new \InvalidArgumentException(sprintf(
'Elastica provider "%s" with class "%s" must implement "%s".',
$providerId,
$providerClass,
PagerProviderInterface::class
));
}
}
}
85 changes: 85 additions & 0 deletions DependencyInjection/Compiler/RegisterPersistersPass.php
@@ -0,0 +1,85 @@
<?php

/*
* This file is part of the FOSElasticaBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\ElasticaBundle\DependencyInjection\Compiler;

use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class RegisterPersistersPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('fos_elastica.persister_registry')) {
return;
}

$defaultIndex = $container->getParameter('fos_elastica.default_index');
$registry = $container->getDefinition('fos_elastica.persister_registry');

$registeredPersisters = [];
foreach ($container->findTaggedServiceIds('fos_elastica.persister') as $id => $attributes) {
foreach ($attributes as $attribute) {
if (!isset($attribute['type'])) {
throw new \InvalidArgumentException(sprintf('Elastica persister "%s" must specify the "type" attribute.', $id));
}

$index = isset($attribute['index']) ? $attribute['index'] : $defaultIndex;
$type = $attribute['type'];

if (isset($registeredPersisters[$index][$type])) {
throw new \InvalidArgumentException(sprintf(
'Cannot register persister "%s". The persister "%s" has been registered for same index "%s" and type "%s"',
$id,
$registeredPersisters[$index][$type],
$index,
$type
));
}

$persisterDef = $container->getDefinition($id);
if (!$persisterDef->getFactory()) {
// You are on your own if you use a factory to create a persister.
// It would fail in runtime if the factory does not return a proper persister.
$this->assertClassImplementsPersisterInterface($id, $persisterDef->getClass());
}

$registeredPersisters[$index][$type] = $id;
}
}

$registry->replaceArgument(0, $registeredPersisters);
}

/**
* @param $persisterId
* @param $persisterClass
*
* @throws \InvalidArgumentException if persister service does not implement ObjectPersisterInterface
*/
private function assertClassImplementsPersisterInterface($persisterId, $persisterClass)
{
$rc = new \ReflectionClass($persisterClass);

if (!$rc->implementsInterface(ObjectPersisterInterface::class)) {
throw new \InvalidArgumentException(sprintf(
'Elastica persister "%s" with class "%s" must implement "%s".',
$persisterId,
$persisterClass,
ObjectPersisterInterface::class
));
}
}
}
9 changes: 5 additions & 4 deletions DependencyInjection/Compiler/RegisterProvidersPass.php
Expand Up @@ -14,6 +14,9 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @deprecated since 4.x and will be removed in 5.0
*/
class RegisterProvidersPass implements CompilerPassInterface
{
/**
Expand All @@ -25,17 +28,15 @@ class RegisterProvidersPass implements CompilerPassInterface
private $implementations = [];

/**
* @see Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface::process()
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('fos_elastica.provider_registry')) {
return;
}

// Infer the default index name from the service alias
$defaultIndex = substr($container->getAlias('fos_elastica.index'), 19);

$defaultIndex = $container->getParameter('fos_elastica.default_index');
$registry = $container->getDefinition('fos_elastica.provider_registry');
$providers = $container->findTaggedServiceIds('fos_elastica.provider');

Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -322,6 +322,10 @@ protected function getPersistenceNode()
->end()
->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end()
->scalarNode('service')->end()
->booleanNode('pager_provider')
->defaultFalse()
->info('If true, a pager provider will also be registered.')
->end()
->end()
->end()
->arrayNode('listener')
Expand Down

0 comments on commit 0632969

Please sign in to comment.