solving cross-cutting concerns in php - dutchphp conference 2016

122
Solving Cross-Cutting Concerns in PHP Alexander Lisachenko

Upload: alexander-lisachenko

Post on 16-Apr-2017

1.616 views

Category:

Presentations & Public Speaking


0 download

TRANSCRIPT

Page 1: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Solving Cross-Cutting Concerns in PHPAlexander Lisachenko

Page 2: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

About me:

2

lisachenkolisachenko

Page 3: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

‣ Head of Software Architecture at Alpari (RU) Forex Broker

About me:

2

lisachenkolisachenko

Page 4: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

‣ Head of Software Architecture at Alpari (RU) Forex Broker

‣ Have worked with computers since 7 years old

About me:

2

lisachenkolisachenko

Page 5: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

‣ Head of Software Architecture at Alpari (RU) Forex Broker

‣ Have worked with computers since 7 years old

‣ Clean code advocate, guru in enterprise architecture

About me:

2

lisachenkolisachenko

Page 6: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

‣ Head of Software Architecture at Alpari (RU) Forex Broker

‣ Have worked with computers since 7 years old

‣ Clean code advocate, guru in enterprise architecture

‣ Author of the aspect-oriented framework Go! AOP http://go.aopphp.com

About me:

2

lisachenkolisachenko

Page 7: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Agenda

3

Page 8: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Agenda‣ Advantages of Object-Oriented Paradigm

3

Page 9: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Agenda‣ Advantages of Object-Oriented Paradigm‣ Limitations of OOP and how they affect our

application

3

Page 10: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Agenda‣ Advantages of Object-Oriented Paradigm‣ Limitations of OOP and how they affect our

application‣ Existing object-oriented ways of solving

cross-cutting concerns

3

Page 11: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Agenda‣ Advantages of Object-Oriented Paradigm‣ Limitations of OOP and how they affect our

application‣ Existing object-oriented ways of solving

cross-cutting concerns‣ Pros and cons: the aspect-oriented

approach of solving cross-cutting concerns

3

Page 12: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Agenda‣ Advantages of Object-Oriented Paradigm‣ Limitations of OOP and how they affect our

application‣ Existing object-oriented ways of solving

cross-cutting concerns‣ Pros and cons: the aspect-oriented

approach of solving cross-cutting concerns‣ Examples of using aspects in real life

applications3

Page 13: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Текст

Object-Oriented ParadigmSolution to the complexity

Page 14: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

5

ModularityWe connect classes, components, bundles, and frameworks.

Page 15: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

6

ReusabilityWe use existing code to create new code on the top.

Page 16: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

7

EncapsulationWe hide implementation details from the outside world.

Page 17: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

8

Is it a silver bullet?Object-Oriented Paradigm

Page 18: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

8

Is it a silver bullet?Object-Oriented Paradigm

Page 19: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

8

Is it a silver bullet?Object-Oriented Paradigm

Page 20: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Edsger W. Dijkstra

“Object-oriented programming is an exceptionally bad idea which could only have originated in California.”

9

Page 21: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Joe Armstrong

“…You wanted a banana but what you got [with OOP] was a gorilla holding the banana

and the entire jungle”

10

Page 22: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

What are the problems?

11

Page 23: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

What are the problems?‣ Limitation of object-oriented

representation of real life processes

11

Page 24: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

What are the problems?‣ Limitation of object-oriented

representation of real life processes‣ Essential complexity results in strong

coupling between components

11

Page 25: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

What are the problems?‣ Limitation of object-oriented

representation of real life processes‣ Essential complexity results in strong

coupling between components‣ Essential complexity results in scattered

implementation of our concerns

11

Page 26: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Текст

Cross-cutting concernsWhat they are and why we should think about them

Page 27: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

13

Cross-cutting concerns are aspects of a program that affect

other concerns.

Page 28: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Cross-cutting concerns - why they affect our code?

14

Task: Implement an audit system that checks access permission for each public method over

all classes in our system and then logs this information into the security journal.

Page 29: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Clean model

15

Page 30: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Authorization control…

16

Page 31: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Authorization control…

16

Page 32: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Logging and audit…

17

Page 33: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Logging and audit…

17

Page 34: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Logging and audit…

17

Page 35: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Error handling…

18

Page 36: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Error handling…

18

Page 37: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Code tangling

19

Page 38: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Code scattering

20

Page 39: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Текст

OOP waysWhat do we have in OOP to fight cross-cutting concerns?

Page 40: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

DecoratorDesign pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.

22

Page 41: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

23

Page 42: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

23

Page 43: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

23

Page 44: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

23

Page 45: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Decorator+ Respects LSP - We can pass an instance of a

decorator everywhere the original object is expected. + Keeps SRP for the original class because it doesn’t

contain secondary concerns now

- Too many decorator classes for each combination of concern+concrete interface

- Secondary concerns (authorization, caching, etc.) are scattered across all decorators

- Additional overhead for calling original methods

24

Page 46: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

MediatorDefines an object that incapsulates all logic of interactions between objects, giving them the ability to work without explicit references to each other

25

Page 47: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

26

Page 48: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

26

Page 49: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

26

Page 50: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

27

Page 51: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

27

Page 52: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

27

Page 53: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Mediator+ All secondary concerns can be moved to separate

classes without duplication (SRP for secondary concerns)

+ Keeps SRP for the original class, because it doesn’t contain secondary concerns now

+ The most flexible way of extending a system- Requires specific changes in the original class to

perform notification of mediator - Hard to debug code with complex logic of interaction

because there are no explicit links between objects

28

Page 54: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Текст

Take control of everythingWhat if we combine all the best features of the decorator and mediator into the one pattern?

Page 55: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Aspect-Oriented ParadigmA programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns

30

Page 56: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Go! AOP Framework

31

Page 57: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Go! AOP Framework

31

‣ Inspired by famous AspectJ and Spring frameworks

Page 58: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Go! AOP Framework

31

‣ Inspired by famous AspectJ and Spring frameworks

‣ Over 800 stargazers on Github - thank you!

Page 59: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Go! AOP Framework

31

‣ Inspired by famous AspectJ and Spring frameworks

‣ Over 800 stargazers on Github - thank you!‣ Over 200k installations on Packagist

Page 60: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Go! AOP Framework

31

‣ Inspired by famous AspectJ and Spring frameworks

‣ Over 800 stargazers on Github - thank you!‣ Over 200k installations on Packagist‣ PHP5.6, PHP7.0 and Opcache compatible

Page 61: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Go! AOP Framework

31

‣ Inspired by famous AspectJ and Spring frameworks

‣ Over 800 stargazers on Github - thank you!‣ Over 200k installations on Packagist‣ PHP5.6, PHP7.0 and Opcache compatible‣ There are bridges for SF2, ZF2, Laravel…

Page 62: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Let’s take our clean code…

32

Page 63: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Let’s take our clean code…

32

Page 64: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Let’s take our clean code…

Method execution is an event

32

Page 65: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

…and subscribe to our event

33

Page 66: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

…and subscribe to our event

33

Page 67: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

…and subscribe to our event

Pointcut - describes list of interesting events

33

Page 68: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

…and subscribe to our event

Pointcut - describes list of interesting events

33

Page 69: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

…and subscribe to our event

Pointcut - describes list of interesting events

Joinpoint - defines an event object

33

Page 70: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

…and subscribe to our event

Pointcut - describes list of interesting events

Joinpoint - defines an event object

33

Page 71: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

…and subscribe to our event

Pointcut - describes list of interesting events

Joinpoint - defines an event object

Advice - event handler

33

Page 72: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

How it works

34

All aspects (or advisors) are registered in the aspect kernel

Page 73: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

How it works

35

The special php://filter stream filter is registered via stream_filter_register()

Page 74: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

How it works

36

The composer class loader is replaced with a weaving proxy

Page 75: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

How it works

37

Lexical analysis and parsing of AST is performed (nikic/PHP-Parser)

Page 76: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

How it works

38

Static reflection is created from the AST (goaop/parser-reflection)

Page 77: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

How it works

39

The original class is renamed and replaced with a new class with additional behavior; stored in the

cache

Page 78: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

How it works

39

The original class is renamed and replaced with a new class with additional behavior; stored in the

cache

Page 79: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

How it works

39

The original class is renamed and replaced with a new class with additional behavior; stored in the

cacheSimple inheritance

Page 80: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

How it works

39

The original class is renamed and replaced with a new class with additional behavior; stored in the

cacheSimple inheritance

Page 81: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

How it works

39

The original class is renamed and replaced with a new class with additional behavior; stored in the

cacheSimple inheritance

Overridden method

Page 82: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Текст

Go! AOP joinpointsWhat can be intercepted?

Page 83: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Method interceptors:

41

Page 84: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Method interceptors:

41

Can be a final class or a trait!

Page 85: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Property interceptors:

42

Page 86: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Logic interceptors:

43

Page 87: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

AOP+ Keeps SRP for the original class because it doesn’t

contain secondary concerns now + All secondary concerns are stored in separate classes

without duplication (SRP for secondary concerns) + Very flexible way of extending our system (like mediator

does) + Does not require changes in original classes

- No tools to debug AOP code; no help from IDE - Overhead for AOP initialization and execution

44

Page 88: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Go! AOP: Debugging with XDebug

45

Page 89: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Go! AOP: Debugging with XDebug

45

Page 90: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Go! AOP: Plugin for the PhpStorm

46

Page 91: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Pointcut syntax highlighting, completion and analysis

47

Page 92: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Pointcut syntax highlighting, completion and analysis

47

Page 93: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Navigate to advice/advised elements

48

Page 94: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Navigate to advice/advised elements

48

Page 95: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Текст

Circuit breaker Prevents one failure from reoccurring continuously

Page 96: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Logic of CircuitBreaker

50

Page 97: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Logic of CircuitBreaker

50

‣ For each public method execution we count the number of failures in APC/Memcache.

Page 98: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Logic of CircuitBreaker

50

‣ For each public method execution we count the number of failures in APC/Memcache.

‣ If the number of failures is too high, then we break the circuit to prevent subsequent failures.

Page 99: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Logic of CircuitBreaker

50

‣ For each public method execution we count the number of failures in APC/Memcache.

‣ If the number of failures is too high, then we break the circuit to prevent subsequent failures.

‣ If the circuit is broken, then the original method body should not be executed for the configured amount of time.

Page 100: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Logic of CircuitBreaker

50

‣ For each public method execution we count the number of failures in APC/Memcache.

‣ If the number of failures is too high, then we break the circuit to prevent subsequent failures.

‣ If the circuit is broken, then the original method body should not be executed for the configured amount of time.

‣ https://github.com/ejsmont-artur/php-circuit-breaker can be used.

Page 101: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Step 1. Define an annotation

51

Page 102: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Step 2. Define an aspect

52

Page 103: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Step 2. Define an aspect

53

Page 104: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Step 2. Define an aspect

53

All methods with «Fuse» annotation

Page 105: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Step 2. Define an aspect

53

All methods with «Fuse» annotation

Page 106: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Step 2. Define an aspect

53

All methods with «Fuse» annotation

Page 107: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Step 2. Define an aspect

53

All methods with «Fuse» annotation

Page 108: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Step 3. Use it in your code

54

Page 109: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Step 3. Use it in your code

54

Page 110: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

More examples for AOP

55

Page 111: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

More examples for AOP

55

‣ Profiling methods with pinba extension and Intaro Pinboard

Page 112: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

More examples for AOP

55

‣ Profiling methods with pinba extension and Intaro Pinboard

‣ Feature toggle pattern

Page 113: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

More examples for AOP

55

‣ Profiling methods with pinba extension and Intaro Pinboard

‣ Feature toggle pattern‣ Authorization control

Page 114: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

More examples for AOP

55

‣ Profiling methods with pinba extension and Intaro Pinboard

‣ Feature toggle pattern‣ Authorization control‣ Traditional caching, logging, transaction

control, security audit

Page 115: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

More examples for AOP

55

‣ Profiling methods with pinba extension and Intaro Pinboard

‣ Feature toggle pattern‣ Authorization control‣ Traditional caching, logging, transaction

control, security audit‣ Design by contract programming (Php-Deal

library)

Page 116: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

More examples for AOP

55

‣ Profiling methods with pinba extension and Intaro Pinboard

‣ Feature toggle pattern‣ Authorization control‣ Traditional caching, logging, transaction

control, security audit‣ Design by contract programming (Php-Deal

library)‣ Aspect-oriented testing (AspectMock)

Page 117: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Conclusion

56

Page 118: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Conclusion

56

‣ OOP is a nice tool for solving accidental complexity but not for essential complexity.

Page 119: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Conclusion

56

‣ OOP is a nice tool for solving accidental complexity but not for essential complexity.

‣ Pay attention to code scattering and code tangling.

Page 120: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Conclusion

56

‣ OOP is a nice tool for solving accidental complexity but not for essential complexity.

‣ Pay attention to code scattering and code tangling.

‣ Try to extract secondary concerns into separate classes via OOP decomposition for small applications.

Page 121: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Conclusion

56

‣ OOP is a nice tool for solving accidental complexity but not for essential complexity.

‣ Pay attention to code scattering and code tangling.

‣ Try to extract secondary concerns into separate classes via OOP decomposition for small applications.

‣ Try to use AOP for complex enterprise applications.

Page 122: Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

Thank you for the attention!

https://github.com/goaop https://github.com/lisachenko https://twitter.com/lisachenko