Skip to content

Commit

Permalink
QueryExtensionScannerの実装
Browse files Browse the repository at this point in the history
  • Loading branch information
kiy0taka committed Sep 22, 2017
1 parent 5f55e6e commit c856bc4
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/Eccube/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ public function initialize()
]),
new \Eccube\Di\Scanner\RepositoryScanner([
$this['config']['root_dir'].'/src/Eccube/Repository'
]),
new \Eccube\Di\Scanner\QueryExtensionScanner([
$this['config']['root_dir'].'/src/Eccube/Repository'
])
],
'eccube.di.generator.dir' => $this['config']['root_dir'].'/app/cache/provider'
Expand Down
62 changes: 62 additions & 0 deletions src/Eccube/Di/Scanner/QueryExtensionScanner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) 2000-2017 LOCKON CO.,LTD. All Rights Reserved.
*
* http://www.lockon.co.jp/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

namespace Eccube\Di\Scanner;


use Eccube\Annotation\QueryExtension;
use Eccube\Di\ComponentDefinition;

class QueryExtensionScanner extends ComponentScanner
{
/**
* QueryExtensionScanner constructor.
* @param array|string[] $scanDirs
*/
public function __construct($scanDirs)
{
parent::__construct($scanDirs);
}

public function getAnnotationClass()
{
return QueryExtension::class;
}

public function createComponentDefinition($anno, $refClass)
{
return new ComponentDefinition($refClass->getName(), $refClass);
}

public function generateExtend(\Twig_Environment $twig, array $components)
{
return $twig->createTemplate(
'$app->extend("eccube.queries", function ($queries, $app) {
{% for extension in extensions -%}
$queries->addCustomizer($app["{{ extension.id }}"]);
{% endfor %}
return $queries;
});')->render(['extensions' => $components]);
}
}
2 changes: 0 additions & 2 deletions src/Eccube/ServiceProvider/EccubeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,6 @@ public function register(Container $app)
$app['eccube.queries'] = function () {
return new \Eccube\Doctrine\Query\Queries();
};
// TODO QueryCustomizerの追加方法は要検討
$app['eccube.queries']->addCustomizer(new \Acme\Entity\AdminProductListCustomizer());

$app['eccube.purchase.context'] = $app->protect(function (ItemHolderInterface $origin = null) {
return new PurchaseContext($origin);
Expand Down
63 changes: 63 additions & 0 deletions tests/Eccube/Tests/Di/Scanner/QueryExtensionScannerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) 2000-2017 LOCKON CO.,LTD. All Rights Reserved.
*
* http://www.lockon.co.jp/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

namespace Eccube\Tests\Di\Scanner;

use Doctrine\ORM\QueryBuilder;
use Eccube\Di\Scanner\QueryExtensionScanner;
use Eccube\Doctrine\Query\Queries;
use Eccube\Repository\QueryKey;
use Eccube\Tests\Di\Test\QueryCustomizerClass;

class QueryExtensionScannerTest extends AbstractScannerTest
{
public function setUp()
{
parent::setUp();
$this->container['eccube.queries'] = function() {
return new Queries();
};
}

protected function getAutoWiring()
{
return new QueryExtensionScanner([__DIR__.'/../Test']);
}

public function testQueryExtension()
{
$this->di->build($this->container);

self::assertArrayHasKey(QueryCustomizerClass::class, $this->container);
}

public function testCustomize()
{
$this->di->build($this->container);

$qb = $this->createMock(QueryBuilder::class);
$this->container['eccube.queries']->customize(QueryKey::PRODUCT_SEARCH, $qb, []);

self::assertTrue($this->container[QueryCustomizerClass::class]->customized);
}
}
11 changes: 10 additions & 1 deletion tests/Eccube/Tests/Di/Test/FormExtensionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@
namespace Eccube\Tests\Di\Test;

use Eccube\Annotation\FormExtension;
use Symfony\Component\Form\AbstractTypeExtension;

/**
* @FormExtension
*/
class FormExtensionClass
class FormExtensionClass extends AbstractTypeExtension
{
/**
* Returns the name of the type being extended.
*
* @return string The name of the type being extended
*/
public function getExtendedType()
{
}
}
51 changes: 51 additions & 0 deletions tests/Eccube/Tests/Di/Test/QueryCustomizerClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) 2000-2017 LOCKON CO.,LTD. All Rights Reserved.
*
* http://www.lockon.co.jp/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

namespace Eccube\Tests\Di\Test;


use Doctrine\ORM\QueryBuilder;
use Eccube\Annotation\QueryExtension;
use Eccube\Doctrine\Query\QueryCustomizer;
use Eccube\Repository\QueryKey;

/**
* @QueryExtension(QueryKey::PRODUCT_SEARCH)
*/
class QueryCustomizerClass implements QueryCustomizer
{

public $customized = false;

/**
* クエリをカスタマイズします。
*
* @param QueryBuilder $builder
* @param array $params
* @param string $queryKey
*/
public function customize(QueryBuilder $builder, $params, $queryKey)
{
$this->customized = true;
}
}

0 comments on commit c856bc4

Please sign in to comment.