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

Drupal モジュール開発入門講座 第5回 サービス

Drupal モジュール開発入門講座 第5回 サービス

More Decks by アクイアジャパン Acquia Japan

Other Decks in Programming

Transcript

  1. ୈ5ճ αʔϏε Drupal Ϟδϡʔϧ ։ൃೖ໳ߨ࠲

  2. Drupal Ϟδϡʔϧ։ൃೖ໳ߨ࠲γϦʔζ – DrupalͰ͸ʮϞδϡʔϧʯͱݺ͹ΕΔɺখ͞ͳιϑτ΢ΣΞΛ࣮૷͢Δ͜ͱͰػೳ֦ுΛ ߦ͍·͢ɻຊ΢ΣϏφʔγϦʔζͰ͸ɺDrupalͷϞδϡʔϧ։ൃʹؔ͢Δجૅ஌ࣝΛղઆ ͠·͢ɻ – DrupalͷόοΫΤϯυपΓʹ͍ͭͯମܥతʹֶͼ͍ͨํɺࣗ෼ͰΧελϜϞδϡʔϧΛ࡞ ΕΔΑ͏ʹͳΓ͍ͨํɺΞΫΠΞೝఆσϕϩούʔࢼݧΛ໨ࢦ͢ํʹ͓͢͢ΊͰ͢ɻ –

    DrupalΛҰ௨Γૢ࡞ͨ͜͠ͱ͕͋ΔํΛର৅ͱ͍ͯ͠·͢ɻ·ͩDrupalΛ৮ͬͨ͜ͱ͕ແ ͍ํ͸ɺॳ৺ऀ޲͚DrupalνϡʔτϦΞϧಈըʢ೔ຊޠʣΛઌʹݟ͓ͯ͘ͱΑΓཧղ͕ਂ ·Γ·͢ɻ
  3. ΞδΣϯμ 1. DrupalͷαʔϏεͱDI(Dependency Injection ) 2. αʔϏεΛ࢖༻͢Δ 3. ར༻ՄೳͳαʔϏεΛݟ͚ͭΔ 4.

    ಠࣗͷαʔϏεΛ࡞੒͢Δ 5. ࿅श໰୊
  4. DrupalͷαʔϏεͱ DI(Dependency Injection)

  5. ΧελϜϞδϡʔϧͰ࣍ͷಈ࡞Λߦ͏ʹ͸ɺͲ͏͢Ε͹ྑ͍Ͱ͠ΐ͏ ͔ʁ – Կ͔Λ࣮ߦͨ࣌͠ʹɺαΠτʹεςʔλεϝοηʔδΛද͍ࣔͨ͠ – ݱࡏͷϩάΠϯϢʔβʔΛऔಘ͍ͨ͠ – Drupalͷσʔλϕʔεʹ઀ଓͯ͠஋Λऔಘ͍ͨ͠ – watchdogϩάʹॻ͖ࠐΈ͍ͨ

    – ຋༁͍ͨ͠ DrupalͰ͜ΕΒΛߦ͏ʹ͸Ͳ͏͢Δʁ
  6. DrupalͷαʔϏεͱ͸ – DrupalͷϞδϡʔϧ։ൃͰग़ͯ͘ΔʮαʔϏεʯͱ͍͏༻ޠ ͸ɺαʔϏείϯςφʹΑͬͯ؅ཧ͞ΕΔศརͳΦϒδΣΫτ Ͱ͢ɻ – ͜ͷ࢓૊Έ͕ఏڙ͞ΕΔ͜ͱͰDI(Dependency Injection)Λߦ ͏͜ͱ͕Ͱ͖ɺϓϩάϥϜΛૄ݁߹ʹ͢Δ͜ͱ͕Ͱ͖·͢ɻ

  7. DI(Dependency Injection)ͱ͸ʁ – ΦϒδΣΫτؒͷ݁߹౓ΛԼ͛ͯɺಠཱੑΛߴΊΔςΫχοΫ ͷҰͭͰ͢ɻ – ೔ຊޠͰ͸Α͘ʮґଘੑͷ஫ೖʯͱ༁͞Ε·͢ɻ – ΦϒδΣΫτࢦ޲ιϑτ΢ΣΞΞʔΩςΫνϟͰҰൠతʹ࢖༻ ͞ΕΔઃܭύλʔϯͱͯ͠Α͘஌ΒΕ͍ͯ·͢ɻ

  8. ͳͥDI͕ඞཁͳͷ͔ DIͷجຊతͳϝϦοτ͸ɺΫϥεͱͦͷґଘؔ܎͕ૄ݁߹ʹͳΔ͜ ͱͰ͢ɻDIΛ࣮ݱ͢Δ͜ͱͰɺґଘؔ܎͕ͲͷΑ͏ʹ࣮૷͞Ε͍ͯ Δ͔Λ஌Δඞཁ͕ͳ͘ͳΓɺϓϩάϥϜʹ࣍ͷϝϦοτ͕ੜ·Ε· ͢ɻ – ϓϩάϥϜͷ࠶ར༻ੑ – ୯ମςετ(PHPUnit )

    – อकੑ
  9. DIΛߦ͏ʹ͸ʁ – DI͸ಛஈπʔϧΛ࢖Θͳͯ͘΋࣮ݱͰ͖·͕͢ɺΞϓϦέʔγϣϯ͕େ ͖͘ͳΔͱେมʹͳΓ·͢ɻ – Ұൠతʹ͸DIίϯςφͱݺ͹ΕΔπʔϧ/ιϑτ΢ΣΞΛ༻͍ͯDIΛ࣮ݱ ͠·͢ɻ

  10. DIίϯςφͱ͸ – DIίϯςφͱ͸ɺDIΛ࣮ݱ͢ΔͨΊͷπʔϧ/ιϑτ΢ΣΞͰ͢ɻ – DIίϯςφΛ࢖͏͜ͱͰɺґଘੑͷ؅ཧΛίʔυʹهड़͢Δ͜ͱ ͳࣗ͘ಈతʹߦͬͯ͘Ε·͢ɻ – ֤ݴޠɺWebϑϨʔϜϫʔΫͰ༷ʑͳDIίϯςφ͕ଘࡏ͠·͢ɻ – PHPͰ͸ɺPSR-11ͰDIίϯςφ͕ඪ४Խ͞Ε͍ͯ·͢ɻ

    – DrupalͰ͸αʔϏείϯςφͰDIΛ࣮ݱͰ͖·͢ɻ
  11. αʔϏείϯςφͱ͸ – αʔϏείϯςφͱ͸ɺDrupalͰఏڙ͞ΕΔDIίϯςφͰ͢ɻ – SymfonyͷDependency InjectionίϯϙʔωϯτΛϕʔεʹ࣮૷͞Ε͍ͯ ·͢ɻ

  12. αʔϏεͱ͸ – αʔϏεͱ͸ɺศརͳػೳ͕ఏڙ͞Ε Δ೚ҙͷΦϒδΣΫτͰ͢ɻʢ㲈PHP Ϋϥεʣ – ֤αʔϏε͸*.services.ymlͰఆٛ͞Ε ·͢ɻ – αʔϏε͸αʔϏείϯςφΛհͯ͠

    ΞΫηεͰ͖·͢ɻ services: cache_context.ip: class: Drupal\Core\Cache\Context\IpCacheContext arguments: ['@request_stack'] tags: - { name: cache.context } cache_context.protocol_version: class: Drupal\Core\Cache\Context\ProtocolVersionCacheContext arguments: ['@request_stack'] tags: - { name: cache.context } cache_context.headers: class: Drupal\Core\Cache\Context\HeadersCacheContext arguments: ['@request_stack'] tags: - { name: cache.context } ...লུ... core.services.yml
  13. ༻ޠͷ੔ཧ – DI(Dependency Injection)͸ɺΦϒδΣΫτؒΛૄ݁߹ʹ͢ ΔςΫχοΫͰ͢ɻ – DIίϯςφ͸ɺDIΛ࣮ݱ͢ΔͨΊͷπʔϧͰ͢ɻ – αʔϏείϯςφ͸ɺDrupalͰఏڙ͞ΕΔDIίϯςφͰ͢ɻ –

    αʔϏε͸ɺศརͳػೳΛؚΉ೚ҙͷΫϥε/ΦϒδΣΫτ Ͱ͢ɻ
  14. αʔϏεΛ࢖༻͢Δ

  15. αʔϏεΛ࢖͏ํ๏ DrupalͷศརͳαʔϏεʹΞΫηε͢Δʹ͸ɺ࣍ͷ2ͭͷํ ๏͕༻ҙ͞Ε͍ͯ·͢ɻ 1. άϩʔόϧؔ਺Ͱ੩తʹݺͼग़͢ 2. DIΛར༻͢Δ

  16. άϩʔόϧؔ਺Ͱ੩తʹݺͼग़͢ άϩʔόϧͳ\DrupalΫϥεΛ࢖༻ͯ͠ɺ؆୯ʹαʔϏεͷΠϯελϯεΛऔಘͰ͖·͢ɻ ओʹखଓ͖తͳίʔυͰར༻Ͱ͖·͢ɻ \Drupal::service()ؔ਺ – ίϯςφ͔ΒαʔϏεΛऔಘ͢Δؔ਺͕༻ҙ͞Ε͍ͯ·͢ɻ – ྫɿ\Drupal::service(‘messenger’) ; ઐ༻ͷαʔϏε

    – සൟʹར༻͞ΕΔαʔϏε͸ɺ\Drupal͔Βಠࣗͷ੩తϝιου͕༻ҙ͞Ε͍ͯ·͢ɻ – ྫɿDrupal::messenger() ; – DrupalίΞʹ͸ɺઐ༻ͷ੩తϝιου͕໿50ݸଘࡏ͠·͢ɻ
  17. DIΛར༻͢Δ DIΛ࢖༻ͯ͠αʔϏεΛ஫ೖͰ͖·͢ɻ
 create()ϝιου – ίϯτϩʔϥʔɺϑΥʔϜɺϒϩοΫͳͲͷΫϥεͰ͸ɺcreate()ϝιουΛ࣮ ૷ͯ͠໨తͷαʔϏεΛ஫ೖͰ͖·͢ɻ αʔϏεҾ਺ – ίϯετϥΫλͱ *.services.ymlϑΝΠϧʹɺར༻͍ͨ͠αʔϏεΛࢦఆ͢Δ͜

    ͱͰ஫ೖͰ͖·͢ɻ ࠓճ͸ɺcreate()ϝιου Λར༻͢Δํ๏ͰDI͢Δํ ๏Λ͝঺հ͠·͢ɻ
  18. – ՄೳͰ͋Ε͹ɺৗʹDIΛ࢖༻ͯ͠αʔϏεΛΠϯελϯε Խ͢Δ͜ͱ͕ਪ঑͞Ε·͢ɻ – ྫɿίϯτϩʔϥʔΫϥεɺϑΥʔϜΫϥεͳͲɾɾ – ϑοΫؔ਺ͳͲखଓ͖తͳίʔυͷҰ෦Ͱ͸ɺάϩʔόϧ ؔ਺Λ࢖༻͢Δଞɺબ୒ͷ༨஍͕͋Γ·ͤΜɻ – ྫɿ*.moduleϑΝΠϧͳͲɾɾ

    ࢖͍෼͚
  19. ԋश ϑΥʔϜΫϥεͰαʔϏεΛར༻ͯ͠ΈΑ͏ʂ 1. άϩʔόϧؔ਺Ͱ੩తʹݺͼग़ͯ͠ΈΑ͏ 2. DIΛར༻ͯ͠ݺͼग़ͯ͠ΈΑ͏ ίʔυ͸ͪ͜Βˠ https://github.com/hmaruyama/my-drupal9-module/compare/vol5

  20. ར༻ՄೳͳαʔϏεΛݟ͚ͭΔ

  21. αʔϏε͸Ͳ͔͜Βఏڙ͞ΕΔʁ – ίΞ – ίϯτϦϏϡʔτϞδϡʔϧ – ΧελϜϞδϡʔϧ

  22. ར༻ՄೳͳαʔϏεΛݟ͚ͭΔํ๏ – api.drupal.org Λࢀর͢Δ – ίʔυϕʔε͔Β *.services.yml ϑΝΠϧΛ ୳͢ –

    DevelϞδϡʔϧΛ࢖༻͢Δ
  23. api.drupal.org Λࢀর͢Δ – api.drupal.orgͰ͸ɺServicesϖʔδ͔ΒDrupalίΞͷશͯͷαʔϏεͷҰཡΛݟΔ͜ͱ͕Ͱ͖ ·͢ɻ – ͜͜Ͱ͸ίΞͰఆٛ͞Ε͍ͯΔαʔϏεͷΈͰ͢ɻίϯτϦϏϡʔτϞδϡʔϧ΍ΧελϜϞ δϡʔϧͰఆٛ͞ΕͨαʔϏε͸͔͜͜ΒࢀরͰ͖·ͤΜɻ

  24. ίʔυϕʔε͔Β *.services.yml ϑΝΠϧΛ୳͢ – αʔϏε͸*.services.ymlϑΝΠϧͰఆٛ͞Ε͍ͯ·͢ɻ – ίΞͰ͸ɺcore.services.yml΍֤ίΞϞδϡʔϧͷ *.services.ymlϑΝΠϧΛݟΔ͜ͱͰαʔϏεΛ֬ೝͰ͖·͢ɻ – Drupalιʔείʔυ͔Β

    *.services.yml ϑΝΠϧΛ୳͢͜ͱ ͰɺDrupalαΠτͰར༻ՄೳͳαʔϏεΛݟ͚ͭΔ͜ͱ͕Ͱ͖ ·͢ɻ
  25. – ίϯτϦϏϡʔτϞδϡʔϧͷʮDevelʯϞδϡʔϧΛΠϯετʔϧ/༗ޮԽͯ͠ɺ/ devel/container/service ϖʔδʹΞΫηε͠·͢ɻ – αΠτͰར༻ՄೳͳશͯͷαʔϏεҰཡ͕֬ೝͰ͖·͢ɻʢίΞɺίϯτϦϏϡʔτ ϞδϡʔϧɺΧελϜϞδϡʔϧʣ DevelϞδϡʔϧΛ࢖༻͢Δ

  26. ར༻ՄೳͳαʔϏεΛݟ͚ͭΔํ๏ ·ͱΊ ࢀরઌ ίΞ ίϯτϦϏϡʔτ/ ΧελϜϞδϡʔϧ api.drupal.org ֎෦WebαΠτ ̋ ʔ

    *.services.yml ίʔυϕʔε ̋ ̋ DevelϞδϡʔϧ ର৅ͷDrupalαΠτͷ؅ཧը໘ ̋ ̋
  27. ಠࣗͷαʔϏεΛ࡞੒͢Δ

  28. ΧελϜϞδϡʔϧͰಠࣗͷαʔϏεΛ࡞੒͢Δํ๏ ࠶ར༻Ͱ͖ͦ͏ͳػೳ͸ɺಠࣗͷαʔϏεͱͯ͠੾Γग़͠ɺ ଞͷαʔϏεͱಉ༷ʹར༻͢Δ͜ͱ͕Ͱ͖·͢ɻ 1. srcσΟϨΫτϦ಺ʹɺ೚ҙͷPHPΫϥεΛ࡞੒ͯ͠αʔ ϏεΛ࣮૷͠·͢ɻ 2. *.sevices.ymlϑΝΠϧΛ࡞੒ͯ͠ɺ࡞੒ͨ͠αʔϏεΛ ఆٛ͠·͢ɻ

  29. ԋश ಠࣗͷαʔϏεΛఆٛͯ͠ɺར༻ͯ͠ΈΑ͏ʂ 1. ϞδϡʔϧσΟϨΫτϦʹ src/Service/MaruchaService.php ϑΝΠϧΛ࡞੒͢Δ 2. αʔϏεΫϥεΛ࣮૷͢Δ 3. ϞδϡʔϧσΟϨΫτϦ௚Լʹ

    marucha.services.ymlϑΝΠϧ Λ࡞੒͠ɺαʔϏεΛఆٛ͢Δ 4. ఆٛͨ͠αʔϏεΛར༻͢Δ ίʔυ͸ͪ͜Βˠ https://github.com/hmaruyama/my-drupal9-module/compare/vol5
  30. ࿅श໰୊

  31. ࿅श໰୊1 DrupalͷʮαʔϏεʯʹ͍ͭͯਖ਼͍͠هड़Λબ୒͍ͯͩ͘͠͞ɻ 1. ศརͳػೳΛؚΜͩ೚ҙͷPHPΦϒδΣΫτɻ 2. Drupalͷಈ࡞Λมߋ͢ΔPHPؔ਺ɻ 3. ʮίϯςϯπʯ΋͘͠͸ʮߏ੒ʯͷɺӬଓతͳετϨʔδʹ࢖༻͞ΕΔΦ ϒδΣΫτɻ

  32. ࿅श໰୊1 DrupalͷʮαʔϏεʯʹ͍ͭͯਖ਼͍͠هड़Λબ୒͍ͯͩ͘͠͞ɻ 1. ศརͳػೳΛؚΜͩ೚ҙͷPHPΦϒδΣΫτɻ 2. Drupalͷಈ࡞Λมߋ͢ΔPHPؔ਺ɻ 3. ʮίϯςϯπʯ΋͘͠͸ʮߏ੒ʯͷɺӬଓతͳετϨʔδʹ࢖༻͞ΕΔΦ ϒδΣΫτɻ

  33. ࿅श໰୊2 DrupalͷʮαʔϏείϯςφʯʹ͍ͭͯਖ਼͍͠هड़Λબ୒͍ͯͩ͘͠͞ɻ 1. Drupalݻ༗ͷσβΠϯύλʔϯΛ࣮ݱ͢ΔͨΊͷπʔϧɻ 2. ґଘੑͷ஫ೖΛߦ͏ͨΊͷπʔϧɻ 3. Drupal͕ड͚औͬͨϦΫΤετʹରͯ͠ɺͲͷίϯτϩʔϥʔʗΞΫ γϣϯΛ࣮ߦ͢Δ͔ΛϚοϐϯά͢Δ࢓૊Έɻ

  34. ࿅श໰୊2 DrupalͷʮαʔϏείϯςφʯʹ͍ͭͯਖ਼͍͠هड़Λબ୒͍ͯͩ͘͠͞ɻ 1. Drupalݻ༗ͷσβΠϯύλʔϯΛ࣮ݱ͢ΔͨΊͷπʔϧɻ 2. ґଘੑͷ஫ೖΛߦ͏ͨΊͷπʔϧɻ 3. Drupal͕ड͚औͬͨϦΫΤετʹରͯ͠ɺͲͷίϯτϩʔϥʔʗΞΫ γϣϯΛ࣮ߦ͢Δ͔ΛϚοϐϯά͢Δ࢓૊Έɻ

  35. ຊ೔ͷΞδΣϯμ͸ऴྃ

  36. ΞδΣϯμ 1. DrupalͷαʔϏεͱDI(Dependency Injection ) 2. αʔϏεΛ࢖༻͢Δ 3. ར༻ՄೳͳαʔϏεΛݟ͚ͭΔ 4.

    ಠࣗͷαʔϏεΛ࡞੒͢Δ 5. ࿅श໰୊
  37. None