Search by

oihana / php-commands

ooop

The Oihana PHP Commands library

Package info

github.com/BcommeBois/oihana-php-commands

pkg:composer/oihana/php-commands

Statistics

Installs: 1 199

Dependents: 7

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.4 2026-06-21 16:54 UTC

This package is auto-updated.

Last update: 2026-11-29 14:31:48 UTC


README

Oihana PHP Commands

Build robust, scriptable command-line tooling for servers and web applications. Part of the Oihana PHP ecosystem, this package gives you a consistent foundation to automate day‑to‑day operations with confidence.

Latest Version Total Downloads License

📚 Documentation

Full documentation: https://bcommebois.github.io/oihana-php-commands

📦 Installation

Requires PHP 8.4+. Install via Composer:

composer require oihana/php-commands

✨ What you can do

  • Manage MySQL databases: create users, grant privileges, back up and restore data.
  • Automate backups: schedule and execute incremental or full backups for databases and apps.
  • Install and update applications: deploy, upgrade, and maintain projects reliably.
  • Control caches: clear and manage systems like Memcached and Redis.
  • Handle SSL certificates: automate issuance and renewal with Let’s Encrypt/Certbot.
  • Configure NGINX: generate modular server configs and reload services.
  • Schedule jobs: keep a crontab in line with the jobs an application declares.
  • Send heartbeats: tell a monitoring tool the machine is alive, and warn when the tool does not answer.

Under the hood

  • First‑class integration with Symfony Console for building custom CLI commands.
  • Ready‑made plugins for databases, caches, web servers, certificates, and more.
  • A flexible options system based on the PSR‑11 Container.
  • An extensible architecture to add your own tasks, workflows, and plugins.

Ideal for DevOps automation, hosting management, CI/CD pipelines, and local or production environment setup.

⏰ Scheduled jobs

CronCommand keeps the crontab of the current user in line with the jobs an application declares. It owns only the lines that end with its marker, # oihana-cron:<scope>:<name>: everything else in the crontab stays as it is. An application can choose its own word for the marker (CronParam::MARKER => 'acme-tasks' gives # acme-tasks:<scope>:<name>): the lines of one word never see the lines of another.

use oihana\commands\cron\CronCommand;
use oihana\commands\enums\CronParam;

$command = new CronCommand( 'app:cron' , $container ,
[
    CronParam::SCOPE   => 'myapp-staging' ,
    CronParam::BACKUPS => '/srv/app/var/crontab' ,
    CronParam::JOBS    =>
    [
        [
            'name'     => 'check' ,
            'schedule' => '0 6 * * 1-6' ,
            'command'  => '/usr/bin/php /srv/app/bin/console.php app:check' ,
            'log'      => '/srv/app/logs/cron/check.log' ,
        ] ,
    ] ,
]) ;
bin/console app:cron list            # the jobs, and what sync would do
bin/console app:cron sync            # the changes the crontab needs — nothing is written
bin/console app:cron sync --apply    # copy the crontab, then write the changes
bin/console app:cron sync --check    # ends on 1 when the crontab is not up to date — nothing is written
bin/console app:cron disable check   # comment the line out, until the next sync

Cron starts its jobs with a minimal environment: write commands with absolute paths.

💓 Heartbeat

A job that fails can send an alert; a machine where nothing runs any more — stopped, its scheduler down, its crontab emptied — fails at nothing and stays silent. A monitoring tool on another machine (Uptime Kuma, Healthchecks…) closes the gap: it gives a push address and waits for a call every few minutes; when the calls stop, it alerts.

HeartbeatCommand makes that call. Its signals are names → push addresses, called as they are; the HTTP client and the request factory are yours (PSR-18, PSR-17):

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;

use oihana\commands\enums\HeartbeatParam;
use oihana\commands\heartbeat\HeartbeatCommand;

$command = new HeartbeatCommand( 'app:heartbeat' , $container ,
[
    HeartbeatParam::SIGNALS         => [ 'cron' => 'https://monitor.example.com/api/push/AbCdEf1234?status=up' ] ,
    HeartbeatParam::CLIENT          => new Client([ 'timeout' => 10 ]) ,
    HeartbeatParam::REQUEST_FACTORY => new HttpFactory() ,
    HeartbeatParam::STATE_DIR       => '/srv/app/var/heartbeat' ,
    HeartbeatParam::ALERT_EVERY     => 3600 ,
]) ;
*/5 * * * * /usr/bin/php /srv/app/bin/console.php app:heartbeat cron || /srv/app/bin/alert.sh

The call watches the tool the other way round too. When the tool does not answer — a timeout, a refused connection, a status out of 2xx, or a JSON body whose ok is false —, the command ends on 1, so the job can alert: once when the failure starts, then once every alertEvery seconds at most; the passes in between end on 0. The failure is remembered in a small file per signal of stateDir (HeartbeatState).

Exit When
0 the tool took the signal; or the signal has no address; or no alert is due yet
1 the tool does not answer and an alert is due
2 the signal is not a name, or no HTTP client or request factory was given

🔒 A push address is a secret: the command only prints its scheme, host and port, and replaces the address and its last segment (the token) by *** in an error message.

🧭 A console built on demand

A console that builds all its commands at boot stops whole when one of them cannot be built — a command that opens a database connection when it is built, while the database does not answer. commandFactories() gives the console a directory instead: the name of each command and the function that builds it. Symfony builds only the command that is run:

use Symfony\Component\Console\Application;
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;

use function oihana\commands\helpers\commandFactories;

$application = new Application() ;
$application->setCommandLoader( new FactoryCommandLoader( commandFactories( $container , [ 'app:backup' , 'app:heartbeat' ] ) ) ) ;
$application->run() ;

A command that cannot be built fails alone, inside Application::run(), which shows the error and ends on 1; the others still run. The key of each command in the container must be its name. A name the container does not know is left out; a definition that does not give a command fails when it is built, saying which one.

🔌 Plugins

Each plugin extends the CLI with focused, production‑ready capabilities.

🔌 Plugin Description
🔏 Oihana PHP Certbot Create, modify and manage Let’s Encrypt certificates via Certbot.
🧠 Oihana PHP Memcached Manage Memcached in‑memory key‑value caching.
🌐 Oihana PHP Nginx Create, modify and control NGINX configurations and commands.
🤖 Oihana PHP Robots Create or remove a robots.txt file from the CLI.

🔏 Certbot

Oihana PHP Certbot

Toolkit to create, modify and manage Let’s Encrypt certificates via the Certbot CLI.

🔗 View the repository: https://github.com/BcommeBois/oihana-php-certbot

🧠 Memcached

Oihana PHP Memcached

CLI to control Memcached using the official Memcached PHP extension.

🔗 View the repository: https://github.com/BcommeBois/oihana-php-memcached

🌐 Nginx

Oihana PHP Nginx

Simple CLI to manage NGINX and create/remove configuration files on your server.

🔗 View the repository: https://github.com/BcommeBois/oihana-php-nginx

🤖 Robots

Oihana PHP Robots

CLI to create or remove a website’s robots.txt file.

🔗 View the repository: https://github.com/BcommeBois/oihana-php-robots

✅ Running tests

Run all tests:

composer test

Run a specific test file:

composer test ./tests/oihana/commands/ProcessTest.php

🛠️ Generate the documentation

We use phpDocumentor to generate documentation into the ./docs folder.

composer doc

🧾 License

Licensed under the Mozilla Public License 2.0 (MPL‑2.0).

👤 About the author

  • Author: Marc ALCARAZ (aka eKameleon)
  • Email: marc@ooop.fr
  • Website: https://www.ooop.fr

🔗 Related packages

  • oihana/php-core – core helpers and utilities used by this library: https://github.com/BcommeBois/oihana-php-core
  • oihana/php-reflect – reflection and hydration utilities: https://github.com/BcommeBois/oihana-php-reflect
  • oihana/php-system – common helpers of the Oihana PHP framework: https://github.com/BcommeBois/oihana-php-system
  • oihana/php-schema – object‑oriented implementation of the Schema.org vocabulary: https://github.com/BcommeBois/oihana-php-schema
  • oihana/php-standards – constants and helpers based on major international standards (ISO, UN, UN/CEFACT, etc.): https://github.com/BcommeBois/oihana-php-standards