-
-
Save hidenorigoto/96fc0a4c4f04b081c0eb to your computer and use it in GitHub Desktop.
bear aopチャレンジ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/Module/AppModule.php | |
namespace Koriym\Work\Module; | |
use BEAR\Package\Module\Package\StandardPackageModule; | |
use Koriym\Work\Interceptor\BenchMarker; | |
use Monolog\Handler\StreamHandler; | |
use Monolog\Logger; | |
use Ray\Di\AbstractModule; | |
use Ray\Di\Di\Inject; | |
use Ray\Di\Di\Named; | |
class AppModule extends AbstractModule | |
{ | |
/** | |
* @var string | |
*/ | |
private $context; | |
/** | |
* @param string $context | |
* | |
* @Inject | |
* @Named("app_context") | |
*/ | |
public function __construct($context = 'prod') | |
{ | |
$this->context = $context; | |
parent::__construct(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function configure() | |
{ | |
$this->install(new StandardPackageModule('Koriym\Work', $this->context, dirname(dirname(__DIR__)))); | |
$this->bind('Psr\Log\LoggerInterface')->to('Monolog\Logger'); | |
$this->bind('Monolog\Logger')->toConstructor(['name'=>'bear!', 'handlers'=>[new StreamHandler(__DIR__ . '/../../logs/debug.log', Logger::DEBUG)], 'processors'=>[]]); | |
$this->bindInterceptor($this->matcher->any(), $this->matcher->annotatedWith('Koriym\Work\Annotation\Benchmark'), [$this->requestInjection('Koriym\Work\Interceptor\BenchMarker')]); | |
// override module | |
// $this->install(new SmartyModule($this)); | |
// $this->install(new AuraViewModule($this)); | |
// install application dependency | |
// $this->install(new App\Dependency); | |
// install application aspect | |
// $this->install(new App\Aspect($this)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/Annotation/Benchmark.php | |
namespace Koriym\Work\Annotation; | |
/** | |
* @Annotation | |
* @Target("METHOD") | |
*/ | |
class Benchmark | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/Interceptor/BenchMarker.php | |
namespace Koriym\Work\Interceptor; | |
use Psr\Log\LoggerInterface; | |
use Ray\Aop\MethodInterceptor; | |
use Ray\Aop\MethodInvocation; | |
use Ray\Di\Di\Inject; | |
class BenchMarker implements MethodInterceptor | |
{ | |
/** | |
* @var LoggerInterface | |
*/ | |
private $logger; | |
/** | |
* @Inject() | |
*/ | |
public function setLogger(LoggerInterface $logger) | |
{ | |
$this->logger = $logger; | |
} | |
public function invoke(MethodInvocation $invocation) | |
{ | |
$start = microtime(true); | |
$result = $invocation->proceed(); // 元のメソッドの実行 | |
$time = microtime(true) - $start; | |
$this->logger->debug(sprintf('benchmark: %s', $time)); | |
return $result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/Resource/Page/Calc.php | |
namespace Koriym\Work\Resource\Page; | |
use BEAR\Resource\ResourceObject; | |
use BEAR\Sunday\Inject\ResourceInject; | |
use Koriym\Work\Annotation\Benchmark; | |
use Psr\Log\LoggerInterface; | |
use Ray\Di\Di\Inject; | |
class Calc extends ResourceObject | |
{ | |
use ResourceInject; | |
/** | |
* @var LoggerInterface | |
*/ | |
private $logger; | |
/** | |
* @Inject() | |
*/ | |
public function setLogger(LoggerInterface $logger) | |
{ | |
$this->logger = $logger; | |
} | |
/** | |
* @Benchmark() | |
*/ | |
public function onGet($a, $b) | |
{ | |
$add = $this->resource | |
->get | |
->uri('app://self/add') | |
->withQuery(['a' => $a, 'b' => $b]) | |
->request(); | |
$this['a'] = $a; | |
$this['b'] = $b; | |
$this['add'] = $add; | |
$this['test'] = get_class($this->logger); | |
$this->logger->debug('di'); | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment