Download - Create Clean Code with AOP
![Page 1: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/1.jpg)
International PHP Conference, Spring Edition 2010
Robert Lemke
Create Clean Code with Aspect-Oriented Programming
Photo: Wolfgang Staudt
![Page 2: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/2.jpg)
Robert Lemkechief architect of TYPO3 Phoenix and FLOW3
co-founder of the TYPO3 Association
34 years old
lives in Lübeck, Germany
1 wife, 1 daughter, 1 espresso machine
likes drumming
![Page 3: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/3.jpg)
= Full Stack Application Framework
![Page 4: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/4.jpg)
OOP Object-Oriented Programming
AOP Aspect-Oriented Programming
MVC Model View Controller
POPO Plain Old PHP Object
DI Dependency Injection DRY
YAGNI
CoC
TDD
LLATIP
![Page 5: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/5.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
/** * Creates a new post * * @param \F3\Blog\Domain\Model\Post $newPost A fresh Post object which has not yet been * added to the repository * @return void */public function createAction(\F3\Blog\Domain\Model\Post $newPost) {
if ($this->policyService->isGranted($this->currentUser, __CLASS__, __METHOD__) { $this->blog->addPost($newPost);
$this->flashMessageContainer->add('Your new post was created.'); $this->systemLogger->log('A new post was created.', LOG_INFO); $this->notificationService->notify('A new post was created.', '[email protected]'); } else { $this->systemLogger->log('Someone tried to create a post.', LOG_WARNING); throw new \F3\FLOW3\Security\Exception\AccessDeniedException('Tried to create.'); } $this->redirect('index');}
![Page 6: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/6.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
/** * Creates a new post * * @param \F3\Blog\Domain\Model\Post $newPost A fresh Post object which has not yet been * added to the repository * @return void */public function createAction(\F3\Blog\Domain\Model\Post $newPost) {
if ($this->authorizationService->isGranted($this->currentUser, __CLASS__, __METHOD__) { $this->blog->addPost($newPost);
$this->flashMessageContainer->add('Your new post was created.'); $this->systemLogger->log('A new post was created.', LOG_INFO); $this->notificationService->notify('A new post was created.', '[email protected]'); } else { $this->systemLogger->log('Someone tried to create a post.', LOG_WARNING); $this->flashMessageContainer->add('Access denied.'); } $this->redirect('index');}
![Page 7: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/7.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
AOP
![Page 8: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/8.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
Aspect-Oriented Programmingprogramming paradigm
separates concerns to improve modularization
OOP modularizes concerns into objects
AOP modularizes cross-cutting concerns into aspects
![Page 9: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/9.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
/** * Creates a new post * * @param \F3\Blog\Domain\Model\Post $newPost A fresh Post object which has not yet been * added to the repository * @return void */public function createAction(\F3\Blog\Domain\Model\Post $newPost) { $this->blog->addPost($newPost); $this->flashMessageContainer->add('Your new post was created.'); $this->redirect('index');}
![Page 10: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/10.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
Concerns?
![Page 11: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/11.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
ConcernsSeparation of Concerns
group features and behavior into manageable parts
have a specific purpose and business to take care of
Cross-Cutting Concerns
are the party poopers who want to have a say in everything
![Page 12: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/12.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
Cross-Cutting ConcernsLogging
Security
Persistence
Global Business Logic
Dirty Hacks
![Page 13: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/13.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
We don't want infrastructure codein our models.
![Page 14: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/14.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
We want to unit-test even cross-cutting concerns
![Page 15: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/15.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
We want to centralize security-related code
![Page 16: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/16.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
AOP Lingo
![Page 17: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/17.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
AspectPart of the application where cross-cutting concerns are implemented
In FLOW3 aspects are classes annotated with @aspect
![Page 18: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/18.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
Join PointIs a single point in the call graph
Method Execution
Exception
Represents an event, not a location
![Page 19: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/19.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
PointcutA set of join points where advices could be executed
can be composed
can be named
![Page 20: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/20.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
AdviceAction to take at a join points defined by the point cut
![Page 21: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/21.jpg)
Inspiring people toshareHitchhiker's Guide to FLOW3
DEMO
![Page 22: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/22.jpg)
Inspiring people toshareHitchhiker's Guide to FLOW3
Kinds of AdviceAdvice types supported by FLOW3:
@before@afterreturning@afterthrowing@after@around
![Page 23: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/23.jpg)
Inspiring people toshareHitchhiker's Guide to FLOW3
Pointcut Designatorsmethod(F3\MyPackage\MyClass->myMethod())class(F3\MyPackage\MyClass)within(F3\MyPackage\MyInterface)classTaggedWith(someTag)methodTaggedWith(anotherTag)setting(Demo.Stuff.SomeSetting = "yeah, do it")ffiilter(F3\MyPackage\MyCustomFilterImplementation)
![Page 24: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/24.jpg)
Inspiring people toshareHitchhiker's Guide to FLOW3
Runtime Evaluationsevaluate(coffee.kind == "Arabica")
![Page 25: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/25.jpg)
Inspiring people toshareHitchhiker's Guide to FLOW3
Compound Pointcuts
! /**! * @around method(.*Controller->(new|create|edit|update|delete)Action()) && ⏎ !methodTaggedWith(anybodyMayAccessThis)! */! public function interceptMethodCalls($joinPoint) {
...}
![Page 26: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/26.jpg)
Inspiring people toshareHitchhiker's Guide to FLOW3
DEMO
![Page 27: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/27.jpg)
Inspiring people toshareHitchhiker's Guide to FLOW3
FLOW3's AOP implementationbased on proxy classes
unlike with most pre-processors line numbers stay the same
no scaffolding
0 Lines of generated code which need to be maintained by you
fast (on the second hit)
![Page 28: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/28.jpg)
Inspiring people toshareHitchhiker's Guide to FLOW3
![Page 29: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/29.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
Progress
FLOW3 1.0.0
![Page 30: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/30.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
Further ReadingFLOW3 Websitehttp://flow3.typo3.org
TYPO3 Forgehttp://forge.typo3.org
Further Readinghttp://flow3.typo3.org/about/principles/further-reading
http://bit.ly/GoodAOPArticle
![Page 31: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/31.jpg)
Create Clean Code with AOP IPC SE 2010, Berlin
Questions
Email: [email protected]: http://robertlemke.de/blogTwitter: @t3rob
Feedback: http://joind.in/1743
![Page 32: Create Clean Code with AOP](https://reader037.vdocument.in/reader037/viewer/2022100309/555a8d7dd8b42abb628b533f/html5/thumbnails/32.jpg)