Upgrade to Pro — share decks privately, control downloads, hide ads and more …

OSDC.TW 2012 - The ecosystems of PHP and Perl

Yo-An Lin
April 15, 2012
1.7k

OSDC.TW 2012 - The ecosystems of PHP and Perl

Yo-An Lin

April 15, 2012
Tweet

Transcript

  1. Speaker • 林佑安 c9s@twitter, c9s@github • Web Developer • CPAN

    author for ~30 distributions. • 187 repos/projects on GitHub. • Perl programming since 2008. • PHP programming since 2011. Saturday, April 14, 12
  2. Let’s take a look at PHP from Perl. 從 Perl

    的角度來看 PHP Saturday, April 14, 12
  3. PHP • 從 C 語言 CGI 函式庫發展 • 以早期 Perl

    的部分語法為雛形 • 2002 年 PHP 4.3.0 新增 CLI SAPI • 2004 年 PHP 5.0.0 新增類 Java 的物件模 型 • 2012 年 PHP 5.4 引進 Trait, 內建 Http Server, 大量改善 Zend Engine 效能 Saturday, April 14, 12
  4. 為了「...」而設計的的 Perl “When Larry designed the first Perl, he was

    a sysadmin, Perl was designed for sysadmins. When Perl5 was created, Larry was a programmer. Perl5 is designed for programming. It is a programming language. Larry is now a language designer, Perl6 is a language for language design.” - Jesse Vincent from gugod http://gugod.org/2011/11/perl-for-dot-dot-dot/ Saturday, April 14, 12
  5. Perl: 以簡馭繁的中心思想 <?php $list  =  array_map(function($item)  {      

                               return  $item  .  'bar';                        },  array_filter(  $list,  function($item)  {                                  return  preg_match('#^foo#',$item);                        }); Saturday, April 14, 12
  6. Perl: 以簡馭繁的中心思想 <?php $list  =  array_map(function($item)  {      

                               return  $item  .  'bar';                        },  array_filter(  $list,  function($item)  {                                  return  preg_match('#^foo#',$item);                        }); #  perl @list  =  map  {  $_  .  'bar'  }  grep  /^foo/,  @list; Saturday, April 14, 12
  7. Perl • 從 Unix scripting language 出發 • 借取了 C,

    sed, awk, shell 等特性 • 1990 年時大量 CGI 程式使用 Perl 寫作 • 1994 年 Perl5 加入 OOP • 5.8.0 版時加入了 Unicode 支援 • 2006 年第⼀一版 Moose 模組 (後現代物件系統) - 可改 善 Perl 的物件導向功能,包含 Trait, Lazy Attributes 等 功能 • 2009 年 Plack 模組的出現使得 Perl 於 Web 開發上效 能、便利性更是大大提昇。 Saturday, April 14, 12
  8. 小結 • PHP 在語言設計上,除了 server-side scripting language 外並無明顯的中心思想 • Perl

    以命令列發展至 Web,而 PHP 是後來才加 入了命令列支援。 • 類別自動載入機制,是在 PHP 5.x 版後才加 入,因此多數 PHP 模組載入方式、路徑格式差 異相當大,也使得各系統間元件相當難重新使 用 (reuse) Saturday, April 14, 12
  9. PHP ecosystem • since 1999-11-21 • 586 packages on pear.php.net

    • 280 extensions on pecl.php.net Saturday, April 14, 12
  10. PHP ecosystem • since 1999-11-21 • 586 packages on pear.php.net

    • 280 extensions on pecl.php.net • 19 packages on pear2.php.net Saturday, April 14, 12
  11. PHP ecosystem • since 1999-11-21 • 586 packages on pear.php.net

    • 280 extensions on pecl.php.net • 19 packages on pear2.php.net • 840 packages on packagist.org Saturday, April 14, 12
  12. PHP ecosystem • since 1999-11-21 • 586 packages on pear.php.net

    • 280 extensions on pecl.php.net • 19 packages on pear2.php.net • 840 packages on packagist.org • no package tester. Saturday, April 14, 12
  13. PHP ecosystem • since 1999-11-21 • 586 packages on pear.php.net

    • 280 extensions on pecl.php.net • 19 packages on pear2.php.net • 840 packages on packagist.org • no package tester & reports • 2 mirror sites (us, de) Saturday, April 14, 12
  14. PHP ecosystem • Use `pear` and `pyrus.phar` to install PEAR

    packages (system-wide). • Pirum, PEAR channel builder. • MiniPear, PEAR channel mirror tool. Saturday, April 14, 12
  15. CPAN • Since October 1995 • 77800 Uploads • 24637

    Distributions Saturday, April 14, 12
  16. CPAN • Since October 1995 • 77800 Uploads • 24637

    Distributions • 105724 Modules Saturday, April 14, 12
  17. CPAN • Since October 1995 • 77800 Uploads • 24637

    Distributions • 105724 Modules • 9627 Authors Saturday, April 14, 12
  18. CPAN • Since October 1995 • 77800 Uploads • 24637

    Distributions • 105724 Modules • 9627 Authors • 245 mirror sites Saturday, April 14, 12
  19. CPAN • Since October 1995 • 77800 Uploads • 24637

    Distributions • 105724 Modules • 9627 Authors • 245 mirror sites • Issue Tracker (RT) Saturday, April 14, 12
  20. CPAN • Since October 1995 • 77800 Uploads • 24637

    Distributions • 105724 Modules • 9627 Authors • 245 mirror sites • Issue Tracker (RT) • CPAN Tester since 1999 Saturday, April 14, 12
  21. CPAN • Since October 1995 • 77800 Uploads • 24637

    Distributions • 105724 Modules • 9627 Authors • 245 mirror sites • Issue Tracker (RT) • CPAN Tester since 1999 • MetaCPAN Saturday, April 14, 12
  22. 250+ new distribution uploads per month From CPAN Tester statistics

    http://stats.cpantesters.org/trends.html Saturday, April 14, 12
  23. Command-line Tools • cpan (Original CPAN shell) • cpanp (CPANPLUS)

    • cpanm (CPAN Minus) • carton (Bundler) • pinto (CPAN package repository tool) • CPAN::Mini (CPAN Mirror Tool) Saturday, April 14, 12
  24. 上傳 CPAN Package: 3 Steps • Register a PAUSE account

    http://pause.perl.org/ Saturday, April 14, 12
  25. 上傳 CPAN Package: 3 Steps • Register a PAUSE account

    • Confirm your email http://pause.perl.org/ Saturday, April 14, 12
  26. 上傳 CPAN Package: 3 Steps • Register a PAUSE account

    • Confirm your email • Upload! http://pause.perl.org/ Saturday, April 14, 12
  27. Upload PEAR package • Register a PEAR account • Confirm

    Email • Are we done yet ? Saturday, April 14, 12
  28. And not yet • Join Mailing List • Use PHP_CodeSniffer

    to verify your standard coding convention. Saturday, April 14, 12
  29. And not yet • Join Mailing List • Use PHP_CodeSniffer

    to verify your standard coding convention. • Create a proposal draft Saturday, April 14, 12
  30. And not yet • Join Mailing List • Use PHP_CodeSniffer

    to verify your standard coding convention. • Create a proposal draft • Proposal stage, discuss & discuss ... Saturday, April 14, 12
  31. And not yet • Join Mailing List • Use PHP_CodeSniffer

    to verify your standard coding convention. • Create a proposal draft • Proposal stage, discuss & discuss ... • Waiting for votes Saturday, April 14, 12
  32. And not yet • Join Mailing List • Use PHP_CodeSniffer

    to verify your standard coding convention. • Create a proposal draft • Proposal stage, discuss & discuss ... • Waiting for votes • Proposal finished Saturday, April 14, 12
  33. PEAR Most packages and libraries are not on PEAR, they

    have their own channels. Saturday, April 14, 12
  34. PHPUnit PEAR Symfony Zend Pyrus phpmd phpspec phrozn Most packages

    and libraries are not on PEAR, they have their own channels. ezComponents Drupal Packages WordPress Composer pearfarm OpenPEAR Saturday, April 14, 12
  35. PEAR Most packages and libraries are not on PEAR, they

    have their own channels. Saturday, April 14, 12
  36. use inc::Module::Install; name 'Plack'; all_from 'lib/Plack.pm'; readme_from 'lib/Plack.pm'; requires 'LWP::UserAgent',

    5.814; requires 'HTTP::Message', 5.814; requires 'URI', 1.36; tests 't/*.t t/*/*.t'; author_tests 'xt'; install_share 'share'; install_script 'scripts/plackup'; auto_install; auto_set_repository; resources homepage => "http://plackperl.org", bugtracker => "https://github.com/ miyagawa/plack/issues"; WriteAll; This is how we do in Perl Saturday, April 14, 12
  37. package.xml <?xml version="1.0" encoding="ISO-8859-1"?> <package packagerversion="1.9.4" version="2.0" xmlns="http://pear.php.net/dtd/ package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0"

    xmlns:xsi="http:// www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/ tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> <name>gearman</name> <channel>pecl.php.net</channel> <summary>PHP wrapper to libgearman</summary> <description>This extension uses libgearman library to provide API for communicating with gearmand, and writing clients and workers.</description> <lead> <name>James M. Luedke</name> <user>jluedke</user> <email>[email protected]</email> <active>no</active> </lead> <lead> <name>Herman J. Radtke III</name> <user>hradtke</user> <email>[email protected]</email> <active>yes</active> </lead> <date>2011-12-05</date> <time>16:41:40</time> <version> <release>1.0.1</release> <api>1.0.1</api> </version> <stability> <release>stable</release> <api>stable</api> </stability> Saturday, April 14, 12
  38. package.xml <license uri="http://www.php.net/license">PHP</license> <notes> - Fixed PECL Bug #60438 (wrong

    version number in php_gearman.h) </notes> <contents> <dir name="/"> <file md5sum="da6d77a214aa6239b68efdbe2b12fd61" name="examples/image_thumbnail_client_bg.php" role="doc" /> <file md5sum="bdda0aa48d9df805b7cb333d1d986d62" name="examples/image_thumbnail_client.php" role="doc" /> <file md5sum="4c6afcfb3c9614c8f67e62ca77c9d93b" name="examples/image_thumbnail_client_task.php" role="doc" /> <file md5sum="828cc27885029474707acda417f15d16" name="examples/image_thumbnail_worker.php" role="doc" /> <file md5sum="8b868e03769c9422ca6891f440105359" name="examples/reverse_client_bg.php" role="doc" /> <file md5sum="3b344e946e0f0fe061ec34472e5f5fc2" name="examples/reverse_client.php" role="doc" /> <file md5sum="dbd0e80feebfc6f411556716e6339afc" name="examples/reverse_client_task.php" role="doc" /> <file md5sum="90be1117d2a4b2ccd79962ec6bb6e8bf" name="examples/reverse_worker.php" role="doc" /> <file md5sum="3171f70cb81342ee73acd90107bfccbb" name="EXPERIMENTAL" role="doc" /> <file md5sum="bf5270ef67ddf5132999761937755146" name="README" role="doc" /> <file md5sum="1eaa5362eb94b86ac3aaf63d4bb1b1e8" name="CREDITS" role="doc" /> <file md5sum="0e964e9273c606c46afbd311b5ad4d77" name="LICENSE" role="doc" /> <file md5sum="1edac7f37568b77870f60bb8d6fdccd0" name="ChangeLog" role="doc" /> <file md5sum="2965d6db29b2ca681b03ecb09fff689a" name="test_client.php" role="doc" /> <file md5sum="8e194fc11fb0ff5b2ef96f16758060aa" name="test_worker.php" role="doc" /> <file md5sum="7bcde7f5f213cb48e48c62ee9a948240" name="config.m4" role="src" /> <file md5sum="72b72fc3dc25f6b6b9d94acf9f3818a4" name="php_gearman.c" role="src" /> <file md5sum="4a0c4f71d316b31a5e8230abef7132eb" name="php_gearman.h" role="src" /> <file md5sum="c1c60600b416889269c65bf1a72b5862" name="gearman_001.phpt" role="test" /> </dir> </contents> Saturday, April 14, 12
  39. package.xml <dependencies> <required> <php> <min>5.1.0</min> <max>6.0.0</max> <exclude>6.0.0</exclude> </php> <pearinstaller> <min>1.4.0b1</min>

    </pearinstaller> </required> </dependencies> <providesextension>gearman</providesextension> <extsrcrelease /> <changelog> <release> <stability> <release>stable</release> <api>stable</api> </stability> <version> <release>1.0.1</release> <api>1.0.1</api> </version> <date>2011-12-05</date> <notes> - Fixed PECL Bug #60438 (wrong version number in php_gearman.h) </notes> </release> .... cut .... </package> Saturday, April 14, 12
  40. Unfortunately • 既有的 package file builder 幾乎都無法正 常運作 (不斷丟出 Exceptions)

    • 無清楚的文件 • 操作繁瑣,必須⼀一行行依照詢問輸入 Saturday, April 14, 12
  41. Unfortunately • 既有的 package file builder 幾乎都無法正 常運作 (不斷丟出 Exceptions)

    • 無清楚的文件 • 操作繁瑣,必須⼀一行行依照詢問輸入 • XML Tag 順序不對,就無法建置 Saturday, April 14, 12
  42. Unfortunately • 既有的 package file builder 幾乎都無法正 常運作 (不斷丟出 Exceptions)

    • 無清楚的文件 • 操作繁瑣,必須⼀一行行依照詢問輸入 • XML Tag 順序不對,就無法建置 • 雖然可以用 composer ,但卻是另外的生 態系統。 Saturday, April 14, 12
  43. PEAR • 封閉: 上傳套件必須審核審核再審核 • 小團體: PEAR Team < 20

    人 • 無系統化測試 • 不友善的 Issue Tracker • 設計複雜、繁瑣 Saturday, April 14, 12
  44. The Recent tools • Pyrus: pear2.php.net, is not opened yet.

    但工具本 身仍在 Alpha stage. • bundle install = Hack • 仍是使用 package.xml Saturday, April 14, 12
  45. The Recent tools • Pyrus: pear2.php.net, is not opened yet.

    但工具本 身仍在 Alpha stage. • bundle install = Hack • 仍是使用 package.xml • Composer project by @baderman,@Seldaek inspired from npmjs & opensuse. • JSON meta file Saturday, April 14, 12
  46. The package.ini File [package] name = PackageName desc = description

    version = 1.2.0 author = “c9s <[email protected]>” channel = pear.host.com Saturday, April 14, 12
  47. The package.ini File [package] name = PackageName desc = description

    version = 1.2.0 author = “c9s <[email protected]>” channel = pear.host.com 5+ lines to create a package. Saturday, April 14, 12
  48. How to use it? • Put your class files in

    `src/` • Tests in `tests/` • documentation in `doc/` Saturday, April 14, 12
  49. PEAR Class name rule • Twig_Loader_Filesystem => src/Twig/Loader/ Filesystem.php •

    Foo_Class_Bar => src/Foo/Class/Bar.php • Foo => src/Foo.php Saturday, April 14, 12
  50. PSR-0 classname rule • Class\SubClass => src/Class/SubClass.php • Foo =>

    src/Foo.php • Foo\Bar\Zoo => src/Foo/Bar/Zoo.php Saturday, April 14, 12
  51. PSR-0 classname rule • Class\SubClass => src/Class/SubClass.php • Foo =>

    src/Foo.php • Foo\Bar\Zoo => src/Foo/Bar/Zoo.php • Foo::Bar => lib/Foo/Bar.pm • Foo => lib/Foo.pm • Foo::Bar::Zoo => lib/Foo/Bar/Zoo.pm Perl: Saturday, April 14, 12
  52. Build PEAR package $  curl  -­‐s  http://install.onionphp.org/  |  bash $

     vi  package.ini $  onion  build            #  package.xml  is  out $  pyrus  package        #  build  *.tgz Saturday, April 14, 12
  53. Bundle packages [require] php = 5.3 pearinstaller = 1.4.1 pear.phpspec.net/PHPSpec

    = 1.2.2beta pear.symfony-project.com/YAML = “> 1.2” pear.twig-project.org/Twig = “< 0.3” pear.phpunit.de/PHPUnit = 0.0.1 PKG = http://host/to/package.tgz Saturday, April 14, 12
  54. Bundle packages $  tree  -­‐d  vendor   vendor !""  pear

           #""  CLIFramework        $      !""  Command        #""  CacheKit        #""  GetOptionKit        #""  PEARX        #""  SerializerKit        #""  SymfonyComponents        $      !""  YAML        !""  Universal                #""  ClassLoader                #""  Container                #""  Event                #""  FileSystem Saturday, April 14, 12
  55. SPL ClassLoader # autoload.php <?php require 'Universal/ClassLoader/SplClassLoader.php'; $classLoader = new

    \Universal\ClassLoader\SplClassLoader(array( 'Onion' => 'src', 'CurlKit' => 'src', 'PEARX' => 'vendor/pear', 'CacheKit' => 'vendor/pear', 'CLIFramework' => 'vendor/pear', 'GetOptionKit' => 'vendor/pear', )); $classLoader->useIncludePath(true); $classLoader->register(); Saturday, April 14, 12
  56. SPL ClassLoader # autoload.php <?php require 'Universal/ClassLoader/BasePathClassLoader.php'; $loader = new

    \Universal\ClassLoader \BasePathClassLoader( array( 'src', 'vendor/pear' )); $loader->useIncludePath(true); $loader->register(); Saturday, April 14, 12
  57. To build a standalone binary #!/bin/bash onion compile \ --lib

    src \ --lib vendor/pear \ --classloader \ --bootstrap scripts/onion.embed \ --executable \ --output onion.phar chmod +x onion.phar Saturday, April 14, 12
  58. 副產品 • PHPBrew - (perlbrew,rvm-like tool) • CLIFramework - Command-Line

    app framework. • GetOptionKit - getopt parser. • PEARX - PEAR channel client library. • CurlKit - curl download tool. • MiniPear - PEAR channel mirror tool Saturday, April 14, 12
  59. Future work • Support VCS repository (github, git, hg, svn)

    • Lock file • Better channel protocol • Autoloader generator • Authentication support • Support SSH • Fork welcome Saturday, April 14, 12
  60. Conclusion • PHP 生態系統雖先天不足,但 PHP 開發者及市場 仍相當大。 最近兩年 PSR (PHP

    Standard Group - Framework Developers) 逐漸在發展共通的規範, 達到元件 reuse 的目的。 • Composer 雖是獨立的生態系統,且目前主要用 於 Symfony,但可期待未來的發展。 • 期待 PHP 生態系統能有更好的改善 Saturday, April 14, 12
  61. Conclusion • PHP 生態系統雖先天不足,但 PHP 開發者及市場 仍相當大。 最近兩年 PSR (PHP

    Standard Group - Framework Developers) 逐漸在發展共通的規範, 達到元件 reuse 的目的。 • Composer 雖是獨立的生態系統,且目前主要用 於 Symfony,但可期待未來的發展。 • 期待 PHP 生態系統能有更好的改善 ~CoffeePHP ? Saturday, April 14, 12