create a php library the right way

177
Create a PHP Library the Right way By Christian Varela @gabriel0702 [email protected] https://joind.in/talk/de19a 1

Upload: christian-varela

Post on 22-Jan-2018

166 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Create a PHP Library the right way

Create a PHP Library the Right way

By Christian Varela @gabriel0702

[email protected]

https://joind.in/talk/de19a1

Page 3: Create a PHP Library the right way

Requirements

3

https://goo.gl/StPczG

Page 4: Create a PHP Library the right way

Christian Varela

➤ I have a wife and 3 daughters ➤ I am from Mexico ➤ Master Degree in Computer Science ➤ 13 years programming with PHP ➤ I live in Miami ➤ I created Conqueror Soft Inc ➤ I play guitar and piano

4

Page 5: Create a PHP Library the right way

5

Page 6: Create a PHP Library the right way

Conqueror Soft will take your business to the next Level!

6

[email protected]

facebook.com/conquerorsoft

Page 7: Create a PHP Library the right way

What is this session about?

Have you found yourself wondering how to take advantage of what you have developed in the past for current or future projects? Are you tired of copying/pasting then adapting from your previous projects to the new ones? Start developing for the future and contribute to others by developing libraries and sharing them for use. Where do you start? You’ll be guided through this tutorial step by step to include security, tests and all the factors you need to consider when building a library.

7

Page 8: Create a PHP Library the right way

Library / Package

A Software Library is a collection of methods, functions, classes, etc, that provides a particular functionality to be used or reused partially or in whole as part of a more complex system.

It is independent enough to be integrated as a functional block or module along with other libraries to achieve a common goal.

It can be used by multiple systems that have no connections or relations between them.

8

Page 9: Create a PHP Library the right way

9

Page 10: Create a PHP Library the right way

Why should I create libraries?

1. They provide reusability

2. They can help open source

3. They are modular in functionality context

4. They are easier to maintain

10

Page 11: Create a PHP Library the right way

Web application

A web application is an application running in the web and that provides a solution to a particular problem.

The application can have modules, programs, configuration files, etc, all connected and related in a way that each piece provides a specific purpose and that all together accomplish a common goal.

11

Page 12: Create a PHP Library the right way

Web application

1. Served by a server

2. Requested by a user (through a browser)

3. Can interact with DBs

4. Use third party libraries

5. Use private libraries

6. Has configuration files

12

Page 13: Create a PHP Library the right way

Web application

13

Web Application

Web Server

Application Server

DB Server

Internal dependencies

External dependencies

Configuration files

Page 14: Create a PHP Library the right way

14

Page 15: Create a PHP Library the right way

15

Page 16: Create a PHP Library the right way

How the web application knows about its dependencies?

16

Page 17: Create a PHP Library the right way

17

Web Application

Web Server

Application Server

DB Server

Internal dependencies

External dependencies

Configuration files

Dependencies!?!?

composer.json

Page 18: Create a PHP Library the right way

COMPOSERDependency manager

18

Page 19: Create a PHP Library the right way

Composer

1. Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

2. Enables you to declare the libraries you depend on.

3. Finds out which versions of which packages can and need to be installed, and installs them

19

Page 20: Create a PHP Library the right way

composer.json

1. This is all you need to specify dependencies

2. This file is used when creating libraries and / or projects

3. "require" key specifies which packages your project needs

4. "repositories" key specifies where to find those packages

20

Page 21: Create a PHP Library the right way

composer.json keys (most popular)

1. name

2. description

3. type

4. keywords

5. homepage

6. authors

7. support

8. require

9. require-dev

10.suggest

11.autoload

12.autoload-dev

13.repositories

14.scripts

21

Page 22: Create a PHP Library the right way

Where composer get the packages from?

22

Page 23: Create a PHP Library the right way

23

Packages!?!?

packagist.org packagist.com custom repositories

Page 24: Create a PHP Library the right way

PACKAGIST.ORGComposer repository

24

Page 25: Create a PHP Library the right way

packagist.org

Packagist is the main Composer repository. A Composer repository is basically a package source: a place where you can get packages from. Packagist aims to be the central repository that everybody uses. This means that you can automatically require any package that is available there, without further specifying where Composer should look for the package.

25

Page 26: Create a PHP Library the right way

PACKAGIST.COMComposer repository

26

Page 27: Create a PHP Library the right way

packagist.com

Private Packagist is a commercial package hosting product offering professional support and web based management of private and public packages, and granular access permissions. Private Packagist provides mirroring for packages' zip files which makes installs faster and independent from third party systems - e.g. you can deploy even if GitHub is down because your zip files are mirrored.

27

Page 28: Create a PHP Library the right way

CUSTOM REPOSITORIES

From different VCS

28

Page 29: Create a PHP Library the right way

Custom repositoriesThe following repository types are supported:

1. composer: A Composer repository is simply a packages.json file served via the network (HTTP, FTP, SSH), that contains a list of composer.json objects with additional dist and/or sourceinformation. The packages.json file is loaded using a PHP stream. You can set extra options on that stream using the options parameter.

2. vcs: The version control system repository can fetch packages from git, svn, fossil and hg repositories.

3. pear: With this you can import any pear repository into your Composer project.

4. package: If you depend on a project that does not have any support for composer whatsoever you can define the package inline using a package repository. You basically just inline the composer.json object.

29

Page 30: Create a PHP Library the right way

Platform packages

1. Required system packages

2. Not installed by composer

3. Examples:

1. php

2. php extensions (ext-gd)

3. php libraries (lib-curl)

30

Page 31: Create a PHP Library the right way

Autoloading

1. Uses "autoload" key and PRS-4

2. vendor/autoload.php is generated by Composer

3. autoload.php is regenerated with composer dump-autoload

31

Page 32: Create a PHP Library the right way

PSR-4

1. PHP Standard Recommendation 4

2. describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be autoloaded according to the specification.

32

Page 33: Create a PHP Library the right way

Packages versioning

33

Page 34: Create a PHP Library the right way

Versioning (when developing)

1. They can be handled manually in composer.json

2. They can be handled using a Version Control System (VCS, this is recommended)

1. By using branches

2. By using tags (this is recommended)

3. Use a consistent versioning standard, like SemVer.org (this is recommended)

34

Page 35: Create a PHP Library the right way

Versioning (when installing dependencies)

1. Use constraints

1. Exact version (1.2.3)

2. Version range (>=2.0 <3.0)

3. with hyphen (1.1.0 - 1.2.0, equals to >=1.1.0 <= 1.2.0)

4. with wildcard (1.1.*, equals to >=1.1 <1.2)

5. with tilde (~1.3.1, equals to >=1.3.1 <1.4.0)

6. with caret (^0.3, equals to >=0.3 <0.4.0)

35

Page 36: Create a PHP Library the right way

SemVer

Given a version number MAJOR.MINOR.PATCH, increment the:

1. MAJOR version when you make incompatible API changes,

2. MINOR version when you add functionality in a backwards-compatible manner, and

3. PATCH version when you make backwards-compatible bug fixes.

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

http://semver.org/

36

Page 37: Create a PHP Library the right way

License for your library

37

Page 38: Create a PHP Library the right way

License for your library

38https://choosealicense.com/

Page 39: Create a PHP Library the right way

Minimum-stability options

1. dev

2. alpha

3. beta

4. RC

5. stable

39

Page 40: Create a PHP Library the right way

Code Quality

40

Page 41: Create a PHP Library the right way

PSR-2

1. This guide extends and expands on PSR-1, the basic coding standard.

2. The intent of this guide is to reduce cognitive friction when scanning code from different authors. It does so by enumerating a shared set of rules and expectations about how to format PHP code.

3. it helps to have one set of guidelines to be used among all those projects.

4. There are tools to help to comply with this (php code sniffer is recommended)

41http://www.php-fig.org/psr/psr-2/

Page 42: Create a PHP Library the right way

PHP Code Sniffer

PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.

42

Page 43: Create a PHP Library the right way

Changelog

43

Page 44: Create a PHP Library the right way

Changelog

1. it is a file which contains a curated, chronologically ordered list of notable changes for each version of a project.

2. it makes it easier for users and contributors to see precisely what notable changes have been made between each release (or version) of the project.

44

Page 45: Create a PHP Library the right way

Changelog rules

1. There should be an entry for every single version.

2. Same types of changes should be grouped

3. Versions and sections should be linkable

4. The latest version comes first

5. The release date of each versions is displayed

6. SemVer as versioning (recommended)

45

Page 46: Create a PHP Library the right way

Changelog changes

1. Added for new features.

2. Changed for changes in existing functionality.

3. Deprecated for soon-to-be removed features.

4. Removed for now removed features.

5. Fixed for any bug fixes.

6. Security in case of vulnerabilities.

46

Page 47: Create a PHP Library the right way

Continuos Integration tools

47

Page 48: Create a PHP Library the right way

Travis CI prerequisites

1. GitHub login

2. Project hosted as a repository on GitHub

3. Working code in your project

4. Working build or test script

5. Travis CI .org for public repositories

6. Travis CI .com for private repositories

48

Page 49: Create a PHP Library the right way

Travis CI

1. Sign in to Travis using GitHub account

2. Activate GitHub repositories

3. Add a .travis.yml file

4. Add the .travis.yml file to git, commit and push, to trigger a Travis CI build

5. Check the build status page to see if your build passes or fails

6. Github will have the service in the web hooks settings.

49

Page 50: Create a PHP Library the right way

Scrutinizer CI

1. Sign in to Scrutinizer using GitHub account

2. Activate GitHub repositories

3. Add a .scrutinizer.yml file

4. Add the .scrutinizer.yml file to git, commit and push, to trigger a Scrutinizer CI build

5. Check the build status page to see code quality and coverage

6. Github will have the service in the web hooks settings.

50

Page 51: Create a PHP Library the right way

Composer install

51

Composer install

composer.json? Yes composer.lock? Yes files match? Yes Install libraries from

composer.lock

Error

NoInstall libraries

from composer.json

No

Error

No

Generate composer.lock

Page 52: Create a PHP Library the right way

Composer update

52

Composer install

composer.json? Yes composer.lock? Yes files match? Yes Install most recent versions

Error

NoInstall libraries

from composer.json

No

Error

No

Generate composer.lock

Generate composer.lock

Page 53: Create a PHP Library the right way

Exercise 1. Creating a library composer.json file manually

53

Page 54: Create a PHP Library the right way

Exercise 1

1. cd ~ && mkdir -p phplibrary/exercise1

2. cd phplibrary/exercise1

3. Create composer.json

4. composer validate

5. composer install

54

Page 55: Create a PHP Library the right way

Exercise 1 values for composer.json

1. name: php_library_right_way/exercise1

2. description: "Creating a composer.json file manually"

3. require: php ^5.6 || ^7.0

4. require-dev: phpunit/phpunit ^5.7

5. type: library

6. License: MIT

55

Page 56: Create a PHP Library the right way

Exercise 1 solution 1 { 2 "name": "php_library_right_way/exercise1", 3 "description": "Creating a composer.json file manually", 4 "require": { 5 "php": "^5.6 || ^7.0" 6 }, 7 "require-dev": { 8 "phpunit/phpunit": "^5.7" 9 }, 10 "type": "library", 11 "license": "MIT" 12 }

56

Page 57: Create a PHP Library the right way

Exercise 2. Creating a library composer.json file using

composer init

57

Page 58: Create a PHP Library the right way

Exercise 2

1. cd ~ && mkdir -p phplibrary/exercise2

2. cd phplibrary/exercise2

3. composer init

4. composer validate

5. composer install

58

Page 59: Create a PHP Library the right way

Exercise 2 values for composer.json

1. name: php_library_right_way/exercise2

2. description: "Created with composer init"

3. Author: your name and email

4. require: php ^5.6 || ^7.0

5. require-dev: phpunit/phpunit ^5.7

6. type: library

7. License: MIT

8. Others: leave default59

Page 60: Create a PHP Library the right way

Exercise 2 solution 1 { 2 "name": "php_library_right_way/exercise2", 3 "description": "Created with composer init", 4 "type": "library", 5 "require": { 6 "php": "^5.6 || ^7.0" 7 }, 8 "require-dev": { 9 "phpunit/phpunit": "^5.7" 10 }, 11 "license": "MIT", 12 "authors": [ 13 { 14 "name": "Christian Varela", 15 "email": "[email protected]" 16 } 17 ] 18 }

60

Page 61: Create a PHP Library the right way

Exercise 3. Creating my first library

61

Page 62: Create a PHP Library the right way

Repositories

1. https://github.com/ConquerorSoft/my_first_library

2. https://github.com/ConquerorSoft/my_first_project

62

Page 63: Create a PHP Library the right way

Exercise 3

1. cd ~ && mkdir -p phplibrary/my_first_library

2. Create a README.md file

63

Page 64: Create a PHP Library the right way

README.md 1 # My First Library # 2 3 This is my very first library I created at php[world].

64

Page 65: Create a PHP Library the right way

Exercise 3

3. git init

4. git add .

git commit -m "First commit"

5. git tag -a v0.1.0 -m "version 0.1.0"

6. Create a repository in GitHub

7. Connect your repository with GitHub

65

Page 66: Create a PHP Library the right way

66

Page 67: Create a PHP Library the right way

67

Page 68: Create a PHP Library the right way

Exercise 3

8. composer init

9. provide the following values

68

Page 69: Create a PHP Library the right way

Exercise 3 values for composer init

1. name: php_library_right_way/my_first_library

2. description: "Created with composer init”

3. Author: your name and email

4. require: php ^5.6 || ^7.0

5. require-dev: phpunit/phpunit ^5.7

6. type: library

7. License: MIT

8. Others: leave default69

Page 70: Create a PHP Library the right way

Exercise 3 composer.json 1 { 2 "name": "php_library_right_way/my_first_library", 3 "description": "Created with composer init", 4 "type": "library", 5 "require": { 6 "php": "^5.6 || ^7.0" 7 }, 8 "require-dev": { 9 "phpunit/phpunit": "^5.7" 10 }, 11 "license": "MIT", 12 "authors": [ 13 { 14 "name": "Christian Varela", 15 "email": "[email protected]" 16 } 17 ] 18 }

70

Page 71: Create a PHP Library the right way

Exercise 3

10. commit to git

git add .

git commit -m "Composer init"

git tag -a v0.1.1 -m "version 0.1.1"

git push -u origin master

git push origin v0.1.1

11.composer install

12.echo "composer.lock" >> .gitignore71

Page 72: Create a PHP Library the right way

Exercise 3 (click link)

13.Create a CHANGELOG.md file

72

Page 73: Create a PHP Library the right way

1 # Changelog # 2 3 All notable changes to this library will be documented in this file. 4 5 The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 6 and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 7 8 ## [Unreleased] ## 9 10 ## [0.1.2] - 2017-11-04 ## 11 ### Added ### 12 - CHANGELOG.md file created 13 14 ### Changed ### 15 - .gitignore file ignores composer.lock 16 - README.md add steps and fixes 17 18 ## [0.1.1] - 2017-11-04 ## 19 ### Added ### 20 - composer init ran 21 - composer.json file created 22 - .gitignore file created to ignore vendor directory 23 24 ### Changed ### 25 - README.md file has more steps 26 27 ## [0.1.0] - 2017-11-04 ## 28 ### Added ### 29 - README.md file created

73

Page 74: Create a PHP Library the right way

Exercise 3

14. commit to git

git add .

git commit -m "Changelog file added"

git tag -a v0.1.2 -m "version 0.1.2"

git push -u origin master

git push origin v0.1.2

15.mkdir src && mkdir tests

16.add the next to the composer.json file (click link)74

Page 75: Create a PHP Library the right way

1 { 2 ... 3 "keywords": [ 4 "conquerorsoft", 5 "my_first_library", 6 "tutorial", 7 "phpworld 2017", 8 "workshop" 9 ], 10 "homepage": "http://www.conquerorsoft.com/my_first_library", 11 "require-dev": { 12 "phpunit/phpunit": “^5.7”, 13 "squizlabs/php_codesniffer": "3.*" 14 }, 15 "require": { 16 "php": "~5.6 || ~7.0" 17 }, 18 "autoload": { 19 "psr-4": { 20 "conquerorsoft\\my_first_library\\": "src" 21 } 22 }, 23 "autoload-dev": { 24 "psr-4": { 25 "conquerorsoft\\my_first_library\\": "tests" 26 } 27 }, 28 "scripts": { 29 "test": "phpunit", 30 "check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests", 31 "fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests" 32 }, 33 ... 34 } 75

Page 76: Create a PHP Library the right way

Exercise 3

17.create phpunit.xml (click link) 1 <?xml version="1.0" encoding="UTF-8"?> 2 <phpunit bootstrap="vendor/autoload.php" 3 colors="true" 4 verbose="true" 5 convertErrorsToExceptions="true" 6 convertNoticesToExceptions="true" 7 convertWarningsToExceptions="true"> 8 <testsuites> 9 <testsuite name="conquerorsoft my_first_library Test Suite"> 10 <directory>tests</directory> 11 </testsuite> 12 </testsuites> 13 <filter> 14 <whitelist> 15 <directory suffix=".php">src/</directory> 16 </whitelist> 17 </filter> 18 <logging> 19 <log type="tap" target="build/report.tap"/> 20 <log type="junit" target="build/report.junit.xml"/> 21 <log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/> 22 <log type="coverage-text" target="build/coverage.txt"/> 23 <log type="coverage-clover" target="build/logs/clover.xml"/> 24 </logging> 25 </phpunit>

76

Page 77: Create a PHP Library the right way

Exercise 3

18.echo build >> .gitignore

19.composer update

20.commit to git

git add .

git commit -m "Preparation for development"

git tag -a v0.1.3 -m "version 0.1.3"

git push -u origin master

git push origin v0.1.3

77

Page 78: Create a PHP Library the right way

Exercise 3

21.Add a LICENSE.md file (for this example we chose MIT, click link)

22.vim tests/FirstClassTest.php

23.vim src/FirstClass.php

24.commit to git

git add .

git commit -m "Encode and decode string functionality"

git tag -a v0.1.4 -m "version 0.1.4"

git push -u origin master

git push origin v0.1.478

Page 79: Create a PHP Library the right way

Exercise 3

25.add docblock to everything

/**

* This is a summary example

*

* This is a description

*

* @example this is tag

*/79

Page 80: Create a PHP Library the right way

Exercise 3

26.commit to git

git add .

git commit -m "Docblocks added everywhere"

git tag -a v0.1.5 -m "version 0.1.5"

git push -u origin master

git push origin v0.1.5

27.add repository to Travis and create travis configuration file

80

Page 81: Create a PHP Library the right way

.travis.yml (click link) 1 dist: trusty 2 language: php 3 4 php: 5 - 5.6 6 - 7.0 7 - 7.1 8 - hhvm 9 10 # This triggers builds to run on the new TravisCI infrastructure. 11 # See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/ 12 sudo: false 13 14 ## Cache composer 15 cache: 16 directories: 17 - $HOME/.composer/cache 18 19 matrix: 20 include: 21 - php: 5.6 22 env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"' 23 24 before_script: 25 - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist 26 27 script: 28 - vendor/bin/phpcs --standard=psr2 src/ 29 - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover 30 31 after_script: 32 - | 33 if [[ "$TRAVIS_PHP_VERSION" != 'hhvm' && "$TRAVIS_PHP_VERSION" != '7.0' ]]; then 34 wget https://scrutinizer-ci.com/ocular.phar 35 php ocular.phar code-coverage:upload --format=php-clover coverage.clover 36 fi

81

Page 82: Create a PHP Library the right way

82

Page 83: Create a PHP Library the right way

83

Page 84: Create a PHP Library the right way

84

Page 85: Create a PHP Library the right way

85

Page 86: Create a PHP Library the right way

86

Page 87: Create a PHP Library the right way

87

Page 88: Create a PHP Library the right way

88

Page 89: Create a PHP Library the right way

89

Page 90: Create a PHP Library the right way

Exercise 3

28.commit to git

git add .

git commit -m "Travis CI integration"

git tag -a v0.1.6 -m "version 0.1.6"

git push -u origin master

git push origin v0.1.6

29. make sure version for phpunit is ^5.7 in composer.json to support php 5.6

90

Page 91: Create a PHP Library the right way

Exercise 3

30.commit to git

git add .

git commit -m "Phpunit version changed to support php version 5.6"

git tag -a v0.1.7 -m "version 0.1.7"

git push -u origin master

git push origin v0.1.7

91

Page 92: Create a PHP Library the right way

Exercise 3

31.get travis badge to put in README.md

92

Page 93: Create a PHP Library the right way

Exercise 3

32. Put license badge in README

[![Software License][ico-license]](LICENSE.md)

[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat

93

Page 94: Create a PHP Library the right way

94

Page 95: Create a PHP Library the right way

Exercise 3

33.commit to git

git add .

git commit -m "Travis CI and License badges in README"

git tag -a v0.1.8 -m "version 0.1.8"

git push -u origin master

git push origin v0.1.8

34.Create scrutinizer-ci account and link with GitHub

35.Create .scrutinizer.yml file95

Page 96: Create a PHP Library the right way

.scrutinizer.yml (click link) 1 filter: 2 excluded_paths: [tests/*] 3 4 checks: 5 php: 6 remove_extra_empty_lines: true 7 remove_php_closing_tag: true 8 remove_trailing_whitespace: true 9 fix_use_statements: 10 remove_unused: true 11 preserve_multiple: false 12 preserve_blanklines: true 13 order_alphabetically: true 14 fix_php_opening_tag: true 15 fix_linefeed: true 16 fix_line_ending: true 17 fix_identation_4spaces: true 18 fix_doc_comments: true 19 20 tools: 21 external_code_coverage: 22 timeout: 600 23 runs: 3

96

Page 97: Create a PHP Library the right way

97

Page 98: Create a PHP Library the right way

98

Page 99: Create a PHP Library the right way

99

Page 100: Create a PHP Library the right way

100

Page 101: Create a PHP Library the right way

101

Page 102: Create a PHP Library the right way

102

Page 103: Create a PHP Library the right way

103

Page 104: Create a PHP Library the right way

104

Page 105: Create a PHP Library the right way

105

Page 106: Create a PHP Library the right way

106

Page 107: Create a PHP Library the right way

Exercise 3

36.Get scrutinizer badges in README file

107

Page 108: Create a PHP Library the right way

Exercise 3

37.commit to git

git add .

git commit -m "Scrutinizer CI and badges in README"

git tag -a v0.1.9 -m "version 0.1.9"

git push -u origin master

git push origin v0.1.9

38.Make sure there are no type hinting for PHP 7.0 only

108

Page 109: Create a PHP Library the right way

109

Page 110: Create a PHP Library the right way

Exercise 3

39.commit to git

git add .

git commit -m "Fixes to uncompatible type hinting"

git tag -a v0.1.10 -m "version 0.1.10"

git push -u origin master

git push origin v0.1.10

40.Apply any patch proposed from scrutinizer

110

Page 111: Create a PHP Library the right way

Exercise 3

41.commit to git

git add .

git commit -m "Spacing patch from scrutinizer applied"

git tag -a v0.1.11 -m "version 0.1.11"

git push -u origin master

git push origin v0.1.11

111

Page 112: Create a PHP Library the right way

Exercise 3

42.Create contributing files

1. CONTRIBUTING.md (click link)

2. CODE_OF_CONDUCT.md (click link)

112

Page 113: Create a PHP Library the right way

Exercise 3

43.Add more sections to README file

1. Install

2. Usage

3. Change log

4. Testing

5. Contributing

6. Security

7. Credits

8. License113

Page 114: Create a PHP Library the right way

Exercise 3

44.commit to git

git add .

git commit -m "Improvements to README"

git tag -a v0.1.12 -m "version 0.1.12"

git push -u origin master

git push origin v0.1.12

45.Add .gitattributes file to ignore some files or folders when --prefer-dist is used

114

Page 115: Create a PHP Library the right way

Exercise 3

46.commit to git

git add .

git commit -m ".gitattributes file created"

git tag -a v0.1.13 -m "version 0.1.13"

git push -u origin master

git push origin v0.1.13

115

Page 116: Create a PHP Library the right way

Exercise 347.create an account in packagist.org and submit your library using your github

repository

48.Make your package in packagist to be autoupdated on push

49.Add last version in packagist badge to README.md file

[![Latest Version on Packagist][ico-version]][link-packagist]

[ico-version]: https://img.shields.io/packagist/v/conquerorsoft/my_first_library.svg?style=flat

[link-packagist]: https://packagist.org/packages/conquerorsoft/my_first_library

116

Page 117: Create a PHP Library the right way

117

Page 118: Create a PHP Library the right way

118

Page 119: Create a PHP Library the right way

119

Page 120: Create a PHP Library the right way

120

Page 121: Create a PHP Library the right way

121

Page 122: Create a PHP Library the right way

122

Page 123: Create a PHP Library the right way

123

Page 124: Create a PHP Library the right way

124

Page 125: Create a PHP Library the right way

Exercise 3

50.commit to git

git add .

git commit -m "Instructions to use packagist.org in README"

git tag -a v0.1.14 -m "version 0.1.14"

git push -u origin master

git push origin v0.1.14

125

Page 126: Create a PHP Library the right way

Exercise 3

51.create gh-pages branch

git checkout -b gh-pages

git push -u origin gh-pages

git checkout master

52.Go to github settings for your repository

53.Choose a theme in GitHub Pages section

54.Your library page is ready: https://conquerorsoft.github.io/my_first_library/

126

Page 127: Create a PHP Library the right way

127

Page 128: Create a PHP Library the right way

128

Page 129: Create a PHP Library the right way

129

Page 130: Create a PHP Library the right way

Exercise 3

54.Your library page is ready: https://conquerorsoft.github.io/my_first_library/

55.commit to git

git add .

git commit -m "Documentation instructions for the library"

git tag -a v1.0.0 -m "version 1.0.0"

git push -u origin master

git push origin v1.0.0130

Page 131: Create a PHP Library the right way

131

Page 132: Create a PHP Library the right way

Exercise 4. Creating my first project using my first library

132

Page 133: Create a PHP Library the right way

Exercise 4

1. cd ~ && mkdir -p phplibrary/my_first_project && cd phplibrary/my_first_project

2. create a README.md file

133

Page 134: Create a PHP Library the right way

README.md 1 # My First Project # 2 3 This is my very first project I created at php[world].

134

Page 135: Create a PHP Library the right way

Exercise 4

3. git init

4. commit to git

git add .

git commit -m "First commit of my project"

5. Assign a version to your project

git tag -a v0.1.0 -m "version 0.1.0"

6. Create a repository in GitHub

135

Page 136: Create a PHP Library the right way

Exercise 4

7. connect your repository

git remote add origin https://github.com/ConquerorSoft/my_first_project.git

git push -u origin master

git push origin v0.1.0

8. composer init

9. Provide the next values

136

Page 137: Create a PHP Library the right way

Exercise 4 values for composer.json1. name: php_library_right_way/my_first_project

2. description: "Project created with composer init"

3. require: php ^5.6 || ^7.0

4. require-dev:

1. phpunit/phpunit latest ^5.7

2. squizlabs/php_codesniffer 3.*

5. type: project

6. License: MIT

7. Author: your name and email

8. Others: leave default137

Page 138: Create a PHP Library the right way

Exercise 4 composer.json 1 { 2 "name": "conquerorsoft/my_first_project", 3 "description": "Project created with composer init", 4 "type": "project", 5 "require": { 6 "php": "^5.6 || ^7.0" 7 }, 8 "require-dev": { 9 "phpunit/phpunit": "^5.7", 10 "squizlabs/php_codesniffer": "3.*" 11 }, 12 "license": "MIT", 13 "authors": [ 14 { 15 "name": "Christian Varela", 16 "email": "[email protected]" 17 } 18 ] 19 }

138

Page 139: Create a PHP Library the right way

Exercise 4

10.edit composer to include your library

139

Page 140: Create a PHP Library the right way

Exercise 4 composer.json 1 { 2 "name": "conquerorsoft/my_first_project", 3 "description": "Project created with composer init", 4 "type": "project", 5 "repositories": [ 6 { 7 "type": "vcs", 8 "url": "/Users/gabriel/phplibrary/my_first_library" 9 } 10 ], 11 "require": { 12 "php": "^5.6 || ^7.0", 13 "conquerorsoft/my_first_library": "^0.1" 14 }, 15 "require-dev": { 16 "phpunit/phpunit": "^5.7", 17 "squizlabs/php_codesniffer": "3.*" 18 }, 19 "license": "MIT", 20 "authors": [ 21 { 22 "name": "Christian Varela", 23 "email": "[email protected]" 24 } 25 ] 26 }

140

Page 141: Create a PHP Library the right way

Exercise 4

11.commit to git

git add .

git commit -m "Composer init"

git tag -a v0.1.1 -m "version 0.1.1"

git push -u origin master

git push origin v0.1.1

12.composer install

13.create a CHANGELOG.md file (click link)141

Page 142: Create a PHP Library the right way

1 # Changelog # 2 3 All notable changes to this library will be documented in this file. 4 5 The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 6 and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 7 8 ## [Unreleased] ## 9 10 ## [0.1.2] - 2017-11-09 ## 11 ### Added ### 12 - CHANGELOG.md file created 13 14 ### Changed ### 15 - README.md add steps and fixes 16 17 18 ## [0.1.1] - 2017-11-09 ## 19 ### Added ### 20 - composer init ran 21 - composer.json file created 22 - .gitignore file created to ignore vendor directory 23 - composer.lock file created 24 25 ### Changed ### 26 - README.md file has more steps and fixes 27 28 ## [0.1.0] - 2017-11-09 ## 29 ### Added ### 30 - README.md file created 142

Page 143: Create a PHP Library the right way

Exercise 4

14.commit to git

git add .

git commit -m "Changelog file added"

git tag -a v0.1.2 -m "version 0.1.2"

git push -u origin master

git push origin v0.1.2

15.mkdir src && mkdir tests

16.add more information to composer.json143

Page 144: Create a PHP Library the right way

1 { 2 ... 3 "keywords": [ 4 "conquerorsoft", 5 "my_first_project", 6 "tutorial", 7 "phpworld 2017", 8 "workshop" 9 ], 10 "homepage": "http://www.conquerorsoft.com/my_first_project", 11 "minimum-stability": "stable", 12 "scripts": { 13 "test": "phpunit", 14 "check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests", 15 "fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests" 16 } 17 ... 18 }

144

Page 145: Create a PHP Library the right way

Exercise 4

17.create phpunit.xml (click link) 1 <?xml version="1.0" encoding="UTF-8"?> 2 <phpunit bootstrap="vendor/autoload.php" 3 colors="true" 4 verbose="true" 5 convertErrorsToExceptions="true" 6 convertNoticesToExceptions="true" 7 convertWarningsToExceptions="true"> 8 <testsuites> 9 <testsuite name="conquerorsoft my_first_project Test Suite"> 10 <directory>./tests</directory> 11 </testsuite> 12 </testsuites> 13 <filter> 14 <whitelist> 15 <directory suffix=".php">src/</directory> 16 </whitelist> 17 </filter> 18 <logging> 19 <log type="tap" target="build/report.tap"/> 20 <log type="junit" target="build/report.junit.xml"/> 21 <log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/> 22 <log type="coverage-text" target="build/coverage.txt"/> 23 <log type="coverage-clover" target="build/logs/clover.xml"/> 24 </logging> 25 </phpunit>

145

Page 146: Create a PHP Library the right way

Exercise 4

18.echo build >> .gitignore

19.composer update

20.commit to git

echo "build/" >> .gitignore

git add .

git commit -m "Preparation for development"

git tag -a v0.1.3 -m "version 0.1.3"

git push -u origin master

git push origin v0.1.3146

Page 147: Create a PHP Library the right way

Exercise 4

21.Add a LICENSE.md file (for this example we chose MIT click link)

22.Create tests/FirstProjectClassTest.php

23.Create src/FirstProjectClass.php

24.Add the next sections to composer.json

147

Page 148: Create a PHP Library the right way

1 { 2 ... 3 "autoload": { 4 "psr-4": { 5 "conquerorsoft\\my_first_project\\": "src" 6 } 7 }, 8 "autoload-dev": { 9 "psr-4": { 10 "conquerorsoft\\my_first_project\\": "test" 11 } 12 }, 13 ... 14 }

148

Page 149: Create a PHP Library the right way

Exercise 4

25.composer dump-autoload

26.commit to git

git add .

git commit -m "Classes from project calling my library"

git tag -a v0.1.4 -m "version 0.1.4"

git push -u origin master

git push origin v0.1.4

27.add repository to Travis and create travis configuration file149

Page 150: Create a PHP Library the right way

.travis.yml (click link) 1 dist: trusty 2 language: php 3 4 php: 5 - 5.6 6 - 7.0 7 - 7.1 8 - hhvm 9 10 # This triggers builds to run on the new TravisCI infrastructure. 11 # See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/ 12 sudo: false 13 14 ## Cache composer 15 cache: 16 directories: 17 - $HOME/.composer/cache 18 19 matrix: 20 include: 21 - php: 5.6 22 env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"' 23 24 before_script: 25 - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist 26 27 script: 28 - vendor/bin/phpcs --standard=psr2 src/ 29 - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover 30 31 after_script: 32 - | 33 if [[ "$TRAVIS_PHP_VERSION" != 'hhvm' && "$TRAVIS_PHP_VERSION" != '7.0' ]]; then 34 wget https://scrutinizer-ci.com/ocular.phar 35 php ocular.phar code-coverage:upload --format=php-clover coverage.clover 36 fi

150

Page 151: Create a PHP Library the right way

151

Page 152: Create a PHP Library the right way

152

Page 153: Create a PHP Library the right way

153

Page 154: Create a PHP Library the right way

Exercise 4

28.commit to git

git add .

git commit -m "Travis CI integration"

git tag -a v0.1.5 -m "version 0.1.5"

git push -u origin master

git push origin v0.1.5

29.Change composer.json to use github repository instead of file system local folder

154

Page 155: Create a PHP Library the right way

1 { 2 ... 3 "repositories": [ 4 { 5 "type": "vcs", 6 "url": "https://github.com/ConquerorSoft/my_first_library" 7 } 8 ], 9 ... 10 }

155

Page 156: Create a PHP Library the right way

Exercise 4

30.composer update

31.commit to git git add .

git commit -m "VCS reference changed for my_first_library in composer.json"

git tag -a v0.1.6 -m "version 0.1.6"

git push -u origin master git push origin v0.1.6

32.Remove references for repositories in composer.json156

Page 157: Create a PHP Library the right way

Exercise 4

33.Run composer update

composer clearcache

composer update

34.commit to git

git add .

git commit -m "my_first_library is now taken from packagist"

git tag -a v0.1.7 -m "version 0.1.7"

git push -u origin master

git push origin v0.1.7157

Page 158: Create a PHP Library the right way

Exercise 4

35.Link scrutinizer-ci account with github and create .scrutinizer.yml file

158

Page 159: Create a PHP Library the right way

.scrutinizer.yml (click link) 1 filter: 2 excluded_paths: [tests/*] 3 4 checks: 5 php: 6 remove_extra_empty_lines: true 7 remove_php_closing_tag: true 8 remove_trailing_whitespace: true 9 fix_use_statements: 10 remove_unused: true 11 preserve_multiple: false 12 preserve_blanklines: true 13 order_alphabetically: true 14 fix_php_opening_tag: true 15 fix_linefeed: true 16 fix_line_ending: true 17 fix_identation_4spaces: true 18 fix_doc_comments: true 19 20 tools: 21 external_code_coverage: 22 timeout: 600 23 runs: 3

159

Page 160: Create a PHP Library the right way

Exercise 4

36.commit to git

git add .

git commit -m "Scrutinizer support added"

git tag -a v0.1.8 -m "version 0.1.8"

git push -u origin master

git push origin v0.1.8

37.change composer.json to require version ^1.0.0 for my_first_library

38.composer update

160

Page 161: Create a PHP Library the right way

Exercise 4

39.commit to git

git add .

git commit -m "Using version ^1.0 from my_first_library"

git tag -a v0.1.9 -m "version 0.1.9"

git push -u origin master

git push origin v0.1.9

40.Create contributing files: CONTRIBUTING.md and CODE_OF_CONDUCT.md

161

Page 162: Create a PHP Library the right way

Exercise 441.Add more sections to README

1. Install

2. Change log

3. Testing

42.commit to git

git add .

git commit -m "Improvements to README"

git tag -a v0.1.10 -m "version 0.1.10"

git push -u origin master

git push origin v0.1.10162

Page 163: Create a PHP Library the right way

Exercise 4

43.submit the package to packagist.org using the github repository

44.Make the package to be autoupdated in packagist on push

1. Go to your GitHub repository

2. Click the "Settings" button

3. Click "Integrations & services"

4. Add a "Packagist" service, and configure it with your API token, plus your Packagist username

5. Check the "Active" box and submit the form163

Page 164: Create a PHP Library the right way

164

Page 165: Create a PHP Library the right way

165

Page 166: Create a PHP Library the right way

166

Page 167: Create a PHP Library the right way

167

Page 168: Create a PHP Library the right way

168

Page 169: Create a PHP Library the right way

Exercise 4

45.Add last version in packagist badge to README.md file

[![Latest Version on Packagist][ico-version]][link-packagist]

[ico-version]: https://img.shields.io/packagist/v/conquerorsoft/my_first_project.svg?style=flat

[link-packagist]: https://packagist.org/packages/conquerorsoft/my_first_project

169

Page 170: Create a PHP Library the right way

Exercise 4

46.commit to git

git add .

git commit -m "Instructions to use packagist.org in README"

git tag -a v0.1.11 -m "version 0.1.11"

git push -u origin master

git push origin v0.1.11

170

Page 171: Create a PHP Library the right way

Exercise 4

47.create a gh-pages branch

git checkout -b gh-pages

git push -u origin gh-pages

git checkout master

48.Go to github settings for your repository

49.Choose a theme in GitHub Pages section

50.Your project page is ready now: https://conquerorsoft.github.io/my_first_project/

171

Page 172: Create a PHP Library the right way

172

Page 173: Create a PHP Library the right way

Exercise 4

51.commit to git

git add .

git commit -m "Documentation instructions for the project"

git tag -a v1.0.0 -m "version 1.0.0"

git push -u origin master

git push origin v1.0.0

173

Page 175: Create a PHP Library the right way

Questions?

175

Page 176: Create a PHP Library the right way

Thank you

176

https://joind.in/talk/de19a

Page 177: Create a PHP Library the right way

References, credits and resources1. SemVer.org

2. keepachangelog.com

3. phppackagechecklist.com

4. composer.org

5. thephpleague.com

6. poser.pugx.org

7. readthedocs.org

8. www.phpdoc.org

9. phpunit.de

10.packagist.org

11.packagist.com

12.www.php-fig.org/psr/psr-2

13.github.com/squizlabs/PHP_CodeSniffer

14.pages.github.com

15.travis-ci.org

16.choosealicense.com

17.spdx.org/licenses

18.codeclimate.com

19.getcomposer.org/doc/articles/handling-private-packages-with-satis.md

20.https://www.contributor-covenant.org/

21.http://www.mkdocs.org/

177