Przejdź do głównej zawartości

PHP composer na hostingu? Tak! Jak zacząć?

Composer to narzędzie do zarządzania zależnościami w PHP. Pozwala zadeklarować biblioteki, od których zależy Twój projekt, i będzie nimi zarządzać (instalować/zaktualizować) za Ciebie. Composer zarządza komponentami osobno dla każdego projektu, instalując je w katalogu wewnątrz twojego projektu. To taki menedżer zależności.

Tak, w usłudze hostingu WWW jest zainstalowany pakiet composer dla PHP.

[user@h5el ~]$ composer -V
Composer version 2.3.10 2022-07-13 15:48:23
[user@el3 ~]$ composer -V
Composer version 2.4.0 2022-08-16 16:10:48

putty ssh Pakiet PHP composer poprzez SSH.

Nowy projekt w composer PHP

Przedstawimy przykładowy projekt wykorzystania php composer do szybkiego wykorzystania z np. biblioteką zliczania czasu php-timer. Sprawdź Jak zalogować się do SSH?

[adam@el3 tmp]$ mkdir projekt-czas
[adam@el3 tmp]$ cd projekt-czas/

Załóżmy że rozpoczynamy projekt za pomocą kreatora composer init, jako zależności podamy bibliotekę phpunit/php-timer

[adam@el3 projekt-czas]$ composer init


Welcome to the Composer config generator



This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [adam/projekt-czas]:
Description []: Testowo czasowo
Author [n to skip]: HitMe
Minimum Stability []:
Package Type (e.g. library, project, metapackage, composer-plugin) []: project
License []:

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]?
Search for a package: php-timer
Info from https://repo.packagist.org: #StandWithUkraine

Found 15 packages matching php-timer

[0] phpunit/php-timer
[1] ayesh/php-timer
[2] jenner/timer
[3] demi/php-timelog
[4] lucasruroken/simple-php-timer
[5] mrtwenty/timer
[6] marshmallow/laravel-php-timer
[7] romanvazhynskyi/php-timer
[8] markuszeller/php-timer
[9] izabolotnev/php-timer
[10] alecrabbit/php-timers
[11] sunsgne/timer
[12] nicklayb/timer
[13] mts7/php-execution-timer
[14] kanel/timer

Enter package # to add, or the complete package name if it is not listed: phpunit/php-timer
Enter the version constraint to require (or leave blank to use the latest version): ^5.0
Search for a package:
Would you like to define your dev dependencies (require-dev) interactively [yes]? n
Add PSR-4 autoload mapping? Maps namespace "Adam\ProjektCzas" to the entered relative path. [src/, n to skip]: y
The src folder name "y" is invalid. Please add a relative path with tailing forward slash. [A-Za-z0-9_-/]+/
Add PSR-4 autoload mapping? Maps namespace "Adam\ProjektCzas" to the entered relative path. [src/, n to skip]:

{
"name": "adam/projekt-czas",
"description": "Testowo czasowo",
"type": "project",
"require": {
"phpunit/php-timer": "^5.0"
},
"autoload": {
"psr-4": {
"Adam\\ProjektCzas\\": "src/"
}
},
"authors": [
{
"name": "HitMe"
}
]
}

Do you confirm generation [yes]?
Would you like to install dependencies now [yes]? n
PSR-4 autoloading configured. Use "namespace Adam\ProjektCzas;" in src/
Include the Composer autoloader with: require 'vendor/autoload.php';

Wcześniej nie instalowaliśmy zależności, zrobimy to teraz:

composer install

[adam@el3 projekt-czas]$ composer install
No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information.
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking phpunit/php-timer (5.0.3)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Downloading phpunit/php-timer (5.0.3)
- Installing phpunit/php-timer (5.0.3): Extracting archive
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!

Gdybyśmy w przyszłości chcieli szybko sprawdzić jakich zależności używa projekt używamy composer show

[adam@el3 projekt-czas]$ composer show

Direct dependencies:
phpunit/php-timer 5.0.3 Utility class for timing

Transitive dependencies:
Everything up to date

Przykładowy Projekt w composer PHP

get-czas.php
<?php
require __DIR__ . '/vendor/autoload.php';

use SebastianBergmann\Timer\ResourceUsageFormatter;
use SebastianBergmann\Timer\Timer;

$timer = new Timer;
$timer->start();

foreach (\range(0, 100000) as $i) {
// ...
}

print (new ResourceUsageFormatter)->resourceUsage($timer->stop())."\n";
[adam@el3 projekt-czas]$ php get-czas.php
Time: 00:00.003, Memory: 6.00 MB