• Like
Zend Code in ZF 2.0
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Zend Code in ZF 2.0

  • 3,504 views
Published

This is a low-level, and philosophical discussion on the act of compiling data out of your PHP applications using Zend\Code: Scanning, Generating, Annotating code in PHP.

This is a low-level, and philosophical discussion on the act of compiling data out of your PHP applications using Zend\Code: Scanning, Generating, Annotating code in PHP.

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
3,504
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
29
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. October 2011Skynet in ZF 2.0Or, better put “what one can do with ZendCode”
  • 2. Who Am I? •Ralph Schindler (ralphschindler) Software Engineer on the Zend Framework team •At Zend for almost 4 years •Before that TippingPoint/3Com Programming PHP for 13+ years Live in New Orleans, LA. •Lived in Austin, Tx for 5 years2
  • 3. Why Skynet? •From wikipedia: “ ... an artificially intelligent system which became self-aware and revolted against its creators.” “The strategy behind Skynets creation was to remove the possibility of human error and slow reaction time to guarantee fast, efficient response to enemy attack.”3
  • 4. Why Skynet? •So, ... who is the enemy?4
  • 5. Why Skynet? •Other enemies (without photo’s) Unmaintainable applications Untestable applications Slow applications Un-fun / boring code Painful code5
  • 6. Why Skynet? •So, what does Skynet look like? •Anything we develop that looks too magical too-good-to-be-true too all-encompassing too flexible / too many input types requires too much documentation is outside our normal understanding of how things work6
  • 7. Why Skynet? •What does Skynet not look like? Simple Easy to understand Finite inputs / outputs Easy to get up and running with7
  • 8. Why Skynet •In a nutshell: PHP PHP has no development time compile phase On it’s own, it doesn’t know much •Hence, the many upon many frameworks All decisions are pushed into “request” time Single strategy for performance increase •caching •black-box optimizer8
  • 9. Why Skynet? •Basically, ZendCode is Skynet (figuratively) it can understand your code, sometimes better than the developer whom wrote it it can expose information about your code that the developer was unaware of it can generate code faster than you can9
  • 10. Why Skynet? •Ok, not really •ZendCode will not: see developers as a threat attempt to take over the world send the Terminator (a.k.a Matthew Weier O’Phinney) after you for writing bad, unmaintainable, code10
  • 11. Who is the Terminator?11
  • 12. What does ZendCode Do? •So, what does ZendCode do?12
  • 13. Let’s Recap the Humble Beginnings •ZF1 users wanted scaffolding & RAD •So, ... Zend_Tool •Zend_Tool_Project / Zend_Tool_Framework Zend_CodeGenerator •original intention was more than PHP Zend_Reflection •added docblock parsing functionality13
  • 14. ZF1 Problem Area •This setup suffered from 2 major problems: Runtime dependencies are not always loaded Same request changes to structures do not update in-memory structures14
  • 15. Problem 115
  • 16. Problem 2 •Workflow (in the same request): Require File Build ZendCodeGenerator object based off Zend Reflection object of a class •(For example a controller) Add method to class Write to disk Build ZendCodeGenerator object based off Zend Reflection object •FAIL! B/c file is already in memory16
  • 17. Problem 2 - Temporary Solution •Temporary solution was to cache generator objects that were built based one particular reflection objects, and return those when asked for a generator object seeded with a known reflection object •“We’ll fix this in ZF2”17
  • 18. Other Realizations •We were not going to get other generator types •Generators were closely related to Reflection and the new Scanner component •ZendCode was a better place for all of this18
  • 19. Static vs. Dynamic •Runtime Static - Fast Dynamic - Slow •Dev-time Static - slower workflow Dynamic - workflow resembles runtime19
  • 20. Static vs. Dynamic •Typical PHP workflow for dynamic applications: initiate request fulfill request return to consumer20
  • 21. Static vs. Dynamic •Typical PHP workflow for “static” applications with compilation built in (dev time): initiate request compile (during request, but omitted during production) fulfill request return to consumer21
  • 22. Static vs. Dynamic •Typical PHP workflow for “static” applications with compilation NOT built in (dev time): (change code) (compile code) initiate request fulfill request return to consumer22
  • 23. Static vs. Dynamic •You’ve probably seen static analysis in some way before: Caching •If you never intend on expiring the data, it’s not really a cache if (!file_exists(..)) { file_put_contents(..., var_export()) } Zend Optimizer / APC Content Platforms: •Drupal – development mode – non-caching mode – module initialization •Wordpress – installation hooks: plugin initialization23
  • 24. What are we compiling? •Anything dynamically looked up What the valid routes are What the valid application assets are •classes •view scripts Where particular classes are located More? •Which extensions are loaded •What the php environment can support24
  • 25. So, What Is Our Toolset? •ZendCode ZendCodeReflection ZendCodeGenerator ZendCodeScanner ZendCodeAnnotation25
  • 26. ZendCodeReflection •Originally part of ZF1 •Added features: file reflection docblock reflection26
  • 27. ZendCodeReflection27
  • 28. ZendCodeReflection cont’28
  • 29. ZendCodeGenerator •Originally ZF 1’s ZendCodeGenerator •Built OO Classes with an OO API Mainly consumed by Zend_Tool29
  • 30. ZendCode Generator30
  • 31. ZendCodeScanner •ZendCodeScanner Token based “reflection” Reflection for files in ZF1 has a similar approach Ascertain information in files: •what namespaces are inside this file •what use statements are inside a particular namespace •what classes are in this file’s namespaces •what files are required (none, right?) •is there a file level docblock31
  • 32. ZendCodeScanner Class Hierarchy32
  • 33. ZendCode Scanner (Sample)33
  • 34. ZendCodeAnnotation •Annotations Why? You asked for it. At least some of you did. There’s no native PHP support for it Similar to: •Java’s Annotation system •C#’s Attribute system •Doctrine’s Annotation system •with a PHP twist of course34
  • 35. ZendCodeAnnotation •Design: Do not force content into a particular DSL Build as a simple easy to use framework for easy consumption Prototype pattern Use ZendCodeScanner •Drawbacks Slow, very very slow35
  • 36. ZendCodeAnnotation •Single Interface to implement: ZendCodeAnnotationAnnotation36
  • 37. ZendCodeAnnotationManager •Simple Manager to Populate:37
  • 38. Demo 1: ZendCodeScanner •Find all dependencies in a directory full of code •Demo38
  • 39. Demo 2: Built a Micro-framework •Goals Find on github.com/ralphschindler/middlewarephp few classes - the “middleware” is editable fast (2ms request for hello world) compilation based •Routes •Classmap autoloader annotation based utilize include returns, closures, and arrays •Demo39
  • 40. Philisophical •Let’s get philosophical: Should we be “compiling” elements? •Is this what PHP is all about? •Rasmus says yes. Can we architect flexible and performant applications without some kind of static code analysis that affects runtime? How do we introduce these tasks into developer workflows where they make the most sense, don’t impede development?40
  • 41. Fin •Questions and/or comments, let’s go grab a beer discuss. •Remember ZF2 is only beta, if you have opinions on how we should go about applying these techniques, get involved NOW! •Thank you!41