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

Steadily developing CakePHP 4.0 #phpstudy

Steadily developing CakePHP 4.0 #phpstudy

presentation slide for #phpstudy vol.134

original google slide document url is here.
https://docs.google.com/presentation/d/1Ad-1MngmavQq3ebN9fP4TfKmBUgUptqplUJobJeBaAc/edit?usp=sharing

Kazuki Higashiguchi

January 30, 2019
Tweet

More Decks by Kazuki Higashiguchi

Other Decks in Technology

Transcript

  1. Steadily developing
    CakePHP 4.0
    #phpstudy 第134回PHP勉強会@東京
    Kazuki Higashiguchi (@hgsgtk)
    1

    View full-size slide

  2. CakePHP user?
    Question
    2

    View full-size slide

  3. CakePHP user?
    Question
    CakePHP 4.0 is developing
    steadily
    Learn PHP through
    CakePHP 4.0
    3

    View full-size slide

  4. @hgsgtk
    Kazuki Higashiguchi
    job is … Software Engineer
    lang is ... PHP, Go ...etc
    belongs to ... BASE BANK株式会社
    (BASE株式会社の100%子会社)
    at #phpstudy talked about …
    terraform / ECS / Unit Testing
    4

    View full-size slide

  5. Relation between
    $me and CakePHP
    ● work with CakePHP2.x
    ○ 2014 CakePHP 2.3
    ○ 2017~ CakePHP 2.3~2.10 @BASE
    ● private with CakePHP3.x
    ○ php sample code at talks
    ● a contributor to CakePHP
    ○ cakephp/cakephp 2.x
    ○ cakephp/docs 3.x
    ○ not a member
    5

    View full-size slide

  6. =
    6
    sample code with CakePHP 3.7
    https://speakerdeck.com/hgsgtk/tesutokaxin-iwojie-jue-surutesutoqu-dong-kai-fa-falseahuroti-at-p
    hpkanhuarensuxian-tai-2019

    View full-size slide

  7. =
    7
    translation of cakephp3.7 migration guide
    https://github.com/cakephp/docs/pull/5950 https://book.cakephp.org/3.0/ja/appendices/3
    -7-migration-guide.html#

    View full-size slide

  8. About CakePHP
    Web Application Framework
    follow MVC(MVC2)
    written in PHP
    2005 first released
    major versions (1.x/2.x/3.x)
    https://cakephp.org/
    8

    View full-size slide

  9. CakePHP 4.0
    9

    View full-size slide

  10. 3.4 and Path to 4.0.0 talked by Mark Story at CakeFest 2017
    10
    https://www.slideshare.net/markstory/cakephp-the-road-ahead

    View full-size slide

  11. Announcing CakePHP 4 Strawberry
    11
    https://twitter.com/cakephp/status/877888565442560000

    View full-size slide

  12. Work on CakePHP 4 has started!
    12
    https://twitter.com/cakephp/status/1003609827631947777

    View full-size slide

  13. =
    Outline
    CakePHP next major version
    4.0 Roadmap (https://github.com/cakephp/cakephp/wiki/4.0-Roadmap)
    Timeline
    2017/6/11 “3.4 and path to 4.0” talked by Mark Story
    https://www.slideshare.net/markstory/cakephp-the-road-ahead
    2017/6/23 Announcing CakePHP 4 Strawberry
    https://bakery.cakephp.org/2017/06/23/upcoming-cakephp-roadmap.htm
    l
    2018/6/4 Work on CakePHP 4 started
    13
    CakePHP 4

    View full-size slide

  14. 4.x
    https://github.com/cakephp/cakephp/tree/4.x
    14

    View full-size slide

  15. Features
    The CakePHP4 features
    Easy upgrade
    Require PHP 7.1+
    Stricter Typing
    API improvements
    Deprecation Cleanup
    15

    View full-size slide

  16. Features
    The CakePHP4 features
    Easy upgrade
    Require PHP 7.1+
    Stricter Typing
    API improvements
    Deprecation Cleanup
    16

    View full-size slide

  17. =
    ● primarily be a clean-up release
    ● focus on removing all the deprecated features
    ● not add significant new features
    ● “CakePHP 3.6 を使用していれば簡単に移行できるようにする方針”
    17
    Easy upgrade

    View full-size slide

  18. Features
    The CakePHP4 features
    Easy upgrade
    Require PHP 7.1+
    Stricter Typing
    API improvements
    Deprecation Cleanup
    18

    View full-size slide

  19. =
    ● CakePHP 4.0 require PHP 7.1+
    ● Issue#9956 4.x RFC - PHP7 only?
    ○ https://github.com/cakephp/cakephp/issues/9956
    ● active support for 7.0 ends in late 2017 => 7.1
    19
    Require PHP7.1+

    View full-size slide

  20. =
    20
    Require PHP 7.1+
    {
    "name": "cakephp/cakephp",
    (omit)
    "require": {
    "php": ">=7.1.0",
    (omit)
    },
    }
    https://github.com/cakephp/cakephp/blob/d356d363a3fa60cafab45db4e7f2ffaec5a506e1/composer.js
    on#L31

    View full-size slide

  21. =
    ● Null coalesce operator (Null合体演算子)
    ● nullable types
    21
    Use new features of PHP7.1+

    View full-size slide

  22. =
    public static function getRequest(bool $current = false): ?ServerRequest
    {
    if ($current) {
    $request = end(static::$_requests);
    return $request ?: null;
    }
    return static::$_requests[0] ?? null;
    }
    22
    Null coalesce operator and nullable type
    https://github.com/cakephp/cakephp/blob/45becd398ac060c9d1f379c42627a6ba81158933/src/Routi
    ng/Router.php#L301

    View full-size slide

  23. Features
    The CakePHP4 features
    Easy upgrade
    Require PHP 7.1+
    Stricter Typing
    API improvements
    Deprecation Cleanup
    23

    View full-size slide

  24. =
    use scalar type hints, strict typing as much as possible
    The addition of type hints should not be added if they will cause significant
    backwards compatibility breaks.
    24
    Stricter Typing
    https://github.com/cakephp/cakephp/wiki/4.0-Roadmap

    View full-size slide

  25. =
    25
    scalar type hinting and strict typing
    declare(strict_types=1);
    namespace Cake\ORM\Association;
    class HasMany extends Association
    {
    public function isOwningSide(Table $side): bool
    {
    return $side === $this->getSource();
    }
    https://github.com/cakephp/cakephp/blob/c2995a9ffc5c7dd06f67b56153db7a6cd665c17e/src/ORM/
    Association/HasMany.php#L98

    View full-size slide

  26. Features
    The CakePHP4 features
    Easy upgrade
    Require PHP 7.1+
    Stricter Typing
    API improvements
    Deprecation Cleanup
    26

    View full-size slide

  27. API
    Improvement
    Features
    Use null instead of false for
    `nullable` return
    Support for PSR 15 middleware
    Implement PSR 16
    ...etc
    27
    https://github.com/cakephp/cakephp/wiki/4.0-
    Roadmap

    View full-size slide

  28. API
    Improvement
    Features
    Use null instead of false
    for `nullable` return
    Support for PSR 15 middleware
    Implement PSR 16
    ...etc
    28
    https://github.com/cakephp/cakephp/wiki/4.0-
    Roadmap

    View full-size slide

  29. =
    (3.x) @return string|false => 4.x @return string|null
    29
    Use null instead of false for `nullable` return
    https://github.com/cakephp/cakephp/blob/5aa16f4464a062f197eb31ab32b3862be9958cf7/src/O
    RM/BehaviorRegistry.php#L88
    public static function className(string $class): ?string
    {
    $result = App::className($class, 'Model/Behavior',
    'Behavior');
    if (!$result) {
    $result = App::className($class, 'ORM/Behavior',
    'Behavior');
    }
    return $result;
    }

    View full-size slide

  30. API
    Improvement
    Features
    Use null instead of false for
    `nullable` return
    Support for PSR 15
    middleware
    Implement PSR 16
    ...etc
    30
    https://github.com/cakephp/cakephp/wiki/4.0-
    Roadmap

    View full-size slide

  31. =
    “ミドルウェアオブジェクトは、再利用可能で構成可能なリクエスト処理、あるいはレス
    ポンス構築処理の層でアプリケーションを「ラップ」する機能を提供します。”
    (「CakePHP3.7 books ミドルウェア」)
    31
    About middleware
    https://book.cakephp.org/3.0/en/controllers/middleware.html

    View full-size slide

  32. =
    ● BodyParserMiddleware
    ● CsrfProtectionMiddleware
    ● EncryptedCookieMiddleware
    ● SecurityHeadersMiddleware
    ● ErrorHandlerMiddleware
    ● AssetMiddleware
    ● RoutingMiddleware
    32
    CakePHP middlewares

    View full-size slide

  33. ぺちこん仙台2019の山岡さんの発表がとてもわかり易い
    33
    https://speakerdeck.com/hiro_y/phpfalsemiddlewarewo-shi-ikonasutameni

    View full-size slide

  34. =
    PHP Standards Recommendations
    https://www.php-fig.org/psr/
    コミュニティにて影響が大きい勧告
    (決定的な「標準」というわけではない)
    34
    About PSR

    View full-size slide

  35. PSR-7とPSR-15 について山岡さん資料内のありがたい図
    35
    https://speakerdeck.com/hiro_y/phpfalsemiddlewarewo-shi-ikonasutameni?slide=28

    View full-size slide

  36. =
    ● PSR-7: HTTP message interfaces
    ○ https://www.php-fig.org/psr/
    ○ https://github.com/php-fig/http-message
    ● CakePHP では 3.4 にて対応
    Interfaces
    ● Psr\Http\Message\RequestInterface
    ○ https://github.com/php-fig/http-message/blob/master/src/RequestInterface.php
    ● Psr\Http\Message\ResponseInterface
    ○ https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php
    36
    About PSR-7

    View full-size slide

  37. =
    ● PSR-15: HTTP Server Request Handlers
    ○ https://www.php-fig.org/psr/psr-15/
    Interfaces
    ● Psr\Http\Server\RequestHandlerInterface
    ● Psr\Http\Server\MiddlewareInterface
    37
    About PSR-15

    View full-size slide

  38. RequestHandlerInterface
    38
    namespace Psr\Http\Server;
    use Psr\Http\Message\ResponseInterface;
    use Psr\Http\Message\ServerRequestInterface;
    interface RequestHandlerInterface
    {
    public function handle(ServerRequestInterface $request):
    ResponseInterface;
    }
    https://github.com/php-fig/http-server-handler/blob/a557d72810470f7cf1bf5aa83523097c5c566e02/sr
    c/RequestHandlerInterface.php#L3

    View full-size slide

  39. MiddlewareInterface
    39
    namespace Psr\Http\Server;
    use Psr\Http\Message\ResponseInterface;
    use Psr\Http\Message\ServerRequestInterface;
    interface MiddlewareInterface
    {
    public function process(ServerRequestInterface $request,
    RequestHandlerInterface $handler): ResponseInterface;
    }
    https://github.com/php-fig/http-server-middleware/blob/9aad1c57354a158e3582560cf304bcae216d87
    4b/src/MiddlewareInterface.php#L15

    View full-size slide

  40. 40
    https://twitter.com/mark_story/status/1090084761433968640

    View full-size slide

  41. 41
    https://github.com/cakephp/cakephp/pull/12907

    View full-size slide

  42. =
    42
    Implementing PSR-15
    {
    "name": "cakephp/cakephp",
    (omit)
    "require": {
    (omit)
    "psr/http-client": "^1.0",
    "psr/http-server-handler": "^1.0",
    "psr/http-server-middleware": "^1.0",
    (omit)
    },
    }
    https://github.com/cakephp/cakephp/blob/d356d363a3fa60cafab45db4e7f2ffaec5a506e1/composer.js
    on#L31

    View full-size slide

  43. =
    PSR-15をサポートしたので、自前でmiddlewareを書く際の実装に変化
    43
    Changes For 一般PHPer

    View full-size slide

  44. =
    44
    Middleware before CakePHP4.x
    namespace App\Middleware;
    use Cake\I18n\Time;
    use Psr\Http\Message\ResponseInterface;
    use Psr\Http\Message\ServerRequestInterface;
    final class TrackingCookieMiddleware
    {
    public function __invoke(
    ServerRequestInterface $request,
    ResponseInterface $response,
    $next): ResponseInterface
    {
    // pass the control to next middleware
    $response = $next($request, $response);
    if (!$request->getCookie('landing_page')) {
    $expiry = new Time('+ 1 year');
    $response = $response->withCookie('landing_page', [
    'value' => $request->here(),
    'expire' => $expiry->format('U'),
    ]);
    }
    return $response;
    }
    }

    View full-size slide

  45. =
    45
    Middleware implemented PSR-15
    namespace App\Middleware;
    use Cake\I18n\Time;
    use Psr\Http\Message\ResponseInterface;
    use Psr\Http\Message\ServerRequestInterface;
    use Psr\Http\Server\RequestHandlerInterface;
    use Psr\Http\Server\MiddlewareInterface;
    final class TrackingCookie4xMiddleware implements MiddlewareInterface
    {
    public function process(
    ServerRequestInterface $request,
    RequestHandlerInterface $handler
    ): ResponseInterface
    {
    $response = $handler->handle($request);
    if (!$request->getCookie('landing_page')) {
    $expiry = new Time('+1 year');
    $response = $response->withCookie('landing_page', [
    'value' => $request->here(),
    'expire' => $expiry->format('U')
    ]);
    }
    return $response;
    }
    }

    View full-size slide

  46. API
    Improvement
    Features
    Use null instead of false for
    `nullable` return
    Support for PSR 15 middleware
    Implement PSR 16
    ...etc
    46
    https://github.com/cakephp/cakephp/wiki/4.0-
    Roadmap

    View full-size slide

  47. =
    RFC - Implement PSR16 support in Cache
    https://github.com/cakephp/cakephp/issues/12483
    “The PSR16 interfaces define a simpler Cache interface that would allow
    CakePHP to leverage solutions from outside of CakePHP in the longer term if
    we adopt the interfaces.”
    47
    Implement PSR-16

    View full-size slide

  48. 48
    https://github.com/cakephp/cakephp/pull/12591

    View full-size slide

  49. Features
    The CakePHP4 features
    Easy upgrade
    Require PHP 7.1+
    Stricter Typing
    API improvements
    Deprecation Cleanup
    49

    View full-size slide

  50. =
    4.0 以後にて非推奨メソッドを削除
    CakePHP3.6まであげれたら非推奨箇所を書き換えて4.0へアップデート
    3.6 にて非推奨になった機能郡
    https://book.cakephp.org/3.0/ja/appendices/3-6-migration-guide.html#id2
    3.7 にて非推奨になった機能郡
    https://book.cakephp.org/3.0/ja/appendices/3-7-migration-guide.html#id2
    50
    Deprecation Creanup

    View full-size slide

  51. Other
    Core Plugin Updates and tooling
    DebugKit
    Bake
    Migrations
    Authentication
    Authorization
    ElasticSearch
    51

    View full-size slide

  52. 52
    https://twitter.com/mark_story/status/1086827718367420416

    View full-size slide

  53. =
    53
    cakephp/bake folder structure
    3.x 4.x
    https://github.com/cakephp/bake/tree/4.x
    https://github.com/cakephp/bake/tree/3.next

    View full-size slide

  54. =
    declare(strict_types=1);
    class AllCommand extends BakeCommand
    {
    public function execute(Arguments $args, ConsoleIo $io): ?int
    {
    }
    }
    54
    Stricter Typing
    https://github.com/cakephp/bake/blob/38b68f1daaeb94e3e2856d91bc3037250538f640/src/Command/Al
    lCommand.php#L2

    View full-size slide

  55. Long term
    support for
    3.x & 2.x
    CakePHP 3.x
    ● Bug fixes for 18 months after
    4.0.0.
    ● Security fixes for 36 months
    after 4.0.0 is released.
    CakePHP 2.x
    ● Bug fixes for 12 months after
    the release of 4.0.0.
    ● Security fixes for 18 months
    after the release of 4.0.0.
    55
    https://bakery.cakephp.org/2017/06/23/upcoming
    -cakephp-roadmap.html

    View full-size slide

  56. =
    ● 2008 Orlando, FL, USA
    ● 2008 Buenos Aires, Argentina
    ● 2009 Berlin, Germany
    ● 2010 Chicago, IL, USA
    ● 2011 Manchester, UK
    ● 2012 Manchester, UK
    ● 2013 San Francisco, CA, USA
    ● 2014 Madrid, Spain
    ● 2015 New York, NY, USA
    ● 2016 Amsterdam, Netherlands
    ● 2017 New York, NY, USA
    ● 2019 Tokyo, Japan
    57
    CakeFest’s venue

    View full-size slide

  57. 59
    https://cakefest.org/

    View full-size slide

  58. 60
    https://cakefest.org/

    View full-size slide

  59. 61
    https://cakefest.org/

    View full-size slide

  60. スタンプラリー行脚の予定空けておきましょう
    62

    View full-size slide

  61. =
    ● CakePHP 4.0 が開発進行中、随時実装完了している箇所もあり
    ● CakePHP 4.0 は、 PHP7.1以降の新機能を採用していい感じ
    ● メジャーバージョンアップのタイミングに、中でどういう実装をしているか見ると、
    PHP自体の勉強になる
    ● CakeFest2019に参加しよう!
    63
    Summary

    View full-size slide