PhpPackage

Aus Info-Theke
Version vom 1. Dezember 2024, 18:17 Uhr von Hamatoma (Diskussion | Beiträge) (→‎Kochbuch)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Zur Navigation springen Zur Suche springen


Links

Zielsetzung

Leitfaden, wie ein PHP-Package erzeugt wird, wie es eingebunden wird.

Installation

apt install composer

Erzeugen eines Php-Packages

Das Erzeugen erfolgt interaktiv:

composer init

Wichtig für Benennen des Packages:

  • Paketname: hamatoma/my_super_package Namespace: Hamatoma/MySuperPackage
  • Paketname: hamatoma-only/my-super-package Namespace: HamatomaOnly\MySuperPackage
Package name (<vendor>/<name>) [hamatoma/my_super_package]: # Kleinbuchstaben. Namespace:
Description: My super Package
Author: Hamatoma <author@hamatoma.de>
Minimum Stability: dev # oder  alpha, beta, RC, stable
Package Type (e.g. library, project, metapackage, composer-plugin): library
License []: CC0 1.0 Universal
Would you like to define your dependencies (require) interactively: yes
Search for a package: the_ultimate_package
 Found 3 packages matching the_ultimate_package
   [0] fjerbi/ultimateblog 
   [1] assegaiphp/validation 
   [2] ingress-it-solutions/laravel-google-my-business 
Enter the version constraint to require (or leave blank to use the latest version):
...
Would you like to define your dev dependencies (require-dev) interactively: no

Add PSR-4 autoload mapping? Maps namespace "Hamatoma\MySuperPackage" to the path [src/ n to skip]: /src
Do you confirm generation [yes]
Would you like to install dependencies now [yes]?

Kochbuch

Aktualisieren

composer update
composer update --no-dev

Version festlegen

composer require symfony/mailer 7.0.7

Entwicklung-Produktion

Autoloaderdaten generieren

composer dump-autoload

Einbinden in Startseite index.php

require_once 'Hamatoma/MySuperPackage';
$class = new \Hamatoma\MySuperPackage\MyClass();

Einbinden in Projekt MyPhpProject

composer require hamatoma/my_super_package

Einbinden eines lokalen Pakets

  • composer.json
composer config repositories.laraknife '{"type": "path", "url": "../laraknife", "options": {"symlink": true}}'
  • Das ergibt:
"repositories": [
    {
        "type": "path",
        "url": "../my-package",
        "options": {
            "symlink": true
        }
    }
],
...
"minimum-stability": "dev",

Einbinden eines Github-Repository

  • composer.json
  • Der Branch wird in "require" hinter "dev-" angegeben, z.B. "dev-master"
composer config repositories.laraknife vcs https://github.com/hamatoma/laraknife
# Branch main:
composer require hamatoma/laraknife:dev-main
  • Das ergibt dann:
    "repositories": [
        "laraknife": {
            "type": "vcs",
            "url": "https://github.com/hamatoma/laraknife"
        }
    ],
    "require": {
        "hamatoma/laraknife": "dev-main"
    }
}
...
"minimum-stability": "dev",