diff --git a/.travis.yml b/.travis.yml index 71e3bf0..9fe794e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,13 +6,14 @@ php: - 7.4 env: - - SYMFONY_VERSION="2.1" - - SYMFONY_VERSION="3.0" + - SYMFONY_VERSION="3.4" + - SYMFONY_VERSION="4.0" + - SYMFONY_VERSION="4.4" before_script: - composer self-update - composer --version - composer require symfony/framework-bundle:${SYMFONY_VERSION} --no-update - - composer install -n --dev --prefer-source + - composer install -n --prefer-source script: phpunit --coverage-text --configuration phpunit.xml.dist diff --git a/Controller/ElasticsearchProxyController.php b/Controller/ElasticsearchProxyController.php index c82232c..b6e7edf 100644 --- a/Controller/ElasticsearchProxyController.php +++ b/Controller/ElasticsearchProxyController.php @@ -1,19 +1,19 @@ container->getParameter('xola_elasticsearch_proxy'); + $config = $container->getParameter('xola_elasticsearch_proxy'); if (!in_array($index, $config['client']['indexes'])) { throw new AccessDeniedHttpException(); } @@ -31,7 +31,7 @@ public function proxyAction(Request $request, $index, $slug) $dispatcher->dispatch('elasticsearch_proxy.before_elasticsearch_request', $event); $data = $event->getQuery(); // Get url for elastic search - $url = $this->getElasticSearchUrl($request->getQueryString(), $index, $slug); + $url = $this->getElasticSearchUrl($request->getQueryString(), $index, $slug, $container); $response = $this->makeRequestToElasticsearch($url, $request->getMethod(), $data); $event->setResponse($response); $dispatcher->dispatch('elasticsearch_proxy.after_elasticsearch_response', $event); @@ -48,9 +48,9 @@ public function proxyAction(Request $request, $index, $slug) * * @return string */ - public function getElasticSearchUrl($queryStr, $index, $slug) + public function getElasticSearchUrl($queryStr, $index, $slug, ContainerInterface $container): string { - $config = $this->container->getParameter('xola_elasticsearch_proxy'); + $config = $container->getParameter('xola_elasticsearch_proxy'); // Construct url for making elastic search request $url = $config['client']['protocol'] . '://' . $config['client']['host'] . ':' . $config['client']['port'] . '/' . $index . '/' . $slug; @@ -72,7 +72,7 @@ public function getElasticSearchUrl($queryStr, $index, $slug) * * @return Response */ - public function makeRequestToElasticsearch($url, $method, $data) + public function makeRequestToElasticsearch($url, $method, $data): Response { $ch = curl_init(); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index cf0501f..579403c 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -6,7 +6,7 @@ class Configuration implements ConfigurationInterface { - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('xola_elasticsearch_proxy'); @@ -26,6 +26,7 @@ public function getConfigTreeBuilder() ->arrayNode('roles_skip_auth_filter')->prototype('scalar')->end() ->end() ->end(); + return $treeBuilder; } -} \ No newline at end of file +} diff --git a/DependencyInjection/XolaElasticsearchProxyExtension.php b/DependencyInjection/XolaElasticsearchProxyExtension.php index d73d35d..77d5970 100644 --- a/DependencyInjection/XolaElasticsearchProxyExtension.php +++ b/DependencyInjection/XolaElasticsearchProxyExtension.php @@ -11,18 +11,10 @@ class XolaElasticsearchProxyExtension extends Extension public function load(array $configs, ContainerBuilder $container) { $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); - $loader->load('config.yml'); - - $defaultConfig = $container->getParameter($this->getAlias()); - array_splice($configs, 0, 0, array($defaultConfig)); + $loader->load('services.yml'); $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); $container->setParameter($this->getAlias(), $config); } - - public function getAlias() - { - return 'xola_elasticsearch_proxy'; - } } diff --git a/Event/ElasticsearchProxyEvent.php b/Event/ElasticsearchProxyEvent.php index 0193a2e..3ba901a 100644 --- a/Event/ElasticsearchProxyEvent.php +++ b/Event/ElasticsearchProxyEvent.php @@ -1,12 +1,13 @@ response = $response; } - /** - * @param mixed $request - */ public function setRequest($request) { $this->request = $request; } - /** - * @return mixed - */ public function getRequest() { return $this->request; } - /** - * @param mixed $index - */ public function setIndex($index) { $this->index = $index; } - /** - * @return mixed - */ public function getIndex() { return $this->index; } - /** - * @param mixed $slug - */ public function setSlug($slug) { $this->slug = $slug; } - /** - * @return mixed - */ public function getSlug() { return $this->slug; @@ -95,18 +78,18 @@ public function getQuery() } /** - * @param \Symfony\Component\HttpFoundation\Response $response + * @param Response $response */ - public function setResponse($response) + public function setResponse(Response $response) { $this->response = $response; } /** - * @return \Symfony\Component\HttpFoundation\Response + * @return Response */ - public function getResponse() + public function getResponse(): ?Response { return $this->response; } -} \ No newline at end of file +} diff --git a/Resources/config/config.yml b/Resources/config/config.yml deleted file mode 100644 index 0169d91..0000000 --- a/Resources/config/config.yml +++ /dev/null @@ -1,9 +0,0 @@ -parameters: - xola_elasticsearch_proxy: - client: - protocol: http - host: localhost - port: 9200 - indexes: [] - - roles_skip_auth_filter: [] \ No newline at end of file diff --git a/Resources/config/services.yml b/Resources/config/services.yml new file mode 100644 index 0000000..59446c5 --- /dev/null +++ b/Resources/config/services.yml @@ -0,0 +1,8 @@ +services: + _defaults: + autowire: true + public: false + + Xola\ElasticsearchProxyBundle\Controller\: + resource: '../../Controller' + tags: ['controller.service_arguments'] diff --git a/Tests/Controller/ElasticSearchProxyControllerTest.php b/Tests/Controller/ElasticsearchProxyControllerTest.php similarity index 66% rename from Tests/Controller/ElasticSearchProxyControllerTest.php rename to Tests/Controller/ElasticsearchProxyControllerTest.php index 7953b00..26b13f4 100644 --- a/Tests/Controller/ElasticSearchProxyControllerTest.php +++ b/Tests/Controller/ElasticsearchProxyControllerTest.php @@ -8,10 +8,7 @@ class ElasticsearchProxyControllerTest extends TestCase { - /** @var ElasticsearchProxyController */ - private $controller; - - protected function setUp(): void + public function testGetElasticSearchUrl() { $container = $this->createMock(ContainerInterface::class); $config = array( @@ -23,19 +20,12 @@ protected function setUp(): void ), 'roles_skip_auth_filter' => array() ); - $container->expects($this->any())->method("getParameter")->with("xola_elasticsearch_proxy") + $container->expects($this->once())->method("getParameter")->with("xola_elasticsearch_proxy") ->willReturn($config); + $controller = new ElasticsearchProxyController(); - $this->controller = new ElasticsearchProxyController(); - $this->controller->setContainer($container); - } - - - public function testGetElasticSearchUrl() - { - $url = $this->controller->getElasticSearchUrl('pretty=true', 'logs', '_search'); + $url = $controller->getElasticSearchUrl('pretty=true', 'logs', '_search', $container); $this->assertEquals('http://localhost:9200/logs/_search?pretty=true', $url, 'Should be url built from proxy'); } - -} \ No newline at end of file +} diff --git a/composer.json b/composer.json index 8972427..35e5dba 100644 --- a/composer.json +++ b/composer.json @@ -5,19 +5,9 @@ "keywords": ["elasticsearch", "symfony", "search", "proxy", "authorization"], "homepage": "https://github.com/xola/XolaElasticsearchProxyBundle", "license": "MIT", - "authors": [ - { - "name": "Venkata Krishna Kotra", - "email": "kotrakrishna@gmail.com" - }, - { - "name": "Anush Ramani", - "email": "anush@xola.com" - } - ], "require": { "php": ">=7.1", - "symfony/framework-bundle": ">=2.1 <4.0", + "symfony/framework-bundle": "^3.4|^4.0", "ext-curl": "*", "ext-json": "*" }, @@ -25,9 +15,11 @@ "phpunit/phpunit": "^7" }, "autoload": { - "psr-0": { - "Xola\\ElasticsearchProxyBundle": "" - } - }, - "target-dir": "Xola/ElasticsearchProxyBundle" + "psr-4": { + "Xola\\ElasticsearchProxyBundle\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + } }