Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Aspects of love slideshare

154 views

Published on

Slides from my presentation on Aspect Oriented Programming at the PHPBenelux 2019 Conference

Published in: Software
  • Be the first to comment

  • Be the first to like this

Aspects of love slideshare

  1. 1. Aspects of Love
  2. 2. Aspects of Love Go Deep into the Rabbit Hole, and Enter a Wonderland of Possibilities with Aspect Oriented Programming
  3. 3. Aspects of Love
  4. 4. Aspects of Love What is AOP? Aspect Oriented Programming
  5. 5. Aspects of Love What is AOP? Procedural Programming Object Oriented Programming (OOP) Functional Programming (FP) Aspect Oriented Programming (AOP)
  6. 6. Aspects of Love What is AOP? Object Oriented Programming (OOP) + Aspect Oriented Programming (AOP)
  7. 7. Aspects of Love Separation of Concerns Business Logic Aspects Cross-Cutting Concerns
  8. 8. Aspects of Love namespace AppHttpControllers; use AppUser; use AppHttpControllersController; class UserController extends Controller { public function showProfile($id) { return view('user.profile', ['user' => User::findOrFail($id)]); } } Route::get('user/{id}', 'UserController@showProfile');
  9. 9. Aspects of Love But only Logged- in Users should be able to see another User’s Profile
  10. 10. Aspects of Love class UserController extends Controller { public function __construct() { $this->middleware('auth'); } ... } Route::get('profile', 'UserController@showProfile') ->middleware('auth');
  11. 11. Aspects of Love We need to log every time that somebody views any User’s Profile
  12. 12. Aspects of Love Don’t forget that we also want to cache recently accessed User Profiles to improve performance
  13. 13. Aspects of Love And in the functionality to Edit a User’s Profile, we need an audit record of all the changes that were made Except passwords (of course)
  14. 14. Aspects of Love class UserController extends Controller { public function __construct() { $this->middleware('auth'); $this->middleware('log'); $this->middleware('audit')->only('edit'); $this->middleware('cache'); } ... }
  15. 15. Aspects of Love
  16. 16. Aspects of Love S O L I D
  17. 17. Aspects of Love S O L I D
  18. 18. Aspects of Love Cross-Cutting Concerns Security Privilege Checks Logging Auditing Caching Transaction Management
  19. 19. Aspects of Love Terminology Aspect Advice Join Point Pointcut Advice Chain Aspect Weaver
  20. 20. Aspects of Love Terminology Aspect AOP Implementation of a Cross-Cutting Concern
  21. 21. Aspects of Love Terminology Aspects apply an “Advice” A specific behaviour or piece of code logic
  22. 22. Aspects of Love Terminology Aspects apply Advices at “Join Points” Points within the Business Logic where the additional behaviour should be joined
  23. 23. Aspects of Love Terminology Join Points are identified by “Pointcuts” Expressions that identify one or more Join Points where an Advice should be applied.
  24. 24. Aspects of Love Terminology An Advice Chain is a series of Advices that will all be applied to a single Join Point
  25. 25. Aspects of Love
  26. 26. Aspects of Love namespace SystemAspect; use GoAopAspect; use GoAopInterceptMethodInvocation; use GoLangAnnotationBefore; class LoggingAspect implements Aspect { /** * @Before("execution(public *UserController->showProfile(*))", order=-128) */ public function beforeMethodExecution(MethodInvocation $invocation) { Log::info(sprintf( 'Access to %s with arguments %s’, $invocation, json_encode($invocation->getArguments()) ); } }
  27. 27. Aspects of Love namespace SystemAspect; use GoAopAspect; use GoAopInterceptMethodInvocation; use GoLangAnnotationBefore; class LoggingAspect implements Aspect { /** * @Before("execution(public *UserController->showProfile(*))", order=-128) */ public function beforeMethodExecution(MethodInvocation $invocation) { Log::info(sprintf( 'Access to %s with arguments %s’, $invocation, json_encode($invocation->getArguments()) ); } }
  28. 28. Aspects of Love namespace SystemAspect; use GoAopAspect; use GoAopInterceptMethodInvocation; use GoLangAnnotationBefore; class LoggingAspect implements Aspect { /** * @Before("execution(public *UserController->showProfile(*))", order=-128) */ public function beforeMethodExecution(MethodInvocation $invocation) { Log::info(sprintf( 'Access to %s with arguments %s’, $invocation, json_encode($invocation->getArguments()) ); } }
  29. 29. Aspects of Love
  30. 30. Aspects of Love PointCuts Methods Public or Protected Final or Static Before After After Return After Throw Around
  31. 31. Aspects of Love PointCuts Properties Public or Protected Final or Static Get Set
  32. 32. Aspects of Love PointCuts Class Initialisation System Functions (within namespaces)
  33. 33. Aspects of Love Terminology Aspect Weaver
  34. 34. Aspects of Love Terminology Aspect Weaver The Code/Framework that applies Aspects to the appropriate Join Points in the Business Logic
  35. 35. Aspects of Love And that’s the Magic!
  36. 36. Aspects of Love And that’s the Magic! AOP allows us to do “stuff” before or after a method call (or even override it completely), without modifying the original code!
  37. 37. Aspects of Love But I don’t like Magic! ™
  38. 38. Aspects of Love But the number of AOP Libraries/frameworks that are available suggests that some people do like their magic
  39. 39. Aspects of Love AOP Libraries/Extensions AOP PECL Extension PHP 5 (there is a PR for PHP 7, but unmerged) https://github.com/AOP-PHP/AOP http://aop-php.github.io/
  40. 40. Aspects of Love AOP Libraries/Extensions JMS AOP Bundle For Symfony 2+ http://jmsyst.com/bundles/JMSAopBundle https://github.com/schmittjoh/JMSAopBu ndle
  41. 41. Aspects of Love AOP Libraries/Extensions Lithium Framework https://github.com/UnionOfRAD/lithium
  42. 42. Aspects of Love AOP Libraries/Extensions Ray.Aop https://github.com/ray-di/Ray.Aop
  43. 43. Aspects of Love AOP Libraries/Extensions Kdyby/Aop For the Nette Framework https://github.com/Kdyby/Aop
  44. 44. Aspects of Love AOP Libraries/Extensions Swoft Coroutine componentisation Framework, based on swoole 2 native Coroutines https://github.com/swoft-cloud/swoft
  45. 45. Aspects of Love AOP Libraries/Extensions Flow Formerly Typo3 Flow, formerly Flow3 https://flow.neos.io/
  46. 46. Aspects of Love AOP Libraries/Extensions Go! AOP https://github.com/goaop/framework https://go-aop-php.readthedocs.io/
  47. 47. Aspects of Love Go! AOP framework Symfony Bundle: https://github.com/goaop/goaop-symfony-bundle Laravel bridge: https://github.com/goaop/goaop-laravel-bridge Zend 2 module: https://github.com/goaop/goaop-zf2-module IDEA plugin (PHPStorm, IntelliJ): https://github.com/goaop/idea-plugin
  48. 48. Aspects of Love Go! AOP – How does the magic work?
  49. 49. Aspects of Love But I don’t like Magic! ™
  50. 50. Aspects of Love Clarke’s third law “Any sufficiently advanced technology is indistinguishable from magic.” Arthur C. Clarke, “Profiles of The Future”, 1961
  51. 51. Aspects of Love Go! AOP – How does the magic work? Wraps the Composer loader with its own proxy Intercepts calls to load a class file, and applies its own stream filter to read the file without actually “loading” it Tokenises and parses the file to an AST (using the nikic/PHP-Parser library), and applies static reflection
  52. 52. Aspects of Love Go! AOP – How does the magic work? Checks all registered pointcuts and transforms the original class code, renaming it as <class>_AopProxied Creates its own version of the original class file, with the additional logic from the relevant Advices, extending <class>_AopProxied, and stores that new file in the cache folder
  53. 53. Aspects of Love Go! AOP – How does the magic work? Directs the autoloader to load the new file from the cache folder Autoloader will subsequently load <class>_AopProxied as well, because the new version of the class extends that, but no further transformation is required
  54. 54. Aspects of Love So they all lived happily ever after…
  55. 55. Aspects of Love So they all lived happily ever after… Well, not necessarily…
  56. 56. Aspects of Love Potential Problems with AOP
  57. 57. Aspects of Love Potential Problems with AOP Generic Advices Non-Obvious Aspects Fragile Pointcuts Lack of Visibility Difficult to Debug
  58. 58. Aspects of Love Potential Problems with AOP
  59. 59. Aspects of Love Potential Benefits with AOP
  60. 60. Aspects of Love Potential Benefits with AOP Separation of Concerns Separate Unit Tests for Business Logic and for Aspects Simplifies Unit Testing for Business Logic No need to mock Aspect Objects like Loggers
  61. 61. Aspects of Love Potential Benefits with AOP
  62. 62. Aspects of Love Libraries Using AOP AspectMock https://github.com/Codeception/AspectMock Roave/StrictPHP https://github.com/Roave/StrictPhp PHPDeal (Design by Contract) https://github.com/php-deal/framework
  63. 63. Aspects of Love Alternatives to AOP Events
  64. 64. Aspects of Love The Future? Composite Oriented Programming https://www.infoq.com/articles/Composite-Programming- Qi4j Aspect Oriented Programming Dependency Injection Domain-Driven Design
  65. 65. Aspects of Love The Future? Apache Polygene https://polygene.apache.org/ Behavior Depends on Context Decoupling is a Virtue Business Rules (and Domain Model) matters more Classes are Dead, Long Live Interfaces
  66. 66. Aspects of Love Are you ready to start exploring the magic of Wonderland?
  67. 67. Aspects of Love All Images are from the Alice Tarot deck published by Baba Studios © Karen Mahony & Baba Studios Used with permission

×