PHP code example of apparel-php / warp

1. Go to this page and download the library: Download apparel-php/warp 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/ */

    

apparel-php / warp example snippets




$appRoot = dirname(__DIR__);
;
use Woof\Http\ResponseBuilder;
use Warp\RoutingContext;
use App\Resolver\RootResolver;

// アプリケーション環境の構築
$env = (new WebEnvironmentBuilder())
    ->setConfigDir("{$appRoot}/config")
    ->setResourcesDir("{$appRoot}/resources")
    ->build();

// HTTP リクエストの読み込み
$request = $env->getClientRequest();

// ルーティングコンテキストの生成
$context = RoutingContext::create($request, $env);

// 最上位の Resolver を構築して実行
$rootResolver = new RootResolver();
$target       = $rootResolver->resolve($context);

// 解決された Controller を取得し、リクエストを処理する
$controller = $target->unwrapOr(function () {
    // 見つからなかった場合は NotFoundController を採用する
    return new NotFoundController();
});
$response   = $controller->handle($request, $env);

// HTTP レスポンスの送信
$output = new DefaultOutput();
$output->send($response);

namespace App\Resolver;

use Warp\Resolver;
use Warp\RoutingContext;
use Warp\Target;

class RootResolver implements Resolver
{
    public function resolve(RoutingContext $context): Target
    {
        $path = $context->getPartialPath();

        // URL: /
        if ($path === "/") {
            return Target::found(new TopController());
        }
        // URL: /privacy-policy.html
        if ($path === "/privacy-policy.html") {
            return Target::found(new PrivacyPolicyController());
        }
        // URL: /sitemap.html
        if ($path === "/sitemap.html") {
            return Target::found(new SitemapController());
        }

        // 下位の Resolver に処理を移譲
        $segment = $context->peek();

        // URL: /articles/... 配下
        if ($segment === "articles") {
            return (new ArticlesResolver())->resolve($context->shift());
        }

        // URL: /admin/... 配下
        if ($segment === "admin") {
            return (new AdminResolver())->resolve($context->shift());
        }

        // 存在しなかった場合
        return Target::empty();
    }
}

namespace App\Resolver\Admin;

use Warp\Resolver;
use Warp\RoutingContext;
use Warp\Target;

class AdminResolver implements Resolver
{
    public function resolve(RoutingContext $context): Target
    {
        $path = $context->getPartialPath();

        // 完全一致でのルーティング
        // URL: /admin/
        if ($path === "/") {
            return Target::found(new DashboardController());
        }
        // URL: /admin/login/
        if ($path === "/login/") {
            return Target::found(new LoginController());
        }
        // URL: /admin/config/
        if ($path === "/config/") {
            return Target::found(new ConfigController());
        }

        // URL: /admin/posts/... 配下
        $segment = $context->peek();

        if ($segment === "posts") {
            return (new AdminPostsResolver())->resolve($context->shift());
        }

        return Target::empty();
    }
}

namespace App\Resolver\Admin;

use Warp\Resolver;
use Warp\RoutingContext;
use Warp\Target;

class AdminPostsResolver implements Resolver
{
    public function resolve(RoutingContext $context): Target
    {
        // URL: /admin/posts/ (記事一覧)
        if ($context->getPartialPath() === "/") {
            return Target::found(new PostListController());
        }

        // URL: /admin/posts/{id}/edit/, /admin/posts/{id}/remove/ など (記事の各操作)
        $match = $context->matchPath("/{id}/{action}/");
        if ($match->isMatched()) {
            return Target::found(new PostItemActionController($match->get("id"), $match->get("action")));
        }

        // URL: /admin/posts/{id}/ (記事の詳細確認)
        $matchView = $context->matchPath("/{id}/");
        if ($matchView->isMatched()) {
            return Target::found(new PostItemActionController($matchView->get("id"), "view"));
        }

        return Target::empty();
    }
}

namespace App\Controller\Admin;

use Woof\Http\Request;
use Woof\Web\WebEnvironment;
use Woof\Web\Controller;

class PostItemActionController implements Controller
{
    private $id;
    private $action;

    public function __construct(string $id, string $action)
    {
        $this->id     = $id;
        $this->action = $action;
    }

    public function handle(Request $req, WebEnvironment $env)
    {
        // 1. 実行フェーズでエンティティを取得 (DB アクセス用のクラスが別途実装されている前提)
        $postStorage = new PostStorage($env);
        $post        = $postStorage->get($this->id);

        // 2. 共通の前処理: 存在しない場合は 404 Not Found
        if ($post === null) {
            return (new NotFoundController())->handle($req, $env);
        }

        // 3. アクションに応じたコントローラの生成と委譲
        switch ($this->action) {
            case "view":
                return (new PostViewController($post))->handle($req, $env);
            case "edit":
                return (new PostEditController($post))->handle($req, $env);
            case "remove":
                return (new PostRemoveController($post))->handle($req, $env);
            default:
                // 想定外の action が指定された場合は 404 Not Found
                return (new NotFoundController())->handle($req, $env);
        }
    }
}

use Warp\Standard\CanonicalPathResolver;
use App\Resolver\RootResolver;

// (中略)

// RootResolver を CanonicalPathResolver でラップして実行する
$rootResolver = new CanonicalPathResolver(new RootResolver());
$target       = $rootResolver->resolve($context);

namespace App\Controller;

use Woof\Http\Request;
use Woof\Web\Controller;
use Woof\Web\Operator;
use Woof\Web\WebEnvironment;
use Warp\Standard\RedirectController;

class SamplePageController implements Controller
{
    public function handle(Request $req, WebEnvironment $env)
    {
        // 特定の条件を満たした場合は新しいページ (/new-page/) へ 302 リダイレクトする
        if (some_redirect_condition()) {
            return (new RedirectController("/new-page/"))->handle($req, $env);
        }

        // 以後、通常時の処理
        $operator = new Operator($req, $env);
        // (中略)
        return $operator->build();
    }
}
bash
composer