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

Composerって何?
どう動くの?
読んでみました!  / lets-read-compo...

Composerって何?
どう動くの?
読んでみました!  / lets-read-composer!

 PHP Conference Japan 2019での発表資料(25分発表エディション)です

https://fortee.jp/phpcon-2019/proposal/80fe2e57-a35d-48cd-9c78-4af5000cbeac

hideki kinjyo

December 01, 2019
Tweet

More Decks by hideki kinjyo

Other Decks in Programming

Transcript

  1. © - BASE, Inc. ⾃⼰紹介 • ۚ৓लथ / ͖Μ͡ΐ͏ͻͰ͖ •

    GitHub: @o0h / Twitter: @o0h_ • BASE, Inc. (2019.10) / ج൫νʔϜ • ex Connehito Inc.(2015.04-) • ޷͖ͳFW͸CakePHP • ΞΠίϯ͸
 ඒຯ͍͠ࡪ਌ࢠကͷࣸਅͰ͢
  2. © - BASE, Inc. Composerͱ͸Կ͔ʁ =>Composer͸ύοέʔδ؅ཧγεςϜ ※ެࣜαΠτͰ͸ʮComposer is a tool

    for dependency management in PHP.ʯͱઆ໌͍ͯ͠·͢ɻ
 Introduction - Composer https://getcomposer.org/doc/00-intro.md
  3. パッケージ管理システム? © - BASE, Inc. • ϨϙδτϦͷߪಡ • ύοέʔδͷΠϯετʔϧɾ࡟আ •

    ґଘؔ܎ͷղܾ • ઃఆ؅ཧ https://ja.wikipedia.org/wiki/ύοέʔδ؅ཧγεςϜ
  4. パッケージ管理とComposer © - BASE, Inc. • Packagistͱ΍ΓͱΓͨ͠Γ
 <= ϨϙδτϦͷߪಡ •

    GitHub͔Βguzzlehttp/clientΛऔಘͯ͠഑ஔͨ͠Γ
 <= ύοέʔδͷΠϯετʔϧɾ࡟আ • ^1.2ͱ~1.2ʹର͠1.3.0Ͱ͸ͳ͘1.2.9ΛऔͬͨΓ
 <= ґଘؔ܎ͷղܾ • composer.jsonͷத਎Λղऍͨ͠Γ
 <= ઃఆ؅ཧ

  5. 抽象の解釈としての実装 © - BASE, Inc. • ϨϙδτϦͷߪಡ • ύοέʔδͷΠϯετʔϧɾ࡟আ •

    ґଘؔ܎ͷղܾ • ઃఆ؅ཧ ݱ࣮ɾந৅ ιϑτ΢ΣΞɾ࣮૷ • \Composer\Repository\ • \Composer\Installer\ • \Composer\Package\ • \Composer\DependencyResolver\
  6. \Composer\Repository\ © - BASE, Inc. • ϨϙδτϦ৘ใͷ؅ཧ –repositories[].typeͷ෦෼ʹରԠ͢ΔΠϝʔδ • ex:

    –ComposerRepository: packagistϨϙ –VcsRepository: GitHub΍SVNͳͲͷϨϙ –PathRepository: ϩʔΧϧϑΝΠϧ Repositories - Composer https://getcomposer.org/doc/05-repositories.md
  7. \Composer\Installer\ © - BASE, Inc. • Πϯετʔϧͷ؅ཧ –ύοέʔδDLͷىಈͱ͔औಘϑΝΠϧͷ഑ஔͱ͔ • ex:

    –LibraryInstaller: packageͷΠϯετʔϧ –ProjectInstaller: root projectͷΠϯετʔϧ –PluginInstaller: plugin packageͷΠϯετʔϧ –PearInstaller: pear packageͷΠϯετʔϧ
  8. \Composer\Package\ © - BASE, Inc. • ύοέʔδ৘ใͷ؅ཧ –composer.jsonΛղऍͨ͠Γ • ex:

    –RootPackage: root projectͷ؅ཧ –Locker: composer.lockͷ؅ཧ –Link: packageؒͷ(ґଘ)ؔ܎
  9. \Composer\DependencyResolver\ © - BASE, Inc. • ґଘؔ܎ͷ؅ཧ –ෳ਺Ϩϙ͔Βύοέʔδ৘ใΛ౷߹ͨ͠Γ –install͢Δ͔ʁupdate͢Δ͔?ͦͷ··͔?ΛܾΊͨΓ •

    ex: –Solver: ґଘؔ܎Λ੔ཧ͠ύοέʔδͷѻ͍ΛܾΊΔ –Pool: ϨϙδτϦ΍ύοέʔδ৘ใͷू໿ –Operation: install,update,removeͳͲͷૢ࡞Λࣔ͢
  10. 初めてのComposer: まとめ © - BASE, Inc. • ύοέʔδ؅ཧʹඞཁͳཁૉ –ϨϙδτϦ,Πϯετʔϥ,ґଘղܾ,ઃఆ؅ཧ •

    Composer΋ύοέʔδ؅ཧͷʮ࣮૷ʯ –্هͷ4֓೦ʹ͍ͭͯ΋ɺ
 ͦΕͧΕରԠ͢Δnamespace͕ଘࡏ͢Δ
  11. symfony/console © - BASE, Inc. • Composer͸symfony/ consoleΛར༻ • CLIπʔϧΛ؆୯ʹ࡞Ε

    Δύοέʔδ • CommandΫϥεͱ͍͏ ୯ҐΛ࡞Γ • Applicationͷதʹొ࿥ ͢Δ ※ symfony/consoleʹ͍ͭͯ͸ίνϥͷهࣄͳͲΛͲ͏ͦ
 Container(PSR-11)ͱsymfony/consoleͰ؆୯ͳίϚϯυϥΠϯΞϓϦέʔγϣϯΛ࡞ͬͯΈΑ͏ - istyle Tech Blog
 https://techblog.istyle.co.jp/archives/97
  12. RootPackage © - BASE, Inc. • ComposerͷੈքͰ͸ɺ
 1ͭͷcomposer.jsonʹରԠͯ͠
 1ͭͷPackageΠϯελϯε͕࡞ΕΔ •

    ͦͷதͰɺ
 PJࣗ਎ = rootͱͳΔPackage͸
 RootPackageΠϯελϯε͕࡞ΒΕΔ
  13. Package © - BASE, Inc. • ͭ·Γ `composer install` ࣮ߦͱ͸


    RootPackageͷґଘੑͷղܾͱݴ͑Δ • Packageͷதʹ͸ɺࣗ਎ͷόʔδϣϯ৘ ใ΍ґଘ͢Δύοέʔδͷ৘ใetcؚ͕ ·Ε͍ͯΔ
  14. レポジトリ情報の読み込み © - BASE, Inc. • ࢦఆ͞Εͨ΋ͷ͕ଘࡏ͢Δ͔ʁ
 Ͳ͜ʹ͋Δ͔ʁΛ஌Βͳ͍ͱ
 ύοέʔδͷDL͕Ͱ͖ͳ͍ •

    ϨϙδτϦ͕ͦΕΛ؅ཧ͢Δɻ
 ʮԿͷύοέʔδ͕༗Δ͔ʯͷ৘ใΛ
 ೺Ѳ͠ɺϢʔβʔʹఏڙ͢Δ
  15. Repository(Interface) © - BASE, Inc. • ComposerͷੈքͰ͸ɺ
 ϨϙδτϦ͸RepositoryInterfaceͷ࣮૷ Ͱදݱ͞ΕΔ •

    Repository͸ʮύοέʔδ৘ใͷҰཡʯ ΛಡΈࠐΈɺ؅ཧ͢Δ • ʮऔಘʯ΍ʮݕࡧʯͷػೳΛఏڙ͢Δ
  16. composer installͰ͸ɺ
 ϩʔΧϧٴͼϦϞʔτͷ৘ใ͕ؔΘΔ 基本的なレポジトリ3つ © - BASE, Inc. ϩʔΧϧ InstalledFilesystemRepo

    PlatformRepo (΄΅)
 vendorσΟϨΫτϦ PHPͷγεςϜ৘ใ ϦϞʔτ ComposerRepo Packagist
  17. ComposerRepository © - BASE, Inc. • Packagist্ͷύοέʔδͷ؅ཧ –಺༰͸ `https://packagist.org/packages.json` •

    indexϑΝΠϧ –͜͜ʹه͞Ε͍ͯΔιʔεΛϑΣον͢Δ • ࠷ॳ͸ʮύοέʔδ໊ͷҰཡʯͷΈ –ͨͩ͠ɺͦΕࣗମ΋஗Ԇॲཧ͞ΕΔ –ղܾ͕ඞཁͳύοέʔδ͕ग़͖ͯͨ࣌ʹ
 ύοέʔδݸผͷ৘ใΛऔΓʹߦ͘
  18. installの事前処理: まとめ © - BASE, Inc. • PJ৘ใ͸RootPackageͱͯ͠ΠϯελϯεԽ ͞ΕΔ •

    installʹ͓͍ͯ͸ϩʔΧϧͱϦϞʔτͷ Repository͕ಡΈࠐ·ΕΔ –InstalledFilesystem, Platform, Composer • ಡΈࠐ·ΕͨRepository͸Poolʹ௥Ճ͞Εɺ ౷߹తʹղܾ͢Δͷʹར༻͞ΕΔ
  19. 更新内容の算出(依存性の整理)? © - BASE, Inc. • (rootΛؚΉ)ෳ਺ͷύοέʔδ͕ɺ
 ͦΕͧΕʹෳ਺ͷύοέʔδʹґଘ͍ͯ͠Δ • ͦΕΒͷ৘ใΛ౷߹͢Δඞཁ͕͋Δ

    –֤ύοέʔδ͕Կʹґଘ͍ͯ͠Δ͔Λ࠶ؼతʹ೺Ѳ ͢Δ –ચ͍ग़͞Εͨґଘʹ͍ͭͯɺڞ༗Ͱ͖Δ΋ͷ΍িಥ ͕ͳ͍͔Λௐ΂Δ • ࠷ऴతʹʮԿ͕΄͍͔͠ʯΛ೺Ѳ͢Δ
  20. PackageとRequest © - BASE, Inc. • Package->requiresͷ৘ใ Λ࿈Ͷ͍ͯ͘ʮཉ͍͠ ෺ʯͷΩϡʔΛଋͶΔΫ ϥε͕ཉ͍͠

    • ͦΕ͕RequestΫϥε • require1ߦ͝ͱʹjob͕1 ݸͰ͖ΔΠϝʔδ(=job) Package phpunit/phpunit:^8.4 Request jobs <array>
  21. RequestとJob © - BASE, Inc. Request jobs <array> job͸୯७ͳ࿈૝഑ྻ •

    jobͷ࣋ͭ৘ใ –cmd • install|update|remove –packageName –constraint • ver.ࢦఆ
  22. RequestとSolver © - BASE, Inc. • Solver::solve()ʹ
 RequestΛ౉͢ • ͜ͷதͰڝ߹৚݅ɾڞ௨

    ෦෼͕ߟྀ͞ΕΔ • jobsΛݩʹɺ
 ʮԿΛ͢Ε͹͍͍͔ʯΛ Installerʹฦͯ͘͠ΕΔ Solver Request Operation[]
  23. Operation © - BASE, Inc. • job͕ղܾ͞Εɺ࠷ऴ తʹߋ৽ૢ࡞͕ඞཁͳ ΋ͷ͚͕ͩฦ͞ΕΔ •

    ྫ͑͹”Nothing to install or update”ͳ Βɺ`solve()`ͷฦΓ஋ ͸ۭ഑ྻ Solver Operation jobs <array> Operation jobs <array> Operation
  24. 実際にパッケージをDLしてくる © - BASE, Inc. • ύοέʔδͷΠϯετʔϧ͸ɺ InstallationManager::execute()ʹΑͬͯ ߦΘΕΔ •

    औಘݩ͸Package͕஌͍ͬͯΔ • DLઌΛܾఆ͢Δͷ͸Installerͷ࢓ࣄ • DLΛ͢Δͷ͸Downloaderͷ࢓ࣄ
  25. 流れ(ざっくり)② © - BASE, Inc. DownloadManager package͔ΒtypeʹԠͨ͡
 downloaderΛऔಘ package, downloadPath

    package, downloadPath LibraryInstaller Downloader packageͷ৘ใΛಡΈऔͬͯ
 downloadPathʹల։
  26. .lockファイルに載せる情報は? © - BASE, Inc. • .lockϑΝΠϧ͸ɺʮίί·ͰʹΠϯε τʔϧͨ͠΋ͷʯͷࣸ૾ –औಘ͞ΕͨPackage: localRepoͷσʔλ

    –ϓϥοτϑΥʔϜͷ৘ใ: composer.json + PlatformReqRepoͷσʔλ –ͦͷଞɺstabilityϑϥάͳͲ: RootPackageͷ σʔλ
  27. 必要な情報を合成してdumpする © - BASE, Inc. • Composer\Package\Locker –ʮ.lockϑΝΠϧͷΠϯελϯεʯͳΠϝʔδ • Locker::setLockData()ʹ


    ඞཁͳ৘ใΛ౉ͨ͠Βɺ
 jsonσʔλΛ૊ΈཱͯͯϑΝΠϧʹग़ྗ ͜͜·ͰͷRepo/PackageͷΠϝʔδΛ௫Ί͍ͯΕ͹ɺ
 .lockσʔλͷࢉग़͸γϯϓϧͰ͢Ͷʂ