Create a PHP Library the right way

Preview:

Citation preview

Create a PHP Library the Right way

By Christian Varela @gabriel0702

cvarela@conquerorsoft.com

https://joind.in/talk/de19a1

Requirements

3

https://goo.gl/StPczG

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

5

Conqueror Soft will take your business to the next Level!

6

www.conquerorsoft.cominfo@conquerorsoft.com

facebook.com/conquerorsoft

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

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

9

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

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

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

Web application

13

Web Application

Web Server

Application Server

DB Server

Internal dependencies

External dependencies

Configuration files

14

15

How the web application knows about its dependencies?

16

17

Web Application

Web Server

Application Server

DB Server

Internal dependencies

External dependencies

Configuration files

Dependencies!?!?

composer.json

COMPOSERDependency manager

18

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

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

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

Where composer get the packages from?

22

23

Packages!?!?

packagist.org packagist.com custom repositories

PACKAGIST.ORGComposer repository

24

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

PACKAGIST.COMComposer repository

26

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

CUSTOM REPOSITORIES

From different VCS

28

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

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

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

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

Packages versioning

33

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

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

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

License for your library

37

License for your library

38https://choosealicense.com/

Minimum-stability options

1. dev

2. alpha

3. beta

4. RC

5. stable

39

Code Quality

40

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/

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

Changelog

43

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

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

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

Continuos Integration tools

47

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

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

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

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

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

Exercise 1. Creating a library composer.json file manually

53

Exercise 1

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

2. cd phplibrary/exercise1

3. Create composer.json

4. composer validate

5. composer install

54

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

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

Exercise 2. Creating a library composer.json file using

composer init

57

Exercise 2

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

2. cd phplibrary/exercise2

3. composer init

4. composer validate

5. composer install

58

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

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": "cvarela@conquerorsoft.com" 16 } 17 ] 18 }

60

Exercise 3. Creating my first library

61

Repositories

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

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

62

Exercise 3

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

2. Create a README.md file

63

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

64

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

66

67

Exercise 3

8. composer init

9. provide the following values

68

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

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": "cvarela@conquerorsoft.com" 16 } 17 ] 18 }

70

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

Exercise 3 (click link)

13.Create a CHANGELOG.md file

72

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

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

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

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

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

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

Exercise 3

25.add docblock to everything

/**

* This is a summary example

*

* This is a description

*

* @example this is tag

*/79

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

.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

82

83

84

85

86

87

88

89

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

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

Exercise 3

31.get travis badge to put in README.md

92

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

94

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

.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

97

98

99

100

101

102

103

104

105

106

Exercise 3

36.Get scrutinizer badges in README file

107

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

109

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

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

Exercise 3

42.Create contributing files

1. CONTRIBUTING.md (click link)

2. CODE_OF_CONDUCT.md (click link)

112

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

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

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

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

117

118

119

120

121

122

123

124

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

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

127

128

129

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

131

Exercise 4. Creating my first project using my first library

132

Exercise 4

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

2. create a README.md file

133

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

134

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

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

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

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": "cvarela@conquerorsoft.com" 17 } 18 ] 19 }

138

Exercise 4

10.edit composer to include your library

139

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": "cvarela@conquerorsoft.com" 24 } 25 ] 26 }

140

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

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

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

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

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

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

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

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

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

.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

151

152

153

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

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

155

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

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

Exercise 4

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

158

.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

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

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

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

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

164

165

166

167

168

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

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

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

172

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

Questions?

175

Thank you

176

https://joind.in/talk/de19a

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

Recommended