Skip to content

Instantly share code, notes, and snippets.

@hidenorigoto
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hidenorigoto/96fc0a4c4f04b081c0eb to your computer and use it in GitHub Desktop.
Save hidenorigoto/96fc0a4c4f04b081c0eb to your computer and use it in GitHub Desktop.
bear aopチャレンジ
<?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));
}
}
<?php
// src/Annotation/Benchmark.php
namespace Koriym\Work\Annotation;
/**
* @Annotation
* @Target("METHOD")
*/
class Benchmark
{
}
<?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;
}
}
<?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