PHP code example of monkeyscloud / monkeyslegion-skeleton
1. Go to this page and download the library: Download monkeyscloud/monkeyslegion-skeleton 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/ */
monkeyscloud / monkeyslegion-skeleton example snippets
// public/index.php — the entire entry point
declare(strict_types=1);
)->run();
use MonkeysLegion\Entity\Attributes\Entity;
use MonkeysLegion\Entity\Attributes\Field;
use MonkeysLegion\Entity\Attributes\Id;
use MonkeysLegion\Entity\Attributes\Timestamps;
use MonkeysLegion\Entity\Casts\CastManager;
use MonkeysLegion\Auth\Contract\AuthenticatableInterface;
#[Entity(table: 'users')]
#[Timestamps]
final class User implements AuthenticatableInterface
{
#[Id]
#[Field(type: 'unsignedBigInt', autoIncrement: true)]
public private(set) int $id;
#[Field(type: 'string', length: 255, unique: true)]
public string $email {
set(string $value) { $this->email = strtolower(trim($value)); }
}
#[Field(type: 'string', length: 100)]
public string $name {
set(string $value) {
if (strlen($value) === 0) {
throw new \InvalidArgumentException('Name cannot be empty');
}
$this->name = $value;
}
}
#[Field(type: 'string', length: 255)]
public string $password_hash;
#[Field(type: 'datetime', nullable: true)]
public ?\DateTimeImmutable $email_verified_at = null;
#[Field(type: 'integer')]
public int $token_version = 1;
// Computed properties — no backing field, no DB column
public string $displayName {
get => "{$this->name} <{$this->email}>";
}
public bool $isVerified {
get => $this->email_verified_at !== null;
}
}
use MonkeysLegion\Entity\Casts\{JsonCast, BooleanCast, DatetimeCast, EnumCast};
// In your entity:
#[Field(type: 'json')]
#[Cast(JsonCast::class)]
public array $metadata = [];
#[Field(type: 'boolean')]
#[Cast(BooleanCast::class)]
public bool $active = true;
#[Field(type: 'string')]
#[Cast(EnumCast::class, enum: UserRole::class)]
public UserRole $role = UserRole::User;
use MonkeysLegion\DI\Attributes\Singleton;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
#[Singleton]
final class UserService
{
public function __construct(
private readonly UserRepository $users,
private readonly EventDispatcherInterface $events,
private readonly LoggerInterface $logger,
) {}
public function createUser(CreateUserRequest $dto): User
{
$user = new User();
$user->email = $dto->email;
$user->name = $dto->name;
$user->password_hash = password_hash($dto->password, PASSWORD_DEFAULT);
$this->users->persist($user);
$this->events->dispatch(new UserCreated($user));
$this->logger->info('User created', ['email' => $user->email]);
return $user;
}
}
use MonkeysLegion\Router\Attributes\Route;
use MonkeysLegion\Router\Attributes\RoutePrefix;
use MonkeysLegion\Router\Attributes\Middleware;
use MonkeysLegion\Router\Attributes\ApiVersion;
use MonkeysLegion\Router\Attributes\Deprecated;
use MonkeysLegion\Auth\Attribute\Authenticated;
use MonkeysLegion\Auth\Attribute\RequiresRole;
use MonkeysLegion\Http\Message\Response;
#[RoutePrefix('/api/v2/users')]
#[Middleware(['cors', 'throttle:60,1'])]
#[ApiVersion('2')]
final class UserController
{
#[Route('GET', '/', name: 'users.index', summary: 'List users', tags: ['Users'])]
public function index(ServerRequestInterface $request): Response
{
return UserResource::collection($this->users->findActiveUsers());
}
#[Route('POST', '/', name: 'users.create')]
#[Authenticated]
#[RequiresRole('admin')]
public function create(CreateUserRequest $dto): Response
{
$user = $this->service->createUser($dto);
return UserResource::make($user, 201);
}
#[Route('DELETE', '/{id:\d+}', name: 'users.destroy')]
#[Authenticated]
#[RequiresRole('admin')]
#[Deprecated(sunset: '2026-12-31', link: '/docs/api/v3')]
public function destroy(string $id): Response
{
$this->service->deleteUser((int) $id);
return Response::noContent();
}
}
use MonkeysLegion\Auth\OAuth\OAuthManager;
#[Route('GET', '/oauth/{provider}', name: 'oauth.redirect')]
public function redirect(string $provider, OAuthManager $oauth): Response
{
return $oauth->provider($provider)->redirect();
}
#[Route('GET', '/oauth/{provider}/callback', name: 'oauth.callback')]
public function callback(string $provider, OAuthManager $oauth): Response
{
$user = $oauth->provider($provider)->user();
$localUser = $this->userProvider->findOrCreateFromOAuth($provider, $user);
// Generate JWT or session...
return Response::redirect('/dashboard');
}
use MonkeysLegion\Inertia\Inertia;
// In a controller — returns an Inertia response
return Inertia::render('Users/Index', [
'users' => $users,
'filters' => $request->getQueryParams(),
]);
// Lazy props (only loaded when needed)
return Inertia::render('Dashboard', [
'stats' => Inertia::lazy(fn() => $this->stats->compute()),
]);
// SSR is automatic when enabled in config/inertia.mlc
{{-- In templates --}}
@vite(['resources/css/app.css', 'resources/js/app.tsx'])
use MonkeysLegion\FeatureFlags\FeatureManager;
if ($flags->isEnabled('new_checkout_flow')) {
return $this->renderer->render('checkout.v2');
}
return $this->renderer->render('checkout.v1');
// Route-level middleware
#[Route('GET', '/beta', name: 'beta')]
#[Middleware(['feature:new_checkout_flow'])]
public function beta(): Response { }
use MonkeysLegion\Notifications\NotificationDispatcher;
use MonkeysLegion\Notifications\Messages\SlackMessage;
$notifier->send('alerts', new SlackMessage(
channel: '#alerts',
text: 'Deploy completed successfully',
));
use MonkeysLegion\Webhooks\WebhookManager;
$webhooks->dispatch('order.created', [
'order_id' => $order->id,
'total' => $order->total,
]);
// HMAC-signed, retried with exponential backoff
use MonkeysLegion\Markdown\MarkdownRenderer;
$html = $renderer->render('# Hello **World**');
use MonkeysLegion\Search\SearchManager;
$results = $search->index('posts')->search('monkeyslegion', limit: 10);
use MonkeysLegion\Testing\TestCase;
use MonkeysLegion\Testing\Concerns\RefreshDatabase;
final class UserApiTest extends TestCase
{
use RefreshDatabase;
public function test_create_user(): void
{
$response = $this->post('/api/users', [
'name' => 'Bob',
'email' => '[email protected]',
]);
$response->assertStatus(201);
$response->assertJsonPath('data.name', 'Bob');
}
}
bash
php tests/Performance/benchmark_detailed.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.