#!/bin/bash set -eux ## Usage: # cd /tmp # composer create-project bear/skeleton MyVendor.Weekday # What is the vendor name ? MyVendor # What is the project name ? Weekday # wget https://gist.github.com/yuki777/2f16f59f637a348bcd87da7ddfc47284/raw/8f37b33ab0708c4325ab83df44b9d5dc6baac93e/bear-sunday-tutorial1-php8-v2.bash # /bin/bash ./bear-sunday-tutorial1-php8-v2.bash # 移動する cd /tmp/MyVendor.Weekday # composer.jsonを変更して https://github.com/koriym/BEAR.Package/tree/scan-binding-update を使う composer config repositories.koriym/BEAR.package vcs https://github.com/koriym/BEAR.package composer require "bear/package":"dev-scan-binding-update as 1.10.9" --update-with-all-dependencies # リソース mkdir -p src/Resource/App/ $(cat << 'EOF' > src/Resource/App/Weekday.php format('D'); $this->body = ['weekday' => $weekday]; return $this; } } EOF ) # テストコード mkdir -p tests/Resource/App $(cat << 'EOF' > tests/Resource/App/WeekdayTest.php resource = $injector->getInstance(ResourceInterface::class); } public function testOnGet(): void { $ro = $this->resource->get('app://self/weekday', ['year' => '2001', 'month' => '1', 'day' => '1']); $this->assertSame(200, $ro->code); $this->assertSame('Mon', $ro->body['weekday']); } } EOF ) # 例外作成 mkdir -p src/Exception $(cat << 'EOF' > src/Exception/InvalidDateTime.php src/Resource/App/Weekday.php format('D'); $this->body = ['weekday' => $weekday]; return $this; } } EOF ) # テストコード mkdir -p tests/Resource/App $(cat << 'EOF' > tests/Resource/App/WeekdayTest.php resource = $injector->getInstance(ResourceInterface::class); } public function testOnGet(): void { $ro = $this->resource->get('app://self/weekday', ['year' => '2001', 'month' => '1', 'day' => '1']); $this->assertSame(200, $ro->code); $this->assertSame('Mon', $ro->body['weekday']); } public function tesInvalidDateTime(): void { $this->expectException(InvalidDateTime::class); $this->resource->get('app://self/weekday', ['year' => '-1', 'month' => '1', 'day' => '1']); } } EOF ) # Install aura router composer require bear/aura-router-module:'^2.0' # App module $(cat << 'EOF' > src/Module/AppModule.php load(dirname(__DIR__, 2)); $appDir = $this->appMeta->appDir; $this->install(new AuraRouterModule($appDir . '/var/conf/aura.route.php')); $this->install(new PackageModule()); } } EOF ) # router file mkdir -p var/conf $(cat << 'EOF' > var/conf/aura.route.php route('/weekday', '/weekday/{year}/{month}/{day}'); EOF ) # ここまではOK php bin/app.php get /weekday/1981/09/08 # MyLoggerInterface $(cat << 'EOF' > src/MyLoggerInterface.php src/Resource/App/Weekday.php format('D'); $this->body = [ 'weekday' => $weekday ]; $this->logger->log("$year-$month-$day {$weekday}"); return $this; } } EOF ) # MyLogger実装 $(cat << 'EOF' > src/MyLogger.php logFile = $meta->logDir . '/weekday.log'; } public function log(string $message): void { error_log($message . PHP_EOL, 3, $this->logFile); } } EOF ) # App module $(cat << 'EOF' > src/Module/AppModule.php load(dirname(__DIR__, 2)); $appDir = $this->appMeta->appDir; $this->install(new AuraRouterModule($appDir . '/var/conf/aura.route.php')); $this->bind(MyLoggerInterface::class)->to(MyLogger::class); $this->install(new PackageModule()); } } EOF ) # test => error php bin/app.php get /weekday/2011/05/23 # test => OK # rm -fr var/tmp # php bin/app.php get /weekday/2011/05/23