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

Framework agnostic packages for the win (SkiPHP 2016)

Framework agnostic packages for the win (SkiPHP 2016)

Jonathan Reinink

January 15, 2016
Tweet

More Decks by Jonathan Reinink

Other Decks in Technology

Transcript

  1. Jonathan Reinink
    Software developer from Canada.
    Been writing PHP for over 15 years.
    Marketing agency for over a decade.
    Started contract development this year.
    I <3 open source.

    View full-size slide

  2. We're in the middle of
    a PHP Renaissance.

    View full-size slide

  3. We're seeing a huge shift to
    creating smaller pieces of
    focused, reusable
    "framework agnostic" code.

    View full-size slide

  4. What exactly does
    framework agnostic mean?

    View full-size slide

  5. Code that works
    independent of
    frameworks.

    View full-size slide

  6. It’s about
    interoperability.

    View full-size slide

  7. Framework agnostic
    does not mean
    framework intolerant.

    View full-size slide

  8. Why is framework
    agnostic code important?

    View full-size slide

  9. Framework agnostic code
    allows you to choose the
    best library for the job.

    View full-size slide

  10. "You should be using best of
    breed stuff, you shouldn't have to
    use the stuff that came with your
    framework if there is better stuff
    out there; it should just drop in.”
    — Rob Allen

    View full-size slide

  11. Framework agnostic code
    is also more reusable than
    framework specific code.

    View full-size slide

  12. Reusable code is code
    we don't have to write.

    View full-size slide

  13. Creating reusable code is
    key to the continued
    success of PHP.

    View full-size slide

  14. As software become
    increasingly more complex,
    our reliance on existing,
    quality code increases.

    View full-size slide

  15. We’re seeing a future
    where the various PHP
    communities actively share
    code with one another.

    View full-size slide

  16. So, why then are we
    using framework
    specific code?

    View full-size slide

  17. Because we sort of had to.

    View full-size slide

  18. PHP was first
    released in 1995.

    View full-size slide

  19. PHP had a decent
    standard library, but
    pieces were missing.

    View full-size slide

  20. Routing
    Templating
    Database Abstraction
    Validation
    Session Handling
    Error Management
    Authentication
    Caching
    Request Processing
    Responses Sending

    View full-size slide

  21. Developers starting
    writing new, focused
    PHP libraries.

    View full-size slide

  22. But…we had no easy
    way to share them.

    View full-size slide

  23. So we manually
    downloaded zip files.

    View full-size slide

  24. Introducing PEAR:
    PHP first attempt at
    a package manager.

    View full-size slide

  25. But…PEAR had
    some issues.

    View full-size slide

  26. The community gave up
    on PEAR, and started
    using frameworks instead.

    View full-size slide

  27. Frameworks were
    easy to download,
    and they just worked.

    View full-size slide

  28. Frameworks came with
    almost everything needed
    for the average project.

    View full-size slide

  29. Frameworks allowed us
    to get work done quickly.

    View full-size slide

  30. 2005 became the year
    of PHP frameworks.
    Symfony, CakePHP, Solar, Agavi, ezComponents

    View full-size slide

  31. However, frameworks were
    only a bandaid solution to
    the code sharing problem.

    View full-size slide

  32. What if the framework
    didn't have some other
    functionality you needed?

    View full-size slide

  33. Naturally, frameworks
    grew to accommodate a
    wide range of use cases.

    View full-size slide

  34. We continued to
    download zip files. :(

    View full-size slide

  35. This led to very distinct siloed
    micro communities within
    the larger PHP community.

    View full-size slide

  36. Then things changed.

    View full-size slide

  37. 1994 - 2004:
    2005 - 2012:
    2013 - Future:
    Vanilla PHP
    Frameworks
    Packages

    View full-size slide

  38. Unlike PEAR, Composer
    got a LOT of things right.

    View full-size slide

  39. Downloaded packages.
    Resolved their dependencies.
    Autoloaded classes.

    View full-size slide

  40. Composer was an overnight
    success, and became
    mainstream in 2013.

    View full-size slide

  41. Packagist, the official Composer
    package repository, now serves
    well over 100 million package
    installs per month.

    View full-size slide

  42. Developers benefit from
    faster release cycles and
    a wealth of packages to
    choose from.

    View full-size slide

  43. Composer gave us more
    than easy package
    installation, it also gave
    us interoperability.

    View full-size slide

  44. Composer let us cherry
    pick code (packages)
    without all the pain of
    downloading zip files.

    View full-size slide

  45. “We're a group of established
    PHP projects whose goal is to talk
    about commonalities between
    our projects and find ways we can
    work better together.”
    (PHP-FIG goal)

    View full-size slide

  46. Their very first
    recommendation was an
    autoloading standard
    called PSR-0.

    View full-size slide

  47. The PHP-FIG continues
    to develop standards that
    make framework agnostic
    code more possible.

    View full-size slide

  48. namespace Psr\Log;
    interface LoggerInterface
    {
    public function emergency($message, array $context = []);
    public function alert($message, array $context = []);
    public function critical($message, array $context = []);
    public function error($message, array $context = []);
    public function warning($message, array $context = []);
    public function notice($message, array $context = []);
    public function info($message, array $context = []);
    public function debug($message, array $context = []);
    public function log($level, $message, array $context = []);
    }

    View full-size slide

  49. PSR-7: HTTP message
    interfaces.

    View full-size slide

  50. $_SERVER
    $_GET
    $_POST
    $_FILES
    $_SESSION
    $_COOKIE

    View full-size slide

  51. $response = (new Response())
    ->withStatus(200, 'OK')
    ->withHeader('Content-Type', 'application/json')
    ->withBody($body);

    View full-size slide

  52. Expect to see more of
    PSR-7 in 2016.

    View full-size slide

  53. www.php-fig.org

    View full-size slide

  54. So, do we still need
    frameworks?

    View full-size slide

  55. In the time of packages,
    the purpose of
    frameworks is changing.

    View full-size slide

  56. “A huge part of the PHP Renaissance
    has been due to the advent of
    Composer, which has simplified
    dependency management, and led to
    tens of thousands of standalone
    libraries and packages.” (continued)

    View full-size slide

  57. “As such, frameworks, while still popular,
    are often being eschewed for
    homemade, application-specific
    frameworks made of commodity
    components. Frameworks simply cannot
    ignore this trend, and decoupling should
    become the norm going forward.”

    View full-size slide

  58. Frameworks no longer
    have to be everything
    to everyone.

    View full-size slide

  59. “Frameworks dictate architecture,
    handle your bootstrapping and
    essentially give you a set of lines
    to color inside.”
    — Phil Sturgeon

    View full-size slide

  60. Frameworks are now the glue
    between reusable framework
    agnostic packages.

    View full-size slide

  61. Need something your
    framework doesn’t offer?
    No worries, just:
    > composer require the/package

    View full-size slide

  62. Even frameworks are now
    being built using
    framework agnostic code.

    View full-size slide

  63. “Today, even competing frameworks
    make liberal use of each other’s
    components, because the PHP
    community at large has adopted a culture
    of sharing and of working together.”
    — Heroku (blog)

    View full-size slide

  64. Don’t be afraid to pull
    in components from
    other frameworks.

    View full-size slide

  65. We’re seeing an
    increase in framework
    specialization.

    View full-size slide

  66. "You will probably see more
    use-case specific skeletons
    come into the Zend
    Framework world.”
    — Rob Allen

    View full-size slide

  67. "Lumen 5.2.0 marks a shift to
    exclusively focusing on
    stateless APIs with Lumen for
    better definition between two
    frameworks.”
    - Taylor Otwell

    View full-size slide

  68. What will happen to
    bundles/plugins/
    modules?

    View full-size slide

  69. Puli aims to replace "bundles",
    "plugins", "modules" and similar
    specialized packages of different
    frameworks with one generic,
    framework independent solution.

    View full-size slide

  70. puli.io
    https://www.youtube.com/watch?v=nRqwGGROvfw

    View full-size slide

  71. How to choose a
    framework agnostic
    package.

    View full-size slide

  72. Don't reach for the
    framework specific
    package first.

    View full-size slide

  73. Look for packages with
    an existing track record.

    View full-size slide

  74. Beware of the framework
    specific wrapper package.

    View full-size slide

  75. Learn to write simple
    service providers.

    View full-size slide

  76. use Illuminate\Support\ServiceProvider;
    use Services_Twilio;
    class TwilioServiceProvider extends ServiceProvider
    {
    public function register()
    {
    $this->app->singleton(Services_Twilio::class, function ($app) {
    return new Services_Twilio(
    env('TWILIO_SID'),
    env('TWILIO_TOKEN')
    );
    });
    }
    }

    View full-size slide

  77. How to create a
    framework agnostic
    package.

    View full-size slide

  78. Remove all framework
    specific dependencies.

    View full-size slide

  79. // Framework specific
    $value = str_contains($haystack, $needle);
    // Framework agnostic
    $value = strpos($haystack, $needle) !== false;

    View full-size slide

  80. {
    "name": "your-awesome/package",
    "require": {
    "illuminate/support": "^5.2",
    }
    }

    View full-size slide

  81. Don’t over commit, focus
    on doing one thing well.

    View full-size slide

  82. Use abstractions and
    existing interfaces
    wherever possible.

    View full-size slide

  83. File system: Flysystem
    Intervention
    GD
    ImageMagick
    Image processing:
    Local
    Azure
    AWS
    Dropbox
    FTP
    SFTP
    Memory
    Rackspace

    View full-size slide

  84. HTTP Messages:
    Logging:
    DI Containers:
    PSR-7
    PSR-3
    container-interop

    View full-size slide

  85. $server = League\Glide\ServerFactory::create([
    // File system abstraction
    'source' => new Filesystem(new Local('path')),
    // Image processor abstraction
    'driver' => 'imagick',
    // HTTP messages interface (PSR-7)
    'response' => new PsrResponseFactory(
    new Response(),
    function ($stream) {
    return new Stream($stream);
    }
    ),
    ]);

    View full-size slide

  86. Offer framework specific
    support for your package
    using service providers.

    View full-size slide

  87. {
    "name": "league/glide-cake",
    "description": "Glide adapter for CakePHP",
    "require": {
    "cakephp/cakephp": "^3.0",
    "league/glide": "^1.0"
    },
    "require-dev": {
    "phpunit/phpunit": "^4.0"
    },
    "autoload": {
    "psr-4": {
    "League\\Glide\\": "src/"
    }
    }
    }

    View full-size slide

  88. If you’re a maintainer of a
    framework specific package,
    please consider making it
    framework agnostic.

    View full-size slide

  89. It’s good for package maintainers.
    It’s good for other developers.
    It’s good for frameworks.
    It’s good for PHP.

    View full-size slide

  90. Thanks!
    Follow me on Twitter at @reinink.
    Rate this talk https://joind.in/16658.

    View full-size slide