PHP code example of rayfunghk / razy

1. Go to this page and download the library: Download rayfunghk/razy library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rayfunghk / razy example snippets


return [
    'domains' => [
        'example.com' => [
            '/' => 'mysite@v2',                // standard mapping with tag
        ],
    ],
];

// sites/mysite/dist.php
return [
    'dist' => 'mysite',
    'modules' => [ /* ... */ ],
    'config_mapping' => [
        'localhost'       => 'local',         // loads sites/mysite:local/dist.php
        'example.com@v2'  => 'prod',          // loads sites/mysite:prod/dist.php
        'staging.com'     => '!/var/www/cfg',  // absolute path override
    ],
];

return new class extends Controller {
    public function __onInit(Agent $agent): bool
    {
        // Register routes, APIs, events, scripts
        $agent->addLazyRoute(['dashboard' => 'dashboard']);
        $agent->addAPICommand('getUser', 'api/get_user.php');
        $agent->listen('auth/user:onLogin', 'onUserLogin');
        return true;
    }

    public function __onReady(): bool
    {
        // Safe to call APIs here — all modules are loaded
        return true;
    }
};

// Lazy: /modulecode/users/list → controller/users/list.php
$agent->addLazyRoute(['users' => ['list' => 'list']]);

// Regex: /api/user-42/profile → controller/Route.user_profile.php
$agent->addRoute('/api/user-(:d)/profile', 'user_profile');

// Provider: register in __onInit
$agent->addAPICommand('getData', 'api/get_data.php');

// Consumer: call from any handler
$result = $this->api('vendor/provider')->getData($id);

// Simple syntax generates complex SQL automatically
$stmt = $db->prepare()
    ->from('u.user-g.group[group_id]')
    ->where('u.user_id=?,!g.auths~=?')
    ->assign(['auths' => 'view', 'user_id' => 1]);

// → SELECT * FROM `user` AS `u` JOIN `group` AS `g`
//   ON u.group_id = g.group_id
//   WHERE `u`.`user_id` = 1
//   AND !(JSON_CONTAINS(JSON_EXTRACT(`g`.`auths`, '$.*'), '"view"') = 1)

   // vendor/blog/default/package.php
   return [
       'module_code'    => 'vendor/blog',
       'version'        => '1.0.0',
       'api_name'       => 'blog',
       '^7.0',
       ],
   ];
   

use Razy\PackageManager;
use Razy\PackageManager\FtpTransport;

PackageManager::setDefaultTransport(new FtpTransport(
    host: 'mirror.internal',
    username: 'deploy',
    password: 'secret',
    basePath: '/composer',
));

class MyController extends Controller
{
    use PackageTrait;

    public function __onPackageStart(array $packageInfo): bool
    {
        // Register package API for co-modules to call
        $this->registerPackageAPI('greet', fn(string $name) => "Hello, {$name}!");

        // Subscribe to package events
        $this->onPackageEvent('data:ready', fn(array $data) => $this->processData($data));

        return true; // false aborts execution
    }

    public function __onPackageExec(array $packageInfo): int
    {
        // Core logic — return value is the process exit code
        $this->emitPackageEvent('data:ready', ['key' => 'value']);
        return 0;
    }

    public function __onPackageServe(array $packageInfo): void
    {
        // Long-running — start HTTP server, event loop, etc.
        // This method should BLOCK until shutdown.
    }

    public function __onPackageStop(): void
    {
        // Cleanup: close connections, flush buffers
    }

    public function __onPackageHealthcheck(): bool
    {
        return true; // healthy
    }
}

// Package A: register an API action
$this->registerPackageAPI('transform', fn($input) => strtoupper($input));

// Package B: call it
$result = $this->callPackageAPI('vendor/a', 'transform', 'hello'); // "HELLO"

// Events: pub/sub between packages
$this->onPackageEvent('config:changed', fn($data) => $this->reload($data));
$this->emitPackageEvent('config:changed', ['key' => 'timeout']);

public function __onInit(Agent $agent): bool
{
    if (defined('RAZY_PACKAGE_MODE')) {
        // Running as a standalone package
        return true;
    }

    // Normal web mode — register routes, APIs, etc.
    $agent->addLazyRoute(['dashboard' => 'dashboard']);
    return true;
}
bash
git clone https://github.com/RayFungHK/Razy.git
cd Razy
composer install
php build.php
bash
# Build the Razy environment
php Razy.phar build

# Create a distributor
php Razy.phar init dist mysite

# Generate rewrite rules
php Razy.phar rewrite mysite
bash
php Razy.phar build                    # Build environment
php Razy.phar runapp mysite            # Interactive shell
php Razy.phar install owner/repo       # Install from GitHub
php Razy.phar pack distCode            # Package modules
php Razy.phar publish                  # Publish to repository
php Razy.phar validate distCode        # Validate & install deps
php Razy.phar bridge '{"dist":"..."}'  # Cross-distributor call
bash
   php Razy.phar compose mysite
   # → Fetches metadata from Packagist
   # → Downloads & extracts monolog/monolog ^3.0
   # → Downloads & extracts guzzlehttp/guzzle ^7.0
   # → Writes autoload/lock.json