$30 off During Our Annual Pro Sale. View Details »

Phalcon PHP Framework and Phalcon Kernel Introduction - phpconftw2012

Phalcon PHP Framework and Phalcon Kernel Introduction - phpconftw2012

Phalcon PHP Framework and Phalcon Kernel Introduction

Rack Lin (阿土伯)

November 03, 2012
Tweet

More Decks by Rack Lin (阿土伯)

Other Decks in Programming

Transcript

  1. Phalcon  PHP  Framework  
    The  Fastest  Faster  PHP  Framework
    阿土伯

    View Slide

  2. about:me
    !   目前為 ViViPOS  Co.,  Ltd  技術總監  
    !   利用 javascript  寫 〞收銀機〞(傳統產業,全新感受)  
    ! CoCo  都可、50嵐、歇腳亭、Yamaha  、星聚點  KTV  
     
    !   #3  commiHer  of  cphalcon  repository  
    !   PHP  /  Javascript  /  Java  Programmer  (目前移情於 Scala  )  
    ! [email protected]  
    ! hHps://twiHer.com/racklin  
    ! hHps://www.facebook.com/racklin1002  
    ! hHp://racklin.blogspot.tw/      (生小孩後就變癈墟)  

    View Slide

  3. 議題
    ! PhalconPHP  Framework 介紹  
    ! PhalconPHP  Kernel 介紹  
    !   DEMO  c9s  ValidaWonKit  porWng  as  php  extension  

    View Slide

  4. Programmers  共同的罩門  
    !  速度最快!  
    !  使用資源最小!  
    !  跨平台!  
     

    View Slide

  5. Programmers  共同的罩門  
    !  速度最快!  
    !  使用資源最小!  
    !  跨平台!  
     
      所以大家來聽 PhalconPHP  

    View Slide

  6. 為什麼快
    !   Fibonacci  40  (費式數列)  hHp://bit.ly/Pk8HkJ  
    Language Times
    C  with  –O2 0m0.202s
    java 0m1.305s
    scala 0m1.808s
    nodejs 0m2.872s
    Ruby  (1.9.x) 0m37.404s
    Php  (5.4) 1m17.728s
    Python  (2.7.2) 1m17.979s
    Ruby  (1.8.x) 3m35.135s
    Php  (5.3) 6m17.192s

    View Slide

  7. Fibonacci  40
    0  
    100  
    200  
    300  
    400  
    500  
    c  with  -­‐
    O2  
    java   scala   nodejs   ruby  
    1.9.2  
    php  5.4   ruby  
    1.8.x  
    php  5.3  

    View Slide

  8. 所以結論…
     
    下一個專案要用 c  /  c++  來寫  web    

    View Slide

  9. Raphters
    !   A  web  framework  for  C

    View Slide

  10. WOW!  COOOOOOL~
    看起來好像很容易!

    View Slide

  11. 除了慣C哥
     
     
    大部份人打完招呼不知道要做什麼…..

    View Slide

  12. PHP  沒救了?

    View Slide

  13. Fibonacci  as  php  extension

    View Slide

  14. Fibonacci  as  php  extension
    0m0.79s        馬上變成第二名

    View Slide

  15. Fibonacci  圖表
    0  
    50  
    100  
    150  
    200  
    250  
    300  
    350  
    400  
    450  
    c  with  -­‐O2  java   scala   nodejs  
    ruby  1.9.2  
    php  5.4  
    ruby  1.8.x  
    php  5.3  php  ext  

    View Slide

  16. 速度只要出問題,  C  都能搞定

    View Slide

  17. 漂亮的平衡  

    A  PHP  web  framework    
    delivered  as  a  C  extension

    View Slide

  18. PHP  extension  frameworks
     
    PhalconPHP  
    Yaf

    View Slide

  19. 為什麼不是 Yaf
    !   Underscore  Class  Name  (  目標 PHP  5.2 和以上版本)  
    !   Document 實在不多  
    !   Sample  Codes    更少  
     

    View Slide

  20. 為什麼不是 Yaf
    !   Underscore  Class  Name  ( 目標 PHP  5.2 和以上版本)  
    !   Document 實在不多  
    !   Sample  Codes    更少  
     
    !   Yet  Another  Framework  (Yaf)  
     
    這專案名稱我不喜歡  
    很怕將來會出一個  What  The  Framework  (WTx)  

    View Slide

  21. Frameworks  benchmark

    View Slide

  22. Frameworks  memory  usage

    View Slide

  23. PhalconPHP  benefits
    !   PHP  Extension  –  Faster  
    ! Phalcon  all  Components  are  loosely  coupled  
    !   Low-­‐Level  OpWmizaWons  
    !   Using  c-­‐language  ORM  for  PHP  
    !   PHQL  –  lemon  parser  (SQLite)  
    !   Well-­‐documented  

    View Slide

  24. PhalconPHP  0.5  news
    !   Namespaces  
    !   Class  Loader  –  相容於 PSR-­‐0  
    !   Dependency  Injec8on  
    !   Events  Manager  
    !   PDO  Database  Adapters  
    !   *  支援 SQLite.      
    !   *  PostgreSQL  支援 Mul8-­‐Schemas  
    !   *  CLI  Applica8ons  
    !   Micro  Applica8ons  
    !   Full  stack  frameworks  

    View Slide

  25. PhalconPHP  0.6
    !   Object-­‐Document  Mapper  -­‐  MongoDB  
    !   Volt:  Template  Engine    -­‐  inspired  on  Twig  
    !   PS.  11/2  Released  

    View Slide

  26. Class  Loader  
    Dependency  InjecWon  
    Events  Manager
     
     
     
    Flexible  
    Extensible  
    Plugin-­‐able    

    View Slide

  27. Class  Loader
    !   盡可能不用使用 include  /  require  .  
    ! registerNamespaces  –  PSR-­‐0  相容  
    ! registerPrefixes  
    ! registerClasses  
    ! registerDirectories  

    View Slide

  28. Using  Class  Loader

    View Slide

  29. Composer
    •  類似 PEAR  的套件管理工具,但更方便! 像是 npm  /  brew  。  
    •  Install  Composer  
    •  IniWal  project  
    •  Require  /  Install  libraries  
    •  Update  libraries  
    $  curl  -­‐s  hHps://getcomposer.org/installer  |  php
    $  composer.phar  init
    $  composer.phar  require  [vendor/name]
    $  composer.phar  update

    View Slide

  30. Composer  Autoloading
    •  只要加一行在你的程式中  
    require  'vendor/autoload.php';
    hHps://packagist.org/

    View Slide

  31. Phalcon  Class  Loader  +    
    Composer  Autloading
    ! Phalcon  and  Composer  都是使用 spl_autoload_register  ,  
    spl_autoload_register 相當聰明,載入類別後會中斷執行,
    不會重覆載入  
    !   註冊 多個 autoload  handlers  是安全的  
     
     
     
    !   所以只要在 Phalcon  Class  Loader  前先加上一行即可  
     

    require  'vendor/autoload.php’;  

    View Slide

  32. Dependency  InjecWon
    !   \Phalcon\DI  實作了 IoC  設計模式,幫助寫出可擴充性、易
    於測試、維護的程式碼。  
    ! PhalconPHP  內部大量利用 DI  將各元件串接起來。  
     

    View Slide

  33. Dependency  InjecWon
    !   產生新的物件或單例(Singleton) - get  /  getShared  
    !   傳參數給  Constructor  
    !   Lazy  loading  
    !   物件只有在被取得時才會產生  
    !   Magic    
    !   如果沒有註冊,會自動試著找同樣的 Class  Name  
    !   如果可以,全部利用 DI  來產生您的類別實例。

    View Slide

  34. DI  範例  (set)

    View Slide

  35. DI  範例  (get)
    !   取得物件  
    $request  =  $di-­‐>get("request");  
    !   透過 magic  method  
    $request  =  $di-­‐>getRequest();  
    !   Singleton  
    $request  =  $di-­‐>getShared("request");  
    !   傳參數給 constructor  
    $component  =  $di-­‐>get("MyComponent",  array("some-­‐
    parameter",  "other"))

    View Slide

  36. Events  Manager
    !   Web  GUI  程式中,我們大量使用 Events 。  
    !   DOM  Events  
    !   Events  Manager  -­‐  fire  event  ,  aHach  listener  
    !   Event  Type  Using  colon  :  as  namespace  separator.    

    View Slide

  37. DEMO
    !   Using  Class  Loader    /  Dependency  InjecWon  /  Events  Manager  
    for  plugin-­‐able  applicaWons.  
    !   Linux  Upstart  init  daemon  like  demo  
    hHp://upstart.ubuntu.com/    (replacement  for  the  venerable  
    System-­‐V  init)  
    !   Shopping-­‐Cart  Plugin-­‐able  PromoWons  .  

    View Slide

  38. Micro  Framework
    !   Simple  and  ResTul  
    !   Frameworks  
    ! RubyàSinatra,  Cuba
    ! PythonàFlask,  Pyramid  
    !   PHP  àSilex,  Slim  
    !   Java  àNapalm  

    View Slide

  39. Micro  Example
    Not  Found  Handler

    View Slide

  40. Full  Stack  Framework
    !   MVC  Architecture  
    !   Router  /  Dispatcher  
    !   View  Helper  /  Template  Engine  
    !   Filtering  Data  
    !   Session    
    !   Cache  
    !   ACL  
    !   Logging  
    !   I18n/l10n  

    View Slide

  41. Models
    CreaWng  Models
    By  default  model  “Robots”  will  refer  to  the  table  “robots”.

    View Slide

  42. Models
    Finding  Records

    View Slide

  43. Models
    Save  changes

    View Slide

  44. Models
    Finding  with  condiWons  ,  order  and  Limit

    View Slide

  45. Models
    !   RelaWons  

    View Slide

  46. Models
    !   RelaWons  

    View Slide

  47. Models
    !   RelaWons  

    View Slide

  48. Models  -­‐  Criteria
    !   Similar  To    
    !   Doctrine  QueryBuilder  
    ! Yii  Query  Object  
    !   Provides  a  fluent  API.  

    View Slide

  49. PHQL
    !   Similar  To    
    !   Doctrine  DQL  
    !   Hibernate  HQL  
    ! Phalcon  Query  Language  is  a  high  level,  object  oriented  SQL  
    dialect.  
    !   High  performance,  Phalcon  provides  a  parser  that  uses  the  
    same  technology  as  SQLite.  (lemon  parser)  
    hHp://www.sqlite.org/src/doc/trunk/doc/lemon.html  

    View Slide

  50. PHQL
    Select  Records
    Select  Records  with  namespaces

    View Slide

  51. Controller

    View Slide

  52. View  Helpers
    !   WriWng  and  maintaining  HTML  markup  can  quickly
    Document  type
    Produced

    View Slide

  53. Template  Engine  -­‐  Volt
    !   Volt  is  an  ultra-­‐fast  and  designer  friendly  templaWng  language  
    wriHen  in  C  ,  inspired  on  Twig.

    View Slide

  54. Console  ApplicaWon
    !   CLI  Components  
    ! cronjobs  
    !   Daemon  ApplicaWon  
    !   Long-­‐life  applicaWon  ,  using  supervisor  and  kill  yourself.  
    !   Job  Workers  
    !   Using  supervisor  and  kill  yourself.  
     

    View Slide

  55. CLI  Launcher

    View Slide

  56. CLI  MainTask
    Php  console.php    //  MainTask::mainAcWon  
    Php  console.php  main  test  //  MainTask::testAcWon  

    View Slide

  57. CLI  DEMO

    View Slide

  58. PhalconPHP  Kernel
    ! hHps://github.com/racklin/phalcon_kernel  
    !   由 cphalcon  中抽離出來的專案。  
    !   提供大量基於 Zend  API  的巨集/工具。  
    !   簡化 php  extension  開發。  

    View Slide

  59. Why  Extension
    !   Driver  /  Adapter  
    !   Low-­‐level  NaWve  Code  Libraries  
    !   Performance  
    !   EncapsulaWon  /  InformaWon  Hidden  
    !   For  Developer  
    !   For  Customers    

    View Slide

  60. zval

    View Slide

  61. c:  zval  *a      
    相當於  
    php:  $a

    View Slide

  62. Data  Types
    Long Boolean Double String  value
    ZVAL_LONG() ZVAL_BOOL() ZVAL_DOUBLE() ZVAL_STRING()
    NULL
    ZVAL_NULL()
    Create  macro

    View Slide

  63. Data  Types
    Long Boolean Double String  value String  Length
    Z_LVAL() Z_BVAL() Z_DVAL() Z_STRVAL() Z_STRLEN()
    Z_LVAL_P() Z_BVAL_P() Z_DVAL_P() Z_STRVAL_P() Z_STRLEN_P()
    HashTable Object
    Z_ARRVAL() Z_OBJVAL()
    Z_ARRVAL_P() Z_OBJVAL_P()
    GeHer  and  IdenWfy  macro
    Iden8fy   BOOL Long Double String NULL
    Z_TYPE()
    IS_BOOL IS_LONG IS_DOUBLE IS_STRING IS_NULL
    Z_TYPE_P()

    View Slide

  64. Return  Types
    Macro Type
    RETURN_FALSE  /  RETURN_TRUE Boolean
    RETURN_NULL() NULL
    RETURN_LONG() Long
    RETURN_DOUBLE() Double
    RETURN_STRING() String
    RETURN_CTOR()  
    RETURN_CCTOR()  
    RETURN_NCTOR()
    ZVAL  (Phalcon)

    View Slide

  65. Memory  Management
    ! Phalcon  Memory  Management  簡化了 Zend  Memory  
    Managemant.  
    !   PHALCON_MM_GROW()  
    建立一個 phalcon  memory  entry  結構  
    !   PHALCON_INIT_VAR  /  PHALCON_ALLOC_ZVAL_MM    
    建立並配置記憶體給 zval   並將其設為 null,  且將其指標的指
    標存至 phalcon  memory  entry.  
    !   PHALCON_MM_RESTORE  
    釋放所有經由 PHALCON_INIT_VAR  配置且 refcount  =1  的記
    憶體

    View Slide

  66. Phalcon  MM  Bad  Parts
    !   PHALCON_ALLOC_ZVAL_MM  or  PHALCON_INIT_VAR  
    !   如果你 trace  source  code  二者差別只在於一個是先將 zval  
    pointer  pointer  放入 memory  entry  再設定初始值。一個是先設
    定初始值再將 zval  pointer  pointer  放入 memory  entry  ….    
    !   PHALCON_INIT_VAR  3488  lines  
    !   PHALCON_ALLOC_ZVAL_MM      468  lines  
     

    View Slide

  67. MM  Sample

    View Slide

  68. String
    !   ZVAL_STRING()  –  Zend  API  
     
       
    ! concat_funcWon  –  Zend  API  
    ! Concat  macro  
    PHALCON_INIT_VAR(month);    
    ZVAL_STRING(month,  "2012",  1);  
    PHALCON_INIT_VAR(today_is);    
    ZVAL_STRING(today_is,  "Today  is",  1);
    PHALCON_INIT_VAR(first_part);  
    concat_funcWon(first_part,  today_is,  month);
    PHALCON_INIT_VAR(today);    
    PHALCON_CONCAT_SVSV(today,  "Today  is",  month,  "  ",  day);  

    View Slide

  69. Concat  Macros
    #define  PHALCON_CONCAT_SV(result,  op1,  op2)  \  
    #define  PHALCON_CONCAT_SVS(result,  op1,  op2,  op3)  \  
    #define  PHALCON_CONCAT_SVSV(result,  op1,  op2,  op3,  op4)  \  
    #define  PHALCON_CONCAT_SVSVS(result,  op1,  op2,  op3,  op4,  op5)  \  
    #define  PHALCON_CONCAT_SVSVSV(result,  op1,  op2,  op3,  op4,  op5,  op6)  \  
    #define  PHALCON_CONCAT_SVSVSVS(result,  op1,  op2,  op3,  op4,  op5,  op6,  op7)  \  
    #define  PHALCON_CONCAT_SVSVV(result,  op1,  op2,  op3,  op4,  op5)  \  
    #define  PHALCON_CONCAT_SVV(result,  op1,  op2,  op3)  \  
    #define  PHALCON_CONCAT_VS(result,  op1,  op2)  \  
    #define  PHALCON_CONCAT_VSV(result,  op1,  op2,  op3)  \  
    #define  PHALCON_CONCAT_VSVS(result,  op1,  op2,  op3,  op4)  \  
    #define  PHALCON_CONCAT_VSVSVS(result,  op1,  op2,  op3,  op4,  op5,  op6)  \  
    #define  PHALCON_CONCAT_VSVSVSV(result,  op1,  op2,  op3,  op4,  op5,  op6,  op7)  \  
    #define  PHALCON_CONCAT_VV(result,  op1,  op2)  \  
    #define  PHALCON_CONCAT_VVSV(result,  op1,  op2,  op3,  op4)  \  
    #define  PHALCON_CONCAT_VVV(result,  op1,  op2,  op3)  \  
    #define  PHALCON_CONCAT_VVVVV(result,  op1,  op2,  op3,  op4,  op5)  \

    View Slide

  70. Arrays
    !    array_init(zval*)  –  iniWal  as  array  
    !   Add  to  array  
    !    add_next_index_long    /  add_next_index_double  
    !    add_next_index_bool    /  add_next_index_stringl  
    !   Get  (index  type)  (*string  length)  
    ! phalcon_array_fetch_long  /  phalcon_array_fetch_string  
    !   Update  (index  type)  (*string  length)  
    ! phalcon_array_update_long  /  phalcon_array_update_string  
    ! Isset  /  unset    (*string  size)  
    !    phalcon_array_isset_long  /  phalcon_array_isset_string  
    !    phalcon_array_unset  /  phalcon_array_unset_string  

    View Slide

  71. Phalcon  Arrays  Bad  Parts
    !   PHP  String  is  Binary-­‐Safe  
    !   String  Length  =  Actually  String  Length  
    !   String  Size  =  String  Length  +  1  bytes(\0)  
    !   Arrays  fetch  and  update  using  String  Length  
    !   Arrays  isset  and  unset  using  String  Size  
    ! Phalcon_array_unset  not  phalcon_array_unset_long

    View Slide

  72. Call  FuncWons
    !   PHALCON_CALL_FUNC    
    呼叫 PHP  funcWon  ,有回傳值  
    !   PHALCON_CALL_FUNC_NORETURN  
    呼叫 PHP  funcWon  ,沒有回傳值
    !   PHALCON_CALL_FUNC_PARAMS_1    
    呼叫 PHP  funcWon 並傳一個參數,有回傳值  
    !   PHALCON_CALL_FUNC_PARAMS_1_NORETURN  
    呼叫 PHP  funcWon 並傳一個參數,沒有回傳值  
    !   目前巨集定義到傳 3 個參數  

    View Slide

  73. Call  FuncWons
    PHP  Code

    View Slide

  74. Call  FuncWons

    View Slide

  75. Call  Methods
    !   PHALCON_CALL_METHOD  
    呼叫 PHP  funcWon ,有回傳值  
    !   PHALCON_CALL_METHOD_NORETURN  
    呼叫 PHP  funcWon ,沒有回傳值
    !   PHALCON_CALL_METHOD_PARAMS_1    
    呼叫 PHP  funcWon 並傳一個參數,有回傳值  
    !   PHALCON_CALL_METHOD_PARAMS_1_NORETURN  
    呼叫 PHP  funcWon 並傳一個參數,沒有回傳值  
    !   目前巨集定義到傳 5  個參數  

    View Slide

  76. Call  Methods

    View Slide

  77. Object  ProperWes
    !   Update  Property  
    !    phalcon_update_property_long  
    !    phalcon_update_property_bool  
    !    phalcon_update_property_null  
    !    phalcon_update_property_zval  /  
    phalcon_update_property_zval_zval  
    !   Read  Property  
    !    phalcon_read_property  /  phalcon_read_property_zval  
    ! Isset  Property  
    !    phalcon_isset_property  /  phalcon_isset_property_zval  

    View Slide

  78. PHP  Style
    WriWng  c  extension

    View Slide

  79. c9s  ValidaWonKit
    hHps://github.com/c9s/ValidaWonKit  
     
    c  extension  DEMO

    View Slide

  80. References
    !   Slide:  
    hHps://speakerdeck.com/racklin/phalcon-­‐php-­‐framework-­‐and-­‐
    phalcon-­‐kernel-­‐introducWon-­‐phpcon€w2012  
    !   Sample  code:  
    hHps://github.com/racklin/phpcon€w2012_phalcon  
    ! PhalconPHP  internal:    
    hHp://internals.phalconphp.com/en/latest/index.html  

    View Slide

  81. Thank  You

    View Slide