php-einführung - lesson 8 - composer (dependency manager) … · 2017. 6. 27. · php-einfuhrung -...

64
PHP-Einf¨ uhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Upload: others

Post on 14-Aug-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

PHP-Einfuhrung - Lesson 8 - Composer

(Dependency manager) and JSON

Alexander Lichter

June 27, 2017

Page 2: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Content of this lesson

1. Recap

2. Composer

3. JSON

4. Collections (next lesson)

1

Page 3: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Recap

Page 4: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Recap Recap Recap

2

Page 5: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Composer

Page 6: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Questions about the installation?

Do you have questions concerning the installation of Composer? We

have time to go through them now!

3

Page 7: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Version

Alright, we are good to go now!

Let’s start with an easy command:

composer -v It should show you the version of your installed composer

4

Page 8: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Version

Alright, we are good to go now! Let’s start with an easy command:

composer -v

It should show you the version of your installed composer

4

Page 9: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Version

Alright, we are good to go now! Let’s start with an easy command:

composer -v It should show you the version of your installed composer

4

Page 10: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Init

Okay. Let’s initialize composer for our project. Each project has it’s own

composer file and it’s own dependencies (obviously)

Now go to your project root folder and issue composer init You now

should walk through the init wizard without problems.

When it you are asked if you want to add your dependencies interactively,

write no. Same goes for the dev-dependencies. Now confirm the

generation!

5

Page 11: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Init

Okay. Let’s initialize composer for our project. Each project has it’s own

composer file and it’s own dependencies (obviously)

Now go to your project root folder and issue composer init

You now

should walk through the init wizard without problems.

When it you are asked if you want to add your dependencies interactively,

write no. Same goes for the dev-dependencies. Now confirm the

generation!

5

Page 12: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Init

Okay. Let’s initialize composer for our project. Each project has it’s own

composer file and it’s own dependencies (obviously)

Now go to your project root folder and issue composer init You now

should walk through the init wizard without problems.

When it you are asked if you want to add your dependencies interactively,

write no. Same goes for the dev-dependencies. Now confirm the

generation!

5

Page 13: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Lock and JSON file

Congratulations! Your composer file is ready to go! You have a

composer.json and a composer.lock file in your project root now. While

the composer.json is used for the configuration of composer, the

composer.lock saves the currently imported dependencies (with the exact

versions).

But why do we need a lock file?

It’s great for deploying on production servers, because you get the exact

same dependencies as you have on your testing/local environment. Which

means: Your production environment is (almost) equal to your local one.

When committing to a VCS (Git, or SVN [but better go for git]), commit

both files!

6

Page 14: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Lock and JSON file

Congratulations! Your composer file is ready to go! You have a

composer.json and a composer.lock file in your project root now. While

the composer.json is used for the configuration of composer, the

composer.lock saves the currently imported dependencies (with the exact

versions).

But why do we need a lock file?

It’s great for deploying on production servers, because you get the exact

same dependencies as you have on your testing/local environment. Which

means: Your production environment is (almost) equal to your local one.

When committing to a VCS (Git, or SVN [but better go for git]), commit

both files!

6

Page 15: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Lock and JSON file

Congratulations! Your composer file is ready to go! You have a

composer.json and a composer.lock file in your project root now. While

the composer.json is used for the configuration of composer, the

composer.lock saves the currently imported dependencies (with the exact

versions).

But why do we need a lock file?

It’s great for deploying on production servers, because you get the exact

same dependencies as you have on your testing/local environment. Which

means: Your production environment is (almost) equal to your local one.

When committing to a VCS

(Git, or SVN [but better go for git]), commit

both files!

6

Page 16: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Lock and JSON file

Congratulations! Your composer file is ready to go! You have a

composer.json and a composer.lock file in your project root now. While

the composer.json is used for the configuration of composer, the

composer.lock saves the currently imported dependencies (with the exact

versions).

But why do we need a lock file?

It’s great for deploying on production servers, because you get the exact

same dependencies as you have on your testing/local environment. Which

means: Your production environment is (almost) equal to your local one.

When committing to a VCS (Git, or SVN [but better go for git]), commit

both files!

6

Page 17: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Different dependencies

Two slides before, when we initialized our composer setup, I’ve

mentioned dependencies and dev-dependencies. So we have two

different dependency ”types”.. But why?

Dev-dependencies are only those dependencies we need to develop our

application. They shouldn’t appear on our production server. Things like:

Mockup libaries, Testing systems (PHPUnit), Debug tools, and so on.

7

Page 18: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Different dependencies

Two slides before, when we initialized our composer setup, I’ve

mentioned dependencies and dev-dependencies. So we have two

different dependency ”types”.. But why?

Dev-dependencies are only those dependencies we need to develop our

application. They shouldn’t appear on our production server. Things like:

Mockup libaries, Testing systems (PHPUnit), Debug tools, and so on.

7

Page 19: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Different dependencies

Two slides before, when we initialized our composer setup, I’ve

mentioned dependencies and dev-dependencies. So we have two

different dependency ”types”.. But why?

Dev-dependencies are only those dependencies we need to develop our

application. They shouldn’t appear on our production server. Things like:

Mockup libaries, Testing systems (PHPUnit), Debug tools, and so on.

7

Page 20: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Require and Install

This leads us to the next steps: Setting up dependencies This will work

with composer require[-dev] AUTHOR/PACKAGE [version], while

require is for ”normal” dependencies and require-dev is for

dev-dependencies

The version constraint is optional. We’ll talk about that in a few

minutes! If you don’t pass anything, the latest version is used.

If you want to install your dependencies based on your .lock file now (on

another PC/Server e.g.), you can use composer install [--no-dev].

Again, –no-dev is used for production and will not install

dev-dependencies.

8

Page 21: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Require and Install

This leads us to the next steps: Setting up dependencies This will work

with composer require[-dev] AUTHOR/PACKAGE [version], while

require is for ”normal” dependencies and require-dev is for

dev-dependencies

The version constraint is optional. We’ll talk about that in a few

minutes! If you don’t pass anything, the latest version is used.

If you want to install your dependencies based on your .lock file now (on

another PC/Server e.g.), you can use composer install [--no-dev].

Again, –no-dev is used for production and will not install

dev-dependencies.

8

Page 22: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Require and Install

This leads us to the next steps: Setting up dependencies This will work

with composer require[-dev] AUTHOR/PACKAGE [version], while

require is for ”normal” dependencies and require-dev is for

dev-dependencies

The version constraint is optional. We’ll talk about that in a few

minutes! If you don’t pass anything, the latest version is used.

If you want to install your dependencies based on your .lock file now (on

another PC/Server e.g.), you can use composer install [--no-dev].

Again, –no-dev is used for production and will not install

dev-dependencies.

8

Page 23: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Update dependencies

Every now and then you may want to update your dependencies :D (New

features, security stuff and so on). This will work with only one

command: composer update

And that’s it! Your .lock file is updated too, so you don’t need to worry

about that.

9

Page 24: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Update dependencies

Every now and then you may want to update your dependencies :D (New

features, security stuff and so on). This will work with only one

command: composer update

And that’s it! Your .lock file is updated too, so you don’t need to worry

about that.

9

Page 25: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Versions

The cool thing in composer is: You can setup your version constraint by

a huge set of rules. Each developer has his own ”scheme” about how to

provide minor/major/patch/hotfix updates, so this won’t be a problem

for you too if you setup the versions correctly.

Type Example Description

Exact1.0.2 Exact version number (and

only this version)

Range>=1.0 <1.9 A simple version range

Range (-)1.0 - 2.0 The same as >=1.0.0 <2.1

Wildcard1.0.* Equal to >=1.0 <1.1

Tilde˜1.2.3 Like >=1.2.3 <1.3.0

Caretˆ1.2.3 Like >=1.2.3 <2.0.0 (ideal

for non-breaking)

10

Page 26: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Versions

The cool thing in composer is: You can setup your version constraint by

a huge set of rules. Each developer has his own ”scheme” about how to

provide minor/major/patch/hotfix updates, so this won’t be a problem

for you too if you setup the versions correctly.

Type Example Description

Exact1.0.2 Exact version number (and

only this version)

Range>=1.0 <1.9 A simple version range

Range (-)1.0 - 2.0 The same as >=1.0.0 <2.1

Wildcard1.0.* Equal to >=1.0 <1.1

Tilde˜1.2.3 Like >=1.2.3 <1.3.0

Caretˆ1.2.3 Like >=1.2.3 <2.0.0 (ideal

for non-breaking)

10

Page 27: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Versions

The cool thing in composer is: You can setup your version constraint by

a huge set of rules. Each developer has his own ”scheme” about how to

provide minor/major/patch/hotfix updates, so this won’t be a problem

for you too if you setup the versions correctly.

Type Example Description

Exact1.0.2 Exact version number (and

only this version)

Range>=1.0 <1.9 A simple version range

Range (-)1.0 - 2.0 The same as >=1.0.0 <2.1

Wildcard1.0.* Equal to >=1.0 <1.1

Tilde˜1.2.3 Like >=1.2.3 <1.3.0

Caretˆ1.2.3 Like >=1.2.3 <2.0.0 (ideal

for non-breaking)

10

Page 28: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Versions

The cool thing in composer is: You can setup your version constraint by

a huge set of rules. Each developer has his own ”scheme” about how to

provide minor/major/patch/hotfix updates, so this won’t be a problem

for you too if you setup the versions correctly.

Type Example Description

Exact1.0.2 Exact version number (and

only this version)

Range>=1.0 <1.9 A simple version range

Range (-)1.0 - 2.0 The same as >=1.0.0 <2.1

Wildcard1.0.* Equal to >=1.0 <1.1

Tilde˜1.2.3 Like >=1.2.3 <1.3.0

Caretˆ1.2.3 Like >=1.2.3 <2.0.0 (ideal

for non-breaking)

10

Page 29: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Versions

The cool thing in composer is: You can setup your version constraint by

a huge set of rules. Each developer has his own ”scheme” about how to

provide minor/major/patch/hotfix updates, so this won’t be a problem

for you too if you setup the versions correctly.

Type Example Description

Exact1.0.2 Exact version number (and

only this version)

Range>=1.0 <1.9 A simple version range

Range (-)1.0 - 2.0 The same as >=1.0.0 <2.1

Wildcard1.0.* Equal to >=1.0 <1.1

Tilde˜1.2.3 Like >=1.2.3 <1.3.0

Caretˆ1.2.3 Like >=1.2.3 <2.0.0 (ideal

for non-breaking)

10

Page 30: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Versions

The cool thing in composer is: You can setup your version constraint by

a huge set of rules. Each developer has his own ”scheme” about how to

provide minor/major/patch/hotfix updates, so this won’t be a problem

for you too if you setup the versions correctly.

Type Example Description

Exact1.0.2 Exact version number (and

only this version)

Range>=1.0 <1.9 A simple version range

Range (-)1.0 - 2.0 The same as >=1.0.0 <2.1

Wildcard1.0.* Equal to >=1.0 <1.1

Tilde˜1.2.3 Like >=1.2.3 <1.3.0

Caretˆ1.2.3 Like >=1.2.3 <2.0.0 (ideal

for non-breaking)

10

Page 31: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Versions

The cool thing in composer is: You can setup your version constraint by

a huge set of rules. Each developer has his own ”scheme” about how to

provide minor/major/patch/hotfix updates, so this won’t be a problem

for you too if you setup the versions correctly.

Type Example Description

Exact1.0.2 Exact version number (and

only this version)

Range>=1.0 <1.9 A simple version range

Range (-)1.0 - 2.0 The same as >=1.0.0 <2.1

Wildcard1.0.* Equal to >=1.0 <1.1

Tilde˜1.2.3 Like >=1.2.3 <1.3.0

Caretˆ1.2.3 Like >=1.2.3 <2.0.0 (ideal

for non-breaking)

10

Page 32: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Versions

The cool thing in composer is: You can setup your version constraint by

a huge set of rules. Each developer has his own ”scheme” about how to

provide minor/major/patch/hotfix updates, so this won’t be a problem

for you too if you setup the versions correctly.

Type Example Description

Exact1.0.2 Exact version number (and

only this version)

Range>=1.0 <1.9 A simple version range

Range (-)1.0 - 2.0 The same as >=1.0.0 <2.1

Wildcard1.0.* Equal to >=1.0 <1.1

Tilde˜1.2.3 Like >=1.2.3 <1.3.0

Caretˆ1.2.3 Like >=1.2.3 <2.0.0 (ideal

for non-breaking)

10

Page 33: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Versions

The cool thing in composer is: You can setup your version constraint by

a huge set of rules. Each developer has his own ”scheme” about how to

provide minor/major/patch/hotfix updates, so this won’t be a problem

for you too if you setup the versions correctly.

Type Example Description

Exact1.0.2 Exact version number (and

only this version)

Range>=1.0 <1.9 A simple version range

Range (-)1.0 - 2.0 The same as >=1.0.0 <2.1

Wildcard1.0.* Equal to >=1.0 <1.1

Tilde˜1.2.3 Like >=1.2.3 <1.3.0

Caretˆ1.2.3 Like >=1.2.3 <2.0.0 (ideal

for non-breaking)

10

Page 34: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Versions

The cool thing in composer is: You can setup your version constraint by

a huge set of rules. Each developer has his own ”scheme” about how to

provide minor/major/patch/hotfix updates, so this won’t be a problem

for you too if you setup the versions correctly.

Type Example Description

Exact1.0.2 Exact version number (and

only this version)

Range>=1.0 <1.9 A simple version range

Range (-)1.0 - 2.0 The same as >=1.0.0 <2.1

Wildcard1.0.* Equal to >=1.0 <1.1

Tilde˜1.2.3 Like >=1.2.3 <1.3.0

Caretˆ1.2.3 Like >=1.2.3 <2.0.0 (ideal

for non-breaking)

10

Page 35: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Versions

The cool thing in composer is: You can setup your version constraint by

a huge set of rules. Each developer has his own ”scheme” about how to

provide minor/major/patch/hotfix updates, so this won’t be a problem

for you too if you setup the versions correctly.

Type Example Description

Exact1.0.2 Exact version number (and

only this version)

Range>=1.0 <1.9 A simple version range

Range (-)1.0 - 2.0 The same as >=1.0.0 <2.1

Wildcard1.0.* Equal to >=1.0 <1.1

Tilde˜1.2.3 Like >=1.2.3 <1.3.0

Caretˆ1.2.3 Like >=1.2.3 <2.0.0 (ideal

for non-breaking)

10

Page 36: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Versions

The cool thing in composer is: You can setup your version constraint by

a huge set of rules. Each developer has his own ”scheme” about how to

provide minor/major/patch/hotfix updates, so this won’t be a problem

for you too if you setup the versions correctly.

Type Example Description

Exact1.0.2 Exact version number (and

only this version)

Range>=1.0 <1.9 A simple version range

Range (-)1.0 - 2.0 The same as >=1.0.0 <2.1

Wildcard1.0.* Equal to >=1.0 <1.1

Tilde˜1.2.3 Like >=1.2.3 <1.3.0

Caretˆ1.2.3 Like >=1.2.3 <2.0.0 (ideal

for non-breaking)

10

Page 37: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Versions

The cool thing in composer is: You can setup your version constraint by

a huge set of rules. Each developer has his own ”scheme” about how to

provide minor/major/patch/hotfix updates, so this won’t be a problem

for you too if you setup the versions correctly.

Type Example Description

Exact1.0.2 Exact version number (and

only this version)

Range>=1.0 <1.9 A simple version range

Range (-)1.0 - 2.0 The same as >=1.0.0 <2.1

Wildcard1.0.* Equal to >=1.0 <1.1

Tilde˜1.2.3 Like >=1.2.3 <1.3.0

Caretˆ1.2.3 Like >=1.2.3 <2.0.0 (ideal

for non-breaking)

10

Page 38: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Removing dependencies and global

installation

Removing dependencies is as easy as adding them:

composer remove AUTHOR/PACKAGENAME

Sometimes, you may like to add a dependency globally, and therefore for

your whole machine.

composer global require AUTHOR/PACKAGENAME

Be aware that you need to add them to your production machine

manually then!

11

Page 39: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Removing dependencies and global

installation

Removing dependencies is as easy as adding them:

composer remove AUTHOR/PACKAGENAME

Sometimes, you may like to add a dependency globally, and therefore for

your whole machine.

composer global require AUTHOR/PACKAGENAME

Be aware that you need to add them to your production machine

manually then!

11

Page 40: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Removing dependencies and global

installation

Removing dependencies is as easy as adding them:

composer remove AUTHOR/PACKAGENAME

Sometimes, you may like to add a dependency globally, and therefore for

your whole machine.

composer global require AUTHOR/PACKAGENAME

Be aware that you need to add them to your production machine

manually then!

11

Page 41: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Where to find package names

Sometimes you may have an idea what packages you need for your

applications, but you don’t know the name(s)... So, what can we do?

• Look up on their git repository

• Use composer search NAME

• Search on https://packagist.org/

12

Page 42: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Where to find package names

Sometimes you may have an idea what packages you need for your

applications, but you don’t know the name(s)... So, what can we do?

• Look up on their git repository

• Use composer search NAME

• Search on https://packagist.org/

12

Page 43: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Where to find package names

Sometimes you may have an idea what packages you need for your

applications, but you don’t know the name(s)... So, what can we do?

• Look up on their git repository

• Use composer search NAME

• Search on https://packagist.org/

12

Page 44: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Where to find package names

Sometimes you may have an idea what packages you need for your

applications, but you don’t know the name(s)... So, what can we do?

• Look up on their git repository

• Use composer search NAME

• Search on https://packagist.org/

12

Page 45: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Autoloading

One of the greatest features of composer? It creates an Autoloader for all

dependencies! You can require the vendor/autoload.php file and you

are good to go!

(All dependencies are saved in /vendor. You must not need commit this

file to your VCS)

13

Page 46: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Autoloading

One of the greatest features of composer? It creates an Autoloader for all

dependencies! You can require the vendor/autoload.php file and you

are good to go!

(All dependencies are saved in /vendor. You must not need commit this

file to your VCS)

13

Page 47: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Basic usage of composer - Let’s require!

Okay! For our next topics we need a library called ”Collect”. It is a split

from the Laravel Framework Collection package and will help us to deal

with data well.

Let’s require it for our project! It’s called tightenco/collect

14

Page 48: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

JSON

Page 49: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

What is JSON

JSON stands for JavaScript Object Notation. It’s a human-readable data

transmission format, that is lightweight and language-independent.

Because it is text-only, it is widely used for communicating between

browser and server through an API. It can also easily transmit objects,

arrays and simple variables.

1 //An JSON ob j e c t :

2 { ”name” : ”Max” , ” age” : 21 , ” c i t y ” : ”San Jose ” } ;

15

Page 50: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

What is JSON

JSON stands for JavaScript Object Notation. It’s a human-readable data

transmission format, that is lightweight and language-independent.

Because it is text-only, it is widely used for communicating between

browser and server through an API. It can also easily transmit objects,

arrays and simple variables.

1 //An JSON ob j e c t :

2 { ”name” : ”Max” , ” age” : 21 , ” c i t y ” : ”San Jose ” } ;

15

Page 51: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

PHP and JSON

PHP has built-in methods to en- and decode json:

1 $ob j e c t = j s on d e cod e ( ’ { ”name ” :”Max” , ” age ” : 21 , ” c i t y

” : ” San Jose ” } ’ ) ;2 echo $ob j e c t−>name ; //Max

3 var dump ( $ob j e c t ) ;

4

5 echo j s on en cod e ( $ ob j e c t ) ; // Retu rns s t r i n g from above

6

16

Page 52: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

DD

Before we will go through the next tasks, I suggest you another package

called larapack/dd. It comes with a function called dd(), which is a

”die and dump” aka. debug function. It’ll make your life easier when

working with arrays.

How to use it:

1 $ob j e c t = j s on d e cod e ( ’ { ”name ” :”Max” , ” age ” : 21 , ” c i t y

” : ” San Jose ” } ’ ) ;2 dd ( $ob j e c t ) ;

3

17

Page 53: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

DD

Before we will go through the next tasks, I suggest you another package

called larapack/dd. It comes with a function called dd(), which is a

”die and dump” aka. debug function. It’ll make your life easier when

working with arrays.

How to use it:

1 $ob j e c t = j s on d e cod e ( ’ { ”name ” :”Max” , ” age ” : 21 , ” c i t y

” : ” San Jose ” } ’ ) ;2 dd ( $ob j e c t ) ;

3

17

Page 54: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Tasks!!!111eleven

I’ve set up a little JSON file called tracks.json, that can be found on

https://raw.githubusercontent.com/fsr/php-lessons/master/

code/tracks.json

It contains tracks that were downloaded by users of CloudCatcher

(www.cloudcatcher.de)

Your first task is to decode the JSON file, without downloading it locally,

and printing out the object!

Now, iterate over each element and print out the title!

And finally, reduce your title output to those tracks that have a track

number starting with 165583

18

Page 55: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Tasks!!!111eleven

I’ve set up a little JSON file called tracks.json, that can be found on

https://raw.githubusercontent.com/fsr/php-lessons/master/

code/tracks.json

It contains tracks that were downloaded by users of CloudCatcher

(www.cloudcatcher.de)

Your first task is to decode the JSON file, without downloading it locally,

and printing out the object!

Now, iterate over each element and print out the title!

And finally, reduce your title output to those tracks that have a track

number starting with 165583

18

Page 56: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Tasks!!!111eleven

I’ve set up a little JSON file called tracks.json, that can be found on

https://raw.githubusercontent.com/fsr/php-lessons/master/

code/tracks.json

It contains tracks that were downloaded by users of CloudCatcher

(www.cloudcatcher.de)

Your first task is to decode the JSON file, without downloading it locally,

and printing out the object!

Now, iterate over each element and print out the title!

And finally, reduce your title output to those tracks that have a track

number starting with 165583

18

Page 57: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Tasks!!!111eleven

I’ve set up a little JSON file called tracks.json, that can be found on

https://raw.githubusercontent.com/fsr/php-lessons/master/

code/tracks.json

It contains tracks that were downloaded by users of CloudCatcher

(www.cloudcatcher.de)

Your first task is to decode the JSON file, without downloading it locally,

and printing out the object!

Now, iterate over each element and print out the title!

And finally, reduce your title output to those tracks that have a track

number starting with 165583

18

Page 58: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Tasks!!!111eleven

I’ve set up a little JSON file called tracks.json, that can be found on

https://raw.githubusercontent.com/fsr/php-lessons/master/

code/tracks.json

It contains tracks that were downloaded by users of CloudCatcher

(www.cloudcatcher.de)

Your first task is to decode the JSON file, without downloading it locally,

and printing out the object!

Now, iterate over each element and print out the title! BONUS: Create

a array that only contains the titles before!

And finally, reduce your title output to those tracks that have a track

number starting with 165583

19

Page 59: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Tasks!!!111eleven

I’ve set up a little JSON file called tracks.json, that can be found on

https://raw.githubusercontent.com/fsr/php-lessons/master/

code/tracks.json

It contains tracks that were downloaded by users of CloudCatcher

(www.cloudcatcher.de)

Your first task is to decode the JSON file, without downloading it locally,

and printing out the object!

Now, iterate over each element and print out the title! BONUS: Create

a array that only contains the titles before!

And finally, reduce your title output to those tracks that have a track

number starting with 165583

19

Page 60: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Tasks!!!111eleven

I’ve set up a little JSON file called tracks.json, that can be found on

https://raw.githubusercontent.com/fsr/php-lessons/master/

code/tracks.json

It contains tracks that were downloaded by users of CloudCatcher

(www.cloudcatcher.de)

Your first task is to decode the JSON file, without downloading it locally,

and printing out the object!

Now, iterate over each element and print out the title!

BONUS: Create

a array that only contains the titles before!

And finally, reduce your title output to those tracks that have a track

number starting with 165583

19

Page 61: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Tasks!!!111eleven

I’ve set up a little JSON file called tracks.json, that can be found on

https://raw.githubusercontent.com/fsr/php-lessons/master/

code/tracks.json

It contains tracks that were downloaded by users of CloudCatcher

(www.cloudcatcher.de)

Your first task is to decode the JSON file, without downloading it locally,

and printing out the object!

Now, iterate over each element and print out the title! BONUS: Create

a array that only contains the titles before!

And finally, reduce your title output to those tracks that have a track

number starting with 165583

19

Page 62: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Tasks!!!111eleven

I’ve set up a little JSON file called tracks.json, that can be found on

https://raw.githubusercontent.com/fsr/php-lessons/master/

code/tracks.json

It contains tracks that were downloaded by users of CloudCatcher

(www.cloudcatcher.de)

Your first task is to decode the JSON file, without downloading it locally,

and printing out the object!

Now, iterate over each element and print out the title! BONUS: Create

a array that only contains the titles before!

And finally, reduce your title output to those tracks that have a track

number starting with 165583

19

Page 63: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017

Collections (next lesson)

Page 64: PHP-Einführung - Lesson 8 - Composer (Dependency manager) … · 2017. 6. 27. · PHP-Einfuhrung - Lesson 8 - Composer (Dependency manager) and JSON Alexander Lichter June 27, 2017