catpaw/catpaw

The catpaw project


Keywords
php, http, cli, di, server, async, amphp, catpaw
License
MIT

Documentation

What is this?

Catpaw is an opinionated dependency injection library that comes with batteries included for developing asynchronous and declarative general purpose programs.
It leverages php attributes to provide declarative apis, and the amphp platform to make your program asynchronous.

Table of Contents Description
📦 Container Provide dependencies and retrieve them.
📦 Constructors Execute code when a dependency is resolved.
📦 Entry Execute code when a dependency is resolved.
⚠️ Error Management Manage errors.
🌠 Server Start a server.
🚆 Server Router Define routes.
📃 Server Path Parameters Define path parameters for your routes.
🎫 Server Session Manage sessions.
📞 Server Websockets Serve websockets.
💠 Server Open Api Generate an Open Api definition.
🎛️ Command Create a console command.
🗄️ Database Connect to a database and send queries.
🗄️ Stores Store data in memory and react to changes in said data.
🚥 Queues Create in memory queues and tag them.
🚥 Signals Create signals and react to them.
🕐 Schedule Schedule code execution using a human readable format.
💡 RaspberryPi Control your RaspberryPi's GPIOs.

Note

This project is aimed at linux distributions, some features may or not may work on Windows and/or MacOS.
Feel free to contribute fixing issues for specific platforms.

Get started

You will need at least php 8.3.

Create a new project using one of the starter templates.

You can start from scratch

composer create-project catpaw/starter

or you can start with a web server

composer create-project catpaw/web-starter

Program Structure

Every program must declare a main function in the global scope, that will be your entry point.

// src/main.php
use Psr\Log\LoggerInterface;
function main(LoggerInterface $logger){
  $logger->info("hello world");
}

You can run your program in one of three modes.

Development Mode

Enter Development Mode with

make dev

This mode will run your program with XDebug enabled.

Watch Mode

Enter Watch Mode with

make watch

This mode will run your program with XDebug enabled and it will restart your program every time you make a change to your source code.

Note

By default "source code" means the "src" directory.
You can change this configuration in your makefile, see section watch, parameter resources.

Production Mode

Enter Production Mode with

make start

It's just as it sounds, run your program directly.
No debuggers, not extra overhead.

Build

It is possible, but no required, to bundle your program into a single .phar file with

make build

The building process can be configured inside the build.ini file.

After building your application, you can simply run it using

php out/app.phar

The resulting .phar will include the following directories

  • src
  • vendor
  • .build-cache (created at build time)

It's a portable bundle, you just need to make sure php is installed on whatever machine you're trying to run it on.

Debugging with VSCode

Install xdebug

apt install php8.3-xdebug

Configure your .vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen",
            "type": "php",
            "request": "launch",
            "port": 9003
        }
    ]
}

Start debugging.