catpaw/catpaw

The catpaw project

3.8.4 2024-07-06 13:43 UTC

This package is auto-updated.

Last update: 2024-07-06 23:32:14 UTC


README

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
Error Management Manage errors.
🌐 Server Router Create a server and define routes.
🌐 Server Path Parameters Define path parameters in your routes.
🌐 Server Open Api Generate an Open Api definition.
🌐 Server Session Create and manage server sessions.
🌐 Server Byte Range Requests Serve byte range requests.
🌐 Server Websockets Serve websockets.
🌐 Server Views Serve views using Latte.
Container Provide dependencies and retrieve them.
Entry Execute code when a dependency is resolved.
Command Create a console command.
Custom Attributes An easy way to define custom attributes with hooks that pierce into the framework.
Stores Store data in memory and react to changes in said data.
Queues Create in memory queues and tag them.
Schedule Schedule code execution using a human readable format.
Signals Create signals and react to them.
Build Build your project into one single portable file.
Go Interop Interop with Go.
💡 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.2 and the php-mbstring extension.

Create a new project using one of the starter templates.

  • you can start from scratch
    composer create-project catpaw/starter
  • you can start with a web server
    composer create-project catpaw/web-starter

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

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

After you've created your new project, you can run it using

composer dev:watch

to watch file changes (useful in development) or

composer prod:start

for production mode.

Build & Run

It is possible, but not required, to build your application into a single .phar file using

composer prod:build

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

After building your application, you can simply run it using

php app.phar

The resulting .phar, by default (check build.ini), includes the following directories:

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

which means it's a portable binary, 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.2-xdebug
  • Put this configuration in your ./.vscode/launch.json file

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Launch",
                "type": "php",
                "request": "launch",
                "program": "${workspaceRoot}/bin/start",
                "args": [
                    "--libraries='./src/lib'",
                    "--entry='./src/main.php'"
                ],
                "cwd": "${workspaceRoot}",
                "runtimeArgs": [
                    "-dxdebug.start_with_request=yes"
                ],
                "env": {
                    "XDEBUG_MODE": "debug,develop",
                    "XDEBUG_CONFIG": "client_port=${port}"
                }
            },
            {
                "name": "Listen",
                "type": "php",
                "request": "launch",
                "port": 9003
            }
        ]
    }
  • Start debugging