このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
apricot:core:applocation-class [2020/04/30 00:07] y2sunlight [Apricot コア:アプリケーションクラス] |
— (現在) | ||
---|---|---|---|
行 1: | 行 1: | ||
- | ====== Apricot アプリケーションクラス ====== | ||
- | --- // | ||
- | |||
- | [[apricot: | ||
- | |||
- | 関連記事 | ||
- | * [[apricot: | ||
- | * [[apricot: | ||
- | * Apricot コア | ||
- | * [[apricot: | ||
- | * Apricot アプリケーションクラス | ||
- | * [[apricot: | ||
- | * [[apricot: | ||
- | * [[apricot: | ||
- | |||
- | ---- | ||
- | ===== Applicationクラスの仕様 ===== | ||
- | |||
- | さて、準備が出来たのでいよいよコアのクラス群を作って行きたいと思います。最初にApplicationクラスを作成しますが、その前にApplicationクラスの責任を明確にしておきます。 | ||
- | |||
- | === Applicationクラスの責任 === | ||
- | |||
- | * アプリケーションの初期化 | ||
- | * ルーティングとセキュリティの設定 | ||
- | * ミドルウェアの設定 | ||
- | * アクションの起動 | ||
- | |||
- | Applicationクラスは、以下の公開メソッドを持っています。詳細はソースコードを参照して下さい。 | ||
- | |||
- | === フロントコントローラ(index.php)から呼び出されるメソッド === | ||
- | |||
- | ^公開メソッド^機能^ | ||
- | |setup(array $app=[])|アプリケーションのセットアップ| | ||
- | |run(callable $routeDefinitionCallback)|アプリケーションの起動| | ||
- | |||
- | === ゲッターメソッド === | ||
- | |||
- | ^公開メソッド^機能^ | ||
- | |getInstance(): | ||
- | |getSetting($dot = null, $default=null)|アプリケーション設定(app.php)の取得| | ||
- | |getProjectDir(): | ||
- | |getConfigDir(): | ||
- | |getAssetsDir(): | ||
- | |getVarDir(): | ||
- | |getPublicDirectory(): | ||
- | |getRouteBase(): | ||
- | |getControllerName(): | ||
- | |getActionName(): | ||
- | |||
- | \\ | ||
- | |||
- | ===== Applicationクラス(暫定版) ===== | ||
- | |||
- | 以下に暫定版のApplicationクラスを示します。ほとんどの機能は実装できていますが、アクションを実行するメソッド(executeAction)だけが未実装(スタブ)です。 | ||
- | |||
- | {{fa> | ||
- | <code php Application.php> | ||
- | <?php | ||
- | namespace Core; | ||
- | |||
- | /** | ||
- | * Application Class | ||
- | */ | ||
- | class Application | ||
- | { | ||
- | /** | ||
- | * Application Instance | ||
- | * var Application | ||
- | */ | ||
- | private static $instance = null; | ||
- | |||
- | /** | ||
- | * Application Setting | ||
- | * @var array | ||
- | */ | ||
- | private $app = []; | ||
- | |||
- | /* | ||
- | * Project Directories | ||
- | */ | ||
- | private $projectDir; | ||
- | private $configDir; | ||
- | private $assetsDir; | ||
- | private $varDir; | ||
- | |||
- | /* | ||
- | * Public Directory | ||
- | */ | ||
- | private $publicDir; | ||
- | |||
- | /* | ||
- | * Route Base Path | ||
- | */ | ||
- | private $routeBase; | ||
- | |||
- | /* | ||
- | * Controller Name | ||
- | */ | ||
- | private $controllerName; | ||
- | |||
- | /* | ||
- | | ||
- | */ | ||
- | private $actionName; | ||
- | |||
- | /** | ||
- | * Get Project dir | ||
- | * @return string | ||
- | */ | ||
- | public function getProjectDir(): | ||
- | |||
- | /** | ||
- | * Get config dir | ||
- | * @return string | ||
- | */ | ||
- | public function getConfigDir(): | ||
- | |||
- | /** | ||
- | * Get assets dir | ||
- | * @return string | ||
- | */ | ||
- | public function getAssetsDir(): | ||
- | |||
- | /** | ||
- | * Get var dir | ||
- | * @return string | ||
- | */ | ||
- | public function getVarDir(): | ||
- | |||
- | /** | ||
- | * Get Public Directory | ||
- | * @return string | ||
- | */ | ||
- | public function getPublicDirectory(): | ||
- | |||
- | /** | ||
- | * Get Route Base Path | ||
- | * @return string | ||
- | */ | ||
- | public function getRouteBase(): | ||
- | |||
- | /** | ||
- | * Get controller Name | ||
- | * @return string | ||
- | */ | ||
- | public function getControllerName(): | ||
- | |||
- | /** | ||
- | * Get Action Name | ||
- | * @return string | ||
- | */ | ||
- | public function getActionName(): | ||
- | |||
- | /** | ||
- | * Get Application instance. | ||
- | * @return \Core\Application | ||
- | */ | ||
- | static public function getInstance(): | ||
- | { | ||
- | if (!self:: | ||
- | { | ||
- | throw new \RuntimeException(' | ||
- | } | ||
- | return self:: | ||
- | } | ||
- | |||
- | /** | ||
- | * Create Application | ||
- | * @param string $projectDir | ||
- | * @param string $publicDir | ||
- | */ | ||
- | function __construct(string $projectDir, | ||
- | { | ||
- | if (!self:: | ||
- | { | ||
- | // Set Project Directories | ||
- | $this-> | ||
- | $this-> | ||
- | $this-> | ||
- | $this-> | ||
- | |||
- | // Set Public Directory | ||
- | $this-> | ||
- | |||
- | // Set Route Base Path | ||
- | $routeBase = dirname($_SERVER[' | ||
- | if (preg_match('/ | ||
- | $this-> | ||
- | |||
- | // Set Dotenv | ||
- | \Dotenv\Dotenv:: | ||
- | |||
- | // Set timezone | ||
- | date_default_timezone_set(env(' | ||
- | |||
- | self:: | ||
- | } | ||
- | } | ||
- | |||
- | /** | ||
- | * Get an application setting value | ||
- | * @param string|null $dot Dot-notation key | ||
- | * @param mixed $default | ||
- | * @return mixed | ||
- | */ | ||
- | public function getSetting($dot = null, $default=null) | ||
- | { | ||
- | return array_get($this-> | ||
- | } | ||
- | |||
- | /** | ||
- | * Setup Application | ||
- | * @param array $app Application Setting | ||
- | */ | ||
- | public function setup(array $app=[]) | ||
- | { | ||
- | $this-> | ||
- | |||
- | // Application setup | ||
- | if (!empty($this-> | ||
- | { | ||
- | foreach($this-> | ||
- | { | ||
- | $func = require_once $setup; | ||
- | if (!is_callable($func) || ($func()===false)) | ||
- | { | ||
- | throw new \RuntimeException(" | ||
- | } | ||
- | } | ||
- | } | ||
- | } | ||
- | |||
- | /** | ||
- | * Run Application | ||
- | * @param callable $routeDefinitionCallback | ||
- | */ | ||
- | public function run(callable $routeDefinitionCallback) | ||
- | { | ||
- | // Create Dispatcher | ||
- | $dispatcher = \FastRoute\simpleDispatcher($routeDefinitionCallback); | ||
- | |||
- | // Fetch method and URI from somewhere | ||
- | $httpMethod = $_SERVER[' | ||
- | $uri = $_SERVER[' | ||
- | |||
- | // Strip query string (?foo=bar) and decode URI | ||
- | if (false !== $pos = strpos($uri, | ||
- | { | ||
- | $uri = substr($uri, | ||
- | } | ||
- | $uri = rawurldecode($uri); | ||
- | |||
- | $routeInfo = $dispatcher-> | ||
- | switch ($routeInfo[0]) | ||
- | { | ||
- | case \FastRoute\Dispatcher:: | ||
- | abort(404, 'Page Not Found' | ||
- | break; | ||
- | |||
- | case \FastRoute\Dispatcher:: | ||
- | abort(405, ' | ||
- | break; | ||
- | |||
- | case \FastRoute\Dispatcher:: | ||
- | |||
- | $handler = $routeInfo[1]; | ||
- | $params = $routeInfo[2]; | ||
- | |||
- | if (is_callable($handler)) | ||
- | { | ||
- | // Case of callable | ||
- | $handler($params); | ||
- | } | ||
- | elseif(strpos($handler,' | ||
- | { | ||
- | // Case of Controller/ | ||
- | list($this-> | ||
- | |||
- | // Ecexute action | ||
- | $this-> | ||
- | } | ||
- | else | ||
- | { | ||
- | abort(500,' | ||
- | } | ||
- | break; | ||
- | } | ||
- | } | ||
- | |||
- | /** | ||
- | * Ecexute action | ||
- | * @param string $controllerName | ||
- | * @param string $actionName | ||
- | * @param array $params | ||
- | */ | ||
- | private function executeAction(string $controllerName, | ||
- | { | ||
- | // TODO: Stub Version | ||
- | $controller = " | ||
- | $instance = new $controller(); | ||
- | return call_user_func_array(array($instance, | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | \\ | ||
- | |||
- | ===== Applicationクラスのヘルパー関数 ===== | ||
- | |||
- | Applicationクラスのゲッターメソッドで良く使用されるものは boilerplates.php にヘルパー関数として追加しておきます。 | ||
- | |||
- | {{fa> | ||
- | <code php boilerplates.php> | ||
- | // ... | ||
- | |||
- | /** | ||
- | * Get application setting value | ||
- | * @param string|null $dot Dot-notation key | ||
- | * @param mixed $default | ||
- | * @return mixed | ||
- | */ | ||
- | function app($dot = null, $default=null) | ||
- | { | ||
- | return Core\Application:: | ||
- | } | ||
- | |||
- | /** | ||
- | * Get project directory | ||
- | * @param string|null $default | ||
- | * @return string project directory | ||
- | */ | ||
- | function project_dir($path = null): | ||
- | { | ||
- | return add_path(Core\Application:: | ||
- | } | ||
- | |||
- | /** | ||
- | * Get config directory | ||
- | * @param string $path Sub path, if necessary | ||
- | * @return string config directory | ||
- | */ | ||
- | function config_dir($path = null): | ||
- | { | ||
- | return add_path(Core\Application:: | ||
- | } | ||
- | |||
- | /** | ||
- | * Get assets directory | ||
- | * @param string $path Sub path, if necessary | ||
- | * @return string assets directory | ||
- | */ | ||
- | function assets_dir($path = null): | ||
- | { | ||
- | return add_path(Core\Application:: | ||
- | } | ||
- | |||
- | /** | ||
- | * Get var directory | ||
- | * @param string $path Sub path, if necessary | ||
- | * @return string var directory | ||
- | */ | ||
- | function var_dir($path = null): | ||
- | { | ||
- | return add_path(Core\Application:: | ||
- | } | ||
- | |||
- | /** | ||
- | * Get public directory | ||
- | * @param string $path Sub path, if necessary | ||
- | * @return string public directory | ||
- | */ | ||
- | function public_dir($path = null): | ||
- | { | ||
- | return add_path(Core\Application:: | ||
- | } | ||
- | |||
- | /** | ||
- | * Get application URL | ||
- | * @param string $path Sub path, if necessary | ||
- | * @return string URL | ||
- | */ | ||
- | function url($path = null): | ||
- | { | ||
- | // TODO: DomainとProtocolから計算する | ||
- | $base = env(' | ||
- | return add_path($base, | ||
- | } | ||
- | |||
- | /** | ||
- | * Get file URL With version | ||
- | * @param string $filename | ||
- | * @return string URL | ||
- | */ | ||
- | function url_ver(string $filename) | ||
- | { | ||
- | return url($filename).'? | ||
- | } | ||
- | |||
- | /** | ||
- | * Get routing path | ||
- | * @param string $path Sub path, if necessary | ||
- | * @return string routing path | ||
- | */ | ||
- | function route($path = null): | ||
- | { | ||
- | return add_path(Core\Application:: | ||
- | } | ||
- | |||
- | /** | ||
- | * Get current controller name | ||
- | * @return string name | ||
- | */ | ||
- | function controllerName(): | ||
- | { | ||
- | return Core\Application:: | ||
- | } | ||
- | |||
- | /** | ||
- | * Get current action name | ||
- | * @return string name | ||
- | */ | ||
- | function actionName(): | ||
- | { | ||
- | return Core\Application:: | ||
- | } | ||
- | </ | ||
- | |||
- | \\ | ||
- | |||
- | ===== Applicationクラスの設定ファイル ===== | ||
- | |||
- | Applicationクラスは2つの設定ファイル( app.php と routes.php)を持っています。これらはconfigフォルダ内に保存されています。 | ||
- | |||
- | ==== app.php ==== | ||
- | app.php はライブラリとミドルウェアに関する構成とセキュリティーの定義が含まれています。 | ||
- | |||
- | {{fa> | ||
- | <code php app.php> | ||
- | <?php | ||
- | return | ||
- | [ | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ]; | ||
- | </ | ||
- | |||
- | * setup --- ライブラリの初期化ファイルの所在(フルパス) | ||
- | * middleware --- ミドルウェアの完全修飾クラス名 | ||
- | * auth --- ユーザ認証(セッション認証)の設定 | ||
- | * csrf --- CSRFトークンの設定 | ||
- | |||
- | |||
- | ==== routes.php ==== | ||
- | |||
- | routes.php にはルーティング(URIとアクションの紐づけ)の設定が含まれています。 | ||
- | |||
- | {{fa> | ||
- | <code php routes.php> | ||
- | <?php | ||
- | // | ||
- | // Route Definition Callback | ||
- | // | ||
- | return function (FastRoute\RouteCollector $r) | ||
- | { | ||
- | $base = Core\Application:: | ||
- | $r-> | ||
- | { | ||
- | // TODO: Stub Version | ||
- | $r-> | ||
- | header(" | ||
- | echo ' | ||
- | }); | ||
- | }); | ||
- | }; | ||
- | </ | ||
- | |||
- | ルーティング設定に関しては[[https:// | ||
- | |||
- | \\ | ||
- | |||
- | ===== テスト実行 ===== | ||
- | |||
- | ここまでの実装で一度実行してみましょう。以下のように index.php を編集します。 | ||
- | |||
- | {{fa> | ||
- | <code php index.php> | ||
- | TODO: | ||
- | </ | ||
- | |||
- | ブラウザ上で以下のURLにアクセスしてみて下さい。 | ||
- | < | ||
- | http:// | ||
- | </ | ||
- | |||
- | 次のように表示されます: | ||
- | |||
- | < | ||
- | TODO: | ||
- | </ | ||
- | |||
- | \\ | ||