ZF2: Writing Service Components

43
ZF2: Writing Service Components Mike Willbanks | Barnes & Noble Zend Webinar: Thursday, May 2 nd 2013

description

Writing Service Components for Zend Framework 2.

Transcript of ZF2: Writing Service Components

Page 1: ZF2: Writing Service Components

ZF2: Writing Service Components Mike Willbanks | Barnes & Noble

Zend Webinar: Thursday, May 2nd 2013

Page 2: ZF2: Writing Service Components

2

Housekeeping…

• Me  Sr. Web Architect Manager at NOOK Developer

 Open Source Contributor

 Where you can find me:

•  Twitter: mwillbanks G+: Mike Willbanks

•  IRC (freenode): mwillbanks Blog: http://blog.digitalstruct.com

•  GitHub: https://github.com/mwillbanks

Page 3: ZF2: Writing Service Components

3

Agenda

• Background

• ZF2 Service Components

• Writing a Service Component

•  Incorporating your Service Component

Page 4: ZF2: Writing Service Components

Background… Why Service Components

Page 5: ZF2: Writing Service Components

5

A Definition

ZF 1 contained Zend_Service_* components

Page 6: ZF2: Writing Service Components

6

Zend_Service_*

Components were included in the core distribution

Page 7: ZF2: Writing Service Components

7

Zend_Service_*

Make it easy for people to have it available…

Page 8: ZF2: Writing Service Components

8

1.11.13 Release

Caused several issues

Page 9: ZF2: Writing Service Components

9

Started Getting Messy…

Page 10: ZF2: Writing Service Components

Service Component Overview Goals, Lifecycle, and more.

Page 11: ZF2: Writing Service Components

11

RFC March 2012 “RFC Service Components”

Removal of ZF2 Zend\Service namespace into a separate namespace (ZendService) and under their own GitHub project.

Page 12: ZF2: Writing Service Components

Goals of ZF2 Service Components

• Ability to version separately from core framework

• Easier to leverage outside of a ZF context

• Encourage service providers to further contribute

Page 13: ZF2: Writing Service Components

Services for Contribution (Official Services)

• Independently versioned

• Dependencies on framework versions (2.*, 2.0.*, 2.0.1)

• Maintain dependencies by specific packages

• Must follow ZF coding standards

• Must be unique

 A service that does the same thing should not already exist!

Page 14: ZF2: Writing Service Components

14

Discuss on Contributor Mailing

List

CR Team Review Fork Repo

Build ComponentIRC Vote(Meeting Agenda)

Publish!

The Service Component Lifecycle

Page 15: ZF2: Writing Service Components

15

Published ZendService Packages

A published package for ZendService components exist on packages.zendframework.com.

Page 16: ZF2: Writing Service Components

Service Component Maintenance

• Must be maintained for the duration of major versions

 Exceptions must be noted in the ChangeLog

 Component should only state dependency on minor versions

• Maintainers must attempt at all times to keep compatibility with the latest version

 If unable to maintain, actively recruit, if still unable ZF or CR team will make a recommendation on the component.

Page 17: ZF2: Writing Service Components

Writing a Service Component

By example of ZendService\Google\Gcm

Page 18: ZF2: Writing Service Components

General Information

• Service Components are really just like framework libraries

 However, the namespace implies 3rd party integrations.

 They are also organized like the framework.

• Service Components should be reusable for other developers

 Write it out based on the API and not just what you need.

• Create reasonable dependencies

 Zend\Http and Zend\Stdlib being most common.

Page 19: ZF2: Writing Service Components

19

Why Not Modules?

Page 20: ZF2: Writing Service Components

Why Not Modules?

• Modules are more specifically for ZF2 Applications

• Service Components are reusable libraries for any code base.

• Base Rule

 If it involves the MVC; it should more than likely be a module.

Page 21: ZF2: Writing Service Components

21

Starting off…

The file structure of a Service Component.

Page 22: ZF2: Writing Service Components

22

ZendService Components License

https://raw.github.com/zendframework/zf2/master/LICENSE.txt

Page 23: ZF2: Writing Service Components

23

Composer Stub

Main information for your specific library.

Page 24: ZF2: Writing Service Components

24

Composer Stub

Ensure you handle dependencies.

Page 25: ZF2: Writing Service Components

Unit Testing

• Quick Start

 Clone an existing Service Component (Currently no skeleton)

 Copy over files from the tests directory: _autoload.php, Bootstrap.php, phpunit.xml.dist, TestConfiguration.php.dist, TestConfiguration.php.travis

• You will need to customize

 phpunit.xml.dist – Change out the unit test name.

 TestConfiguration.php.dist – Configure your constants and configuration.

Page 26: ZF2: Writing Service Components

Unit Testing

• Tests should contain proper name spacing

 ZendServiceTest\Google\Gcm

• Files you need should be located in an _files directory inside the test area of your component

 tests/ZendService/Google/Gcm/_files

• Write unit tests like normal!

Page 27: ZF2: Writing Service Components

Library Code!

• The guts of the library is what most of us are familiar with

• Write your library inside of your compliant directory

 library/ZendService/Google/Gcm

• Ensure proper namespacing

 ZendService\Google\Gcm

• Attempt to follow great naming so that it makes sense!

Page 28: ZF2: Writing Service Components

Coding Standards

• Remember to make use of the coding standards!

 Docblocks

 Import the proper packages from their respective namespaces

 Read the coding standards doc:

• http://framework.zend.com/wiki/display/ZFDEV2/Coding+Standards

• Mainly; PSR-0, PSR-1, PSR-2 (there are slight differences)

Page 29: ZF2: Writing Service Components

Best Practices

• Follow conventions

 PSR-0/1/2, file locations, options classes

• Hard dependencies

 Use the constructor!

 Only set dependencies for items you require!

• Write Tests

 Hook into Travis-CI, go for 100% code coverage

• Discoverability

 Put the component on Packagist, submit it for inclusion to ZendService.

Page 30: ZF2: Writing Service Components

Documentation

• The most dreaded part of the job…

• All of the documentation is in the “zf2-documentation” project under the “zendframework” github organization.

 This will likely change for services in the future.

• Fork the project

• Create a feature branch: feature/service-google-gcm

• Write your documentation

• Submit a PR

Page 31: ZF2: Writing Service Components

31

Documentation is built out of reStructuredText

Yes, Docbook has gone away; so fear not!

Page 32: ZF2: Writing Service Components

32

Language Files

Always start with English first aka “en” as it is the default in the event a translation is missing.

Page 33: ZF2: Writing Service Components

33

Modify docs/language/en/index.rst to add in your new service

Having a table of contents is a good thing!

Page 34: ZF2: Writing Service Components

34

Modify docs/language/en/index.rst to add in your new service

Follow the convention!

Page 35: ZF2: Writing Service Components

35

Write Away!

docs/language/en/modules/zendservice.google.gcm.rst or the like

Page 36: ZF2: Writing Service Components

36

Seeing your Documentation

• Install the proper tools

 apt-get install python-setuptools python-pygments

 easy_install -U Sphinx

• Enter the docs/ directory

• Run: make html

Page 37: ZF2: Writing Service Components

37

Your documentation will now be in:

docs/_build/html

Review for errors before sending the PR

Page 38: ZF2: Writing Service Components

Integrating your Service Add the module to the composer configuration

Add in potential configuration

Setup the service manager

Fetch it inside of a controller

Page 39: ZF2: Writing Service Components

39

Add to your composer.json!

Followed by “php composer.phar install”

Page 40: ZF2: Writing Service Components

40

Handling Configuration

Likely in /path/to/zf2app/config/autoload/local.php

Page 41: ZF2: Writing Service Components

41

Service Manager Setup

Creating a simple factory to get a configured Client.

Page 42: ZF2: Writing Service Components

42

Leveraging your Service Factory in a Controller

We can easily fetch it now when we have a ServiceLocator.

Page 43: ZF2: Writing Service Components

Questions? These slides will be posted to SlideShare & SpeakerDeck.

 SpeakerDeck: http://speakerdeck.com/u/mwillbanks

 Slideshare: http://www.slideshare.net/mwillbanks

 Twitter: mwillbanks

 G+: Mike Willbanks

 IRC (freenode): mwillbanks

 Blog: http://blog.digitalstruct.com

 GitHub: https://github.com/mwillbanks