Skip to content

Instantly share code, notes, and snippets.

@izayoi256
Last active October 19, 2018 07:21
Show Gist options
  • Save izayoi256/91294506f3c9d775c213297616dbd155 to your computer and use it in GitHub Desktop.
Save izayoi256/91294506f3c9d775c213297616dbd155 to your computer and use it in GitHub Desktop.
EC-CUBE4 エンティティ拡張で既存メソッドを書き換える ref: https://qiita.com/izayoi256/items/cdd8074f7264b5d90f90
// 前略
public function getRoles()
{return $this->getTraitRoles();}
//後略
<?php
// 前略
public function getTraitRoles() // 関数名を変更
// 後略
<?php
namespace Customize\Service;
use Eccube\Service\EntityProxyService as BaseService;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use Symfony\Component\Console\Output\OutputInterface;
class EntityProxyService extends BaseService
{
/**
* @var BaseService
*/
private $baseService;
public function __construct(EntityManagerInterface $entityManager, BaseService $baseService)
{
parent::__construct($entityManager);
$this->baseService = $baseService;
}
public function generate($includesDirs, $excludeDirs, $outputDir, OutputInterface $output = null)
{
// 生成されたプロキシファイル一覧
$generatedFiles = $this->baseService->generate($includesDirs, $excludeDirs, $outputDir, $output);
foreach ($generatedFiles as $generatedFile) {
// 末尾が/Customer.phpなら
if (preg_match('#/Customer.php$#', $generatedFile)) {
$entityTokens = Tokens::fromCode(file_get_contents($generatedFile));
$functionIndex = 0;
// T_FUNCTIONを探す
while(($functionIndex = $entityTokens->getNextTokenOfKind($functionIndex, [[T_FUNCTION]])) > 0) {
$nextIndex = $entityTokens->getNextMeaningfulToken($functionIndex);
$nextToken = $entityTokens[$nextIndex];
// T_FUNCTIONの次の空白以外のトークンがT_STRINGで、getRolesなら
if ($nextToken->isGivenKind(T_STRING) && $nextToken->getContent() === 'getRoles') {
// 次の'{'から対応する'}'まで
$startIndex = $entityTokens->getNextTokenOfKind($nextIndex, ['{']);
$endIndex = $entityTokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startIndex);
// を'{return $this->getTraitRoles();}'で置き換える
$newTokens = Tokens::fromCode('{return $this->getTraitRoles();}');
$entityTokens->overrideRange($startIndex, $endIndex, $newTokens);
$code = $entityTokens->generateCode();
file_put_contents($generatedFile, $code);
break;
}
}
}
}
return $generatedFiles;
}
}
$ php bin/console eccube:generate:proxies
gen -> /var/www/html/app/proxy/entity/Customer.php
$ bin/console cache:clear --no-warmup
$ rm app/proxy/entity/Customer.php
$ php bin/console eccube:generate:proxies
var_export((new \Eccube\Entity\Customer())->getRoles());
// array (
// 0 => 'ROLE_CUSTOMER',
// )
$ php bin/console eccube:generate:proxies
gen -> /var/www/html/app/proxy/entity/Customer.php
var_export((new \Eccube\Entity\Customer())->getRoles());
// array (
// 0 => 'ROLE_CUSTOMER',
// 1 => 'ROLE_HOGE',
// )
# ファイル末尾
Customize\Service\EntityProxyService:
decorates: Eccube\Service\EntityProxyService
arguments:
- '@doctrine.orm.default_entity_manager'
- '@Customize\Service\EntityProxyService.inner'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment