Consider a smooth upgrade to CakePHP 3

652359244199b2b5108a5e09c027e0da?s=47 Kanazawa Yuki
November 09, 2019

Consider a smooth upgrade to CakePHP 3

652359244199b2b5108a5e09c027e0da?s=128

Kanazawa Yuki

November 09, 2019
Tweet

Transcript

  1. $BLF'FTU $POTJEFSBTNPPUI VQHSBEFUP$BLF1)1  https://www.lancers.jp/ Yuki Kanazawa

  2. $BLF'FTU Speaker Biography 2 Yuki Kanzawa Lancers, Inc. / Site

    Reliability Engineer (2013/11 -) Live in Tokyo, Japan Github︓yKanazawa Twitter: @yakitori009 Language: C++, Java, PHP, Go Hobby: Shogi(Japanese Chess)
  3. $BLF'FTU About Lancers https://www.lancers.jp/ Genre: Crowdsourcing Start: 2008/4 PHP 5.2

    → 5.3 → 5.6 → 7.3 CakePHP 1.2 → 1.3 → 2.8 → 2.10
  4. $BLF'FTU (PPHMF5SFOET PG1)1'SBNFXPSLT

  5. $BLF'FTU Google Trends of PHP Framework TOP 5 Country (CakePHP)

    Japan Japan Finland South Africa Brazil Singapore
  6. $BLF'FTU Google Trends of PHP Framework Japan All Countries

  7. $BLF'FTU Google Trends of PHP Framework All Countries Japan CakePHP

    1.2 CakePHP 1.3 CakePHP 2.0 CakePHP 3.0
  8. $BLF'FTU IUUQTXXXMBODFSTKQ 6QHSBEF)JTUPSZ

  9. $BLF'FTU Upgrade History (https://www.lancers.jp/)      YNPSF

         PHP5.6 requires  2 months 1.7 year  1.8 month   1 day  2 months CakePHP2.10 requires PHP7.3 requires
  10. $BLF'FTU $BLF1)1$BLF1)1 DPFYJTUFENJHSBUJPO

  11. $BLF'FTU Pruduct scale (https://www.lancers.jp/) files steps Config 190 60000 Console

    170 30000 Controller 350 110000 Lib 1100 153000 Model 420 105000 View 2800 240000 All 5030 698000
  12. $BLF'FTU CakePHP 1.3 and 2.8 co-existed migration CakePHP1.3 CakePHP2.8

  13. $BLF'FTU CakePHP 1.3 and 2.8 co-existed migration + app(app of

    CakePHP1.3) + config + controllers + libs + models + tests + views + webroot + cake(CakePHP1.3) CakePHP1 Model CakePHP1 Controller CakePHP1 webroot CakePHP1 View CakePHP1 Lib
  14. $BLF'FTU CakePHP 1.3 and 2.8 co-existed migration + app(app of

    CakePHP1.3) + config + controllers + libs + models + tests + views + webroot + cake(CakePHP1.3) + vendor + cakephp + cakephp(CakePHP2.8) Install CakePHP2.8 CakePHP1 Model CakePHP1 Controller CakePHP1 webroot CakePHP1 View CakePHP1 Lib
  15. $BLF'FTU CakePHP 1.3 and 2.8 co-existed migration + app(app of

    CakePHP1.3) + config + controllers + libs + models + tests + views + webroot + cake(CakePHP1.3) + cake28(app of CakePHP2) + Config + Test + webroot + vendor + cakephp + cakephp(CakePHP2.8) Setup CakePHP2 App CakePHP1 Model CakePHP1 Controller CakePHP1 webroot CakePHP1 View CakePHP1 Lib CakePHP2 webroot
  16. $BLF'FTU CakePHP 1.3 and 2.8 co-existed migration + app(app of

    CakePHP1.3) + config + controllers + libs + models + tests + views + webroot + cake(CakePHP1.3) + cake28(app of CakePHP2) + Config + Lib + Model + Test + vendor + cakephp + cakephp(CakePHP2.8) Copy from CakePHP1.3 CakePHP1 Model CakePHP1 Controller CakePHP1 webroot CakePHP1 View CakePHP1 Lib CakePHP2 Model CakePHP2 Lib CakePHP2 webroot
  17. $BLF'FTU CakePHP 1.3 and 2.8 co-existed migration + app(app of

    CakePHP1.3) + config + controllers + libs + models + tests + views + webroot + cake(CakePHP1.3) + cake28(app of CakePHP2) + Config + Controller + Lib + Model + Test + View + webroot + vendor + cakephp + cakephp(CakePHP2.8) Migrating per Controller CakePHP1 Model CakePHP1 Controller CakePHP1 webroot CakePHP1 View CakePHP1 Lib CakePHP2 Model CakePHP2 Lib CakePHP2 Controller CakePHP2 webroot CakePHP1 View
  18. $BLF'FTU CakePHP 1.3 and 2.8 co-existed migration + app(app of

    CakePHP1.3) + config + controllers + libs + models + tests + views + webroot + cake(CakePHP1.3) + cake28(app of CakePHP2) + Config + Controller + Lib + Model + Test + View + webroot + vendor + cakephp + cakephp(CakePHP2.8) Move webroot CakePHP1 Model CakePHP1 Controller CakePHP1 webroot CakePHP1 View CakePHP1 Lib CakePHP2 Model CakePHP2 Lib CakePHP2 Controller CakePHP2 webroot CakePHP1 View
  19. $BLF'FTU CakePHP 1.3 and 2.8 co-existed migration CakePHP2 Model CakePHP2

    Lib CakePHP2 Controller CakePHP2 webroot CakePHP1 View + cake28(app of CakePHP2) + Config + Controller + Lib + Model + Test + View + webroot + vendor + cakephp + cakephp(CakePHP2.8) Delete CakePHP1.3
  20. $BLF'FTU Upgrade per controller (or function) •app/config/switch.yml ◦Switch to CakePHP2.8

    in URI units --- cakephp28: … - /mypage - /mypage/ - /mypage/index - /mypage/index/* - /mypage/charge - /mypage/charge/* … - /user/login - /user/login/* CakePHP1 Model CakePHP1 Controller CakePHP1 webroot CakePHP1 View CakePHP1 Lib CakePHP2 Model CakePHP2 Lib CakePHP2 Controller CakePHP2 webroot CakePHP1 View
  21. $BLF'FTU Upgrade per Controller (or Function) •app/webroot/index.php ◦Switch to CakePHP2.8

    <?php if (!defined('DS')) { define('DS', DIRECTORY_SEPARATOR); } if (!defined('ROOT')) { define('ROOT', dirname(dirname(dirname(__FILE__)))); } // to CakePHP2.8 require ROOT . '/app/vendors/composers/autoload.php’; if (¥Lancers¥Cake::isSwitched()) { include ROOT . '/cake28/webroot/index.php’; return; } …
  22. $BLF'FTU Problem of per controller migration + app(app of CakePHP1.3)

    + config + controllers + libs + models + tests + views + cake(CakePHP1.3) + cake28(app of CakePHP2) + Config + Controller + Lib + Model + Test + View + vendor + cakephp + cakephp(CakePHP2.8) •Both versions need to be modified ◦At usual enhancement Both versions Need to be modifeid
  23. $BLF'FTU $BLF1)1$BLF1)1 DPFYJTUFENJHSBUJPO

  24. $BLF'FTU CakePHP 2 and 3 co-existed migration + cake28(app of

    CakePHP2) + Config + Controller + Lib + Model + Test + Vendor + cakephp + cakephp(CakePHP2.10) + View + vendor + cakephp + cakephp(CakePHP2.10) Move + cake28(app of CakePHP2) + Config + Controller + Lib + Model + Test + Vendor + cakephp + cakephp(CakePHP2.10) + View + vendor + cakephp + cakephp(CakePHP3.8) Install CakePHP 3
  25. $BLF'FTU CakePHP 2 and 3 co-existed migration + config(config of

    CakePHP3) + .env + app.php + bootstrap.php + src(app of CakePHP3) + Controller + Lib + Model + cake28(app of CakePHP2) + Config(config of CakePHP2) + Controller + Lib + Model + Test(test of CakePHP2) + View + webroot(webroot of CakePHP2) + tests(test of CakePHP3) + webroot(webroot of CakePHP3) + index.php •Setup CakePHP3 ◦config ◦src ▪Controller ▪Lib ▪Model ◦tests ◦webroot ▪index.php
  26. $BLF'FTU Add config for CakePHP3 •cake28/Config/bootstrap.php … define('CONFIG_CAKEPHP3', __DIR__ .

    '/../../config/'); if (!env('DB_MASTER_HOST') && file_exists(CONFIG_CAKEPHP3 . '.env')) { $dotenv = new ¥josegonzalez¥Dotenv¥Loader([CONFIG_CAKEPHP3 . '.env']); $dotenv->parse()->putenv()->toEnv()->toServer(); } try { ¥Cake¥Core¥Configure::config( 'default', new ¥Cake¥Core¥Configure¥Engine¥PhpConfig(CONFIG_CAKEPHP3) ); ¥Cake¥Core¥Configure::load('app', 'default', false); } catch (¥Exception $e) { exit($e->getMessage() . "¥n"); } ¥Cake¥Datasource¥ConnectionManager::setConfig( 'default', ¥Cake¥Core¥Configure::read('Datasources.default') ); ¥Cake¥Cache¥Cache::setConfig( '_cake_model_', ¥Cake¥Core¥Configure::read('Cache._cake_model_') ); ¥Cake¥I18n¥I18n::setLocale(¥Cake¥Core¥Configure::read('App.defaultLocale')); Add Config of CakePHP 3
  27. $BLF'FTU $BLF1)1$BLF1)1 .JHSBUF-JC

  28. $BLF'FTU CakePHP2 -> CakePHP3 Migrete Lib •CakePHP2: cake28/Lib/Functions/Convert/City.php <?php namespace

    Cake2¥Lib¥Functions; function hexToStr($hex) { $string = ''; for ($i = 0; $i < strlen($hex) - 1; $i += 2) { $string .= chr(hexdec($hex[$i] . $hex[$i + 1])); } return $string; } CakePHP2 Model CakePHP2 Controller CakePHP2 View CakePHP2 Lib
  29. $BLF'FTU CakePHP2 -> CakePHP3 Migrete Lib •CakePHP3: src/Lib/Functions/Convert/City.php <?php namespace

    App¥Lib¥Functions; function hexToStr($hex) { $string = ''; for ($i = 0; $i < strlen($hex) - 1; $i += 2) { $string .= chr(hexdec($hex[$i] . $hex[$i + 1])); } return $string; } CakePHP2 Model CakePHP2 Controller CakePHP2 View CakePHP3 Lib Change namespace
  30. $BLF'FTU $BLF1)1$BLF1)1 .JHSBUF.PEFM

  31. $BLF'FTU CakePHP2 -> CakePHP3 Migrate Model •CakePHP2: cake28/Model/City.php <?php class

    City extends AppModel { public $name = 'City'; public $validate = [ 'id' => ['numeric'], ]; public function getName($cityId) { $prefectural = $this->findById($prefecturalId); if (isset($prefectural['Prefectural']['name']) { return null; } return $prefectural['Prefectural']['name']; } } CakePHP2 Model CakePHP2 Controller CakePHP2 webroot CakePHP2 View CakePHP2 Lib
  32. $BLF'FTU CakePHP2 -> CakePHP3 Migrate Model •Execute CakePHP3 bake command

    $ bin/cake bake model cities + src + Controller + Lib + Model + Entity + City.php + Table + CitiesTable.php + tests + Fixture + CitiesFixture.php + TestCase + Model + Table + CitiesTableTest.php CakePHP2 Model CakePHP2 Controller CakePHP2 webroot CakePHP2 View CakePHP2 Lib CakePHP3 Table CakePHP3 Entity
  33. $BLF'FTU CakePHP2 -> CakePHP3 Migrate Model •CakePHP2: cake28/Model/City.php public function

    getName(?int $cityId) : ?string { if (!isset($cityId)) { return null; } $result = $this->find()->select(['name’]) ->where(['id' => $cityId])->first(); return isset($result) ? $result->name : null; } public function __construct($id = false, $table = null, $ds = null) { parent::__construct($id, $table, $ds); $this->cake3Table = Cake¥ORM¥TableRegistry::getTableLocator()->get('Cities'); } public function getName($cityId) { return $this->cake3Table->getName($cityId); } Call CakePHP3 function •CakePHP3: src/Model/Table/CitiesTable.php Init CakePHP3 Table CakePHP2 Model CakePHP3 Table Re-implement same function
  34. $BLF'FTU CakePHP2 -> CakePHP3 Migrate Model CakePHP2 Controller CakePHP2 Model

    CakePHP2 webroot CakePHP3 Table CakePHP3 Entity Call same function Validate find save •Before Migrate Controller find save CakePHP2 Test
  35. $BLF'FTU CakePHP2 -> CakePHP3 Migrate Model CakePHP2 webroot CakePHP3 Table

    CakePHP3 Entity CakePHP3 Controller CakePHP3 webroot Validate •After Migrate Controller CakePHP2 Model CakePHP3 Test find save find JOIN (contain) CakePHP2 Test
  36. $BLF'FTU $BLF1)1$BLF1)1 .JHSBUF'PSN

  37. $BLF'FTU CakePHP2 -> CakePHP3 Migrate Form CakePHP2 Controller CakePHP2 Model

    CakePHP2 webroot CakePHP3 Form Validate •Before Migrate Controller public $useTable = false; $ bin/cake bake form xxxxx CakePHP2 Test CakePHP3 Test
  38. $BLF'FTU CakePHP2 -> CakePHP3 Migrate Form CakePHP2 webroot CakePHP3 Form

    CakePHP3 Controller CakePHP3 webroot •After Migrate Controller Validate CakePHP3 Test
  39. $BLF'FTU $BLF1)1$BLF1)1 .JHSBUF$POUSPMMFS

  40. $BLF'FTU CakePHP2 -> CakePHP3 Migrate Controller + src(app of CakePHP3)

    + Controller + Lib + Model + cake28(app of CakePHP2) + Test(UT of CakePHP2) + webroot(webroot of CakePHP2) + index.php + tests(UT of CakePHP3) + vendor + cakephp + (CakePHP3.8) CakePHP2 Controller CakePHP2 Model CakePHP2 webroot CakePHP3 Table CakePHP3 Entity
  41. $BLF'FTU CakePHP2 -> CakePHP3 Migrate Controller + src(app of CakePHP3)

    + Controller + Lib + Model + cake28(app of CakePHP2) + Test(UT of CakePHP2) + webroot(webroot of CakePHP2) + index.php + tests(UT of CakePHP3) + vendor + cakephp + (CakePHP3.8) + webroot(webroot of CakePHP3) + index.php •Add webroot ◦for CakePHP3 CakePHP2 Controller CakePHP2 Model CakePHP2 webroot CakePHP3 Table CakePHP3 Entity CakePHP3 webroot
  42. $BLF'FTU Upgrade per Controller (or Function) •cake28/webroot/index.php ◦Switch to CakePHP3

    <?php if (!defined('DS')) { define('DS', DIRECTORY_SEPARATOR); } if (!defined('ROOT')) { define('ROOT', dirname(dirname(dirname(__FILE__)))); } // to CakePHP3 require ROOT . '/vendor/autoload.php'; if (¥Cake2¥Lib¥Cake¥Cake::isSwitched()) { include ROOT . '/webroot/index.php'; return; } …
  43. $BLF'FTU Upgrade per controller (or function) •cake28/config/switch.yml ◦Switch to CakePHP3

    in URI units --- cakephp3: … - /mypage - /mypage/ - /mypage/index - /mypage/index/* - /mypage/charge - /mypage/charge/* … - /user/login - /user/login/* CakePHP2 Model CakePHP2 Controller CakePHP2 webroot CakePHP2 View CakePHP2 Lib CakePHP2 Table CakePHP3 Lib CakePHP3 Controller CakePHP3 webroot CakePHP3 View CakePHP3 Form
  44. $BLF'FTU $BLF1)1$BLF1)1 *TJUQPTTJCMF

  45. $BLF'FTU Support version PHP Min ver PHP Max ver PHPUnit

    Min ver PHPUnit Max ver CakePHP 2.10 5.3 7.* 3.7 5.7.* CakePHP 3.8 5.6 7.* 5.7.14 6.5 CakePHP 4 7.2 7.* 8.3.3
  46. $BLF'FTU Support version PHP Min ver PHP Max ver PHPUnit

    Min ver PHPUnit Max ver CakePHP 2.10 5.3 7.* 3.7 5.7.* CakePHP 3.8 5.6 7.* 5.7.14 6.5 CakePHP 4 7.2 7.* 8.3.3 •CakePHP2.10 -> CakePHP4 ◦Can use PHP 7.*
  47. $BLF'FTU PHPUnit support version PHP Min ver PHP Max ver

    PHPUnit Min ver PHPUnit Max ver CakePHP 2.10 5.3 7.* 3.7 5.7.* CakePHP 3.8 5.6 7.* 5.7.14 6.5 CakePHP 4 7.2 7.* 8.3.3 •CakePHP2.10 -> CakePHP3 ◦Can use PHPUnit 5.7.*
  48. $BLF'FTU PHPUnit support version PHP Min ver PHP Max ver

    PHPUnit Min ver PHPUnit Max ver CakePHP 2.10 5.3 7.* 3.7 5.7.* CakePHP 3.8 5.6 7.* 5.7.14 6.5 CakePHP 4 7.2 7.* 8.3.3 •CakePHP2.10 -> CakePHP4 ◦Cannot use same version
  49. $BLF'FTU PHPUnit support version PHP Min ver PHP Max ver

    PHPUnit Min ver PHPUnit Max ver CakePHP 2.10 5.3 7.* 3.7 5.7.* CakePHP 3.8 5.6 7.* 5.7.14 6.5 CakePHP 4 7.2 7.* 8.3.3 •CakePHP3.8 -> CakePHP4 ◦Cannot use same version
  50. $BLF'FTU $PFYJTU 1)16OJUBOE1)16OJU

  51. $BLF'FTU Co-Exist PHPUnit 5.7 and 8.4 + cake28(app of CakePHP2)

    + Config + Controller + Lib + Model + Test + Vendor + phpunit(PHPUnit5.7) + View + vendor + phpunit(PHPUnit5.7) Move + cake28(app of CakePHP2) + Config + Controller + Lib + Model + Test + Vendor + phpunit(PHPUnit5.7) + View + vendor + phpunit(PHPUnit8.4) Install PHPUnit 8
  52. $BLF'FTU 5IBOL:PV

  53. $BLF'FTU PHPUnit 8.3.3 in CakePHP2.10 $ ./cake28/Console/cake l_test cake28/Test/Case/Model/CityTest.php Warning

    Error: include(PHPUnit/Autoload.php): failed to open stream: No such file or directory in [/var/www/lancers/cake28/Vendor/cakephp/cakephp/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php, line 178] Warning Error: include(): Failed opening 'PHPUnit/Autoload.php' for inclusion (include_path='/var/www/lancers/vendor/phpunit/phpunit:/var/www/lancers/cake28/Vendor/cakephp/cak ephp/lib:/var/www/lancers/cake28/Vendor/cakephp/cakephp/lib:.:/usr/share/pear:/usr/share/php') in [/var/www/lancers/cake28/Vendor/cakephp/cakephp/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php, line 178] Error: Please install PHPUnit framework v3.7 (http://www.phpunit.de) #0 /var/www/lancers/cake28/Vendor/cakephp/cakephp/lib/Cake/Console/ShellDispatcher.php(221): TestShell->initialize() #1 /var/www/lancers/cake28/Vendor/cakephp/cakephp/lib/Cake/Console/ShellDispatcher.php(66): ShellDispatcher->dispatch() #2 /var/www/lancers/cake28/Console/cake.php(56): ShellDispatcher::run(Array) #3 {main} $ •Execute by cake command
  54. $BLF'FTU PHPUnit 8.3.3 in CakePHP3 $ ./bin/phpunit --bootstrap config/bootstrap.php tests/TestCase/Model/Table/CitiesTableTest.php

    PHP Fatal error: Declaration of Cake¥TestSuite¥TestCase::setUp() must be compatible with PHPUnit¥Framework¥TestCase::setUp(): void in /var/www/lancers/vendor/cakephp/cakephp/src/TestSuite/TestCase.php on line 35 PHP Stack trace: PHP 1. {main}() /var/www/lancers/vendor/phpunit/phpunit/phpunit:0 PHP 2. PHPUnit¥TextUI¥Command::main() /var/www/lancers/vendor/phpunit/phpunit/phpunit:61 PHP 3. PHPUnit¥TextUI¥Command->run() … PHP 13. include() /var/www/lancers/vendor/composer/ClassLoader.php:444 $ •Execute by phpunit command ◦Requires return type declarations
  55. $BLF'FTU 55 SRE 1 Person App 3 Person App 1

    Person SRE 1 Person App SRE others 2017/2 Project start 2017/6 Nginx + PHP-FPM 2017/7 CakePHP1.3→2.8 start 2017/12 CakePHP Co-exist start 2018/2 2018/4 2018/5 2018/7 Batch, Admin start 2018/11 2018/12 Versionup Team App 1 Person SRE 2 Person