Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Izhevsk PHP Meetup #1. Неприметный аспект. Введение в AOP

Izhevsk PHP Meetup #1. Неприметный аспект. Введение в AOP

Дмитрий Кораблинов, ведущий разработчик, Kiwitaxi
Поговорим об основах аспектно-ориентированного программирования:
• выявим ситуации, в которых АОП может быть полезным
• рассмотрим примеры реализации с использованием Go! AOP PHP
• заглянем "под капот" этой библиотеки, чтобы выяснить, как это вообще работает

Izhevsk PHP Meetup

December 08, 2016
Tweet

More Decks by Izhevsk PHP Meetup

Other Decks in Technology

Transcript

  1. AOP в PHP Go! AOP Framework https://github.com/goaop/framework JMSAopBundle https://github.com/schmittjoh/JMSAopBundle AOP

    Pecl package https://pecl.php.net/package/AOP Exar Framework https://github.com/techdev-solutions/exar-framework 5
  2. Пример: кэширование class DataProvider { /** @var CacheItemPoolInterface */ private

    $cacheItemPool; public function getData() { $cacheKey = md5(__METHOD__); $cacheItem = $this->cacheItemPool->getItem($cacheKey); if ($cacheItem->isHit()) { return $cacheItem->get(); } $data = [1,2,3]; $cacheItem->set($data)->expiresAfter(3600); $this->cacheItemPool->save($cacheItem); return $data; } } 6
  3. Go! AOP Framework: Aspect class class CachingAspect implements Aspect {

    /** @var CacheItemPoolInterface */ private $cacheItemPool; /** @Around("@annotation(Annotation\Cacheable)") */ public function aroundCacheable(MethodInvocation $invocation) { $cacheKey = md5(get_class($invocation->getThis()) . $invocation->getMethod()->name); $cacheItem = $this->cacheItemPool->getItem($cacheKey); if ($cacheItem->isHit()) { return $cacheItem->get(); } $data = $invocation->proceed(); $cacheItem->set($data)->expiresAfter( $invocation->getMethod()->getAnnotation('Annotation\Cacheable')->lifetime ); $this->cacheItemPool->save($cacheItem); return $data; } } 7
  4. Go! AOP Framework: App class class DataProvider { /** @Annotation\Cacheable(lifetime=3600)

    */ public function getData() { $data = [1,2,3]; return $data; } } 8