Last active
September 17, 2021 06:51
-
-
Save yuki777/2f16f59f637a348bcd87da7ddfc47284 to your computer and use it in GitHub Desktop.
bear-sunday-tutorial1-php8-v2.bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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.githubusercontent.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 | |
<?php | |
declare(strict_types=1); | |
namespace MyVendor\Weekday\Resource\App; | |
use BEAR\Resource\ResourceObject; | |
use DateTimeImmutable; | |
class Weekday extends ResourceObject | |
{ | |
public function onGet(int $year, int $month, int $day): static | |
{ | |
$dateTime =DateTimeImmutable::createFromFormat('Y-m-d', "$year-$month-$day"); | |
$weekday = $dateTime->format('D'); | |
$this->body = ['weekday' => $weekday]; | |
return $this; | |
} | |
} | |
EOF | |
) | |
# テストコード | |
mkdir -p tests/Resource/App | |
$(cat << 'EOF' > tests/Resource/App/WeekdayTest.php | |
<?php | |
declare(strict_types=1); | |
namespace MyVendor\Weekday\Resource\App; | |
use BEAR\Resource\ResourceInterface; | |
use MyVendor\Weekday\Injector; | |
use PHPUnit\Framework\TestCase; | |
class WeekdayTest extends TestCase | |
{ | |
private ResourceInterface $resource; | |
protected function setUp(): void | |
{ | |
$injector = Injector::getInstance('app'); | |
$this->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 | |
<?php | |
declare(strict_types=1); | |
namespace MyVendor\Weekday\Exception; | |
use RuntimeException; | |
class InvalidDateTime extends RuntimeException | |
{ | |
} | |
EOF | |
) | |
# App/Weekday | |
$(cat << 'EOF' > src/Resource/App/Weekday.php | |
<?php | |
declare(strict_types=1); | |
namespace MyVendor\Weekday\Resource\App; | |
use BEAR\Resource\ResourceObject; | |
use DateTimeImmutable; | |
use MyVendor\Weekday\Exception\InvalidDateTime; | |
class Weekday extends ResourceObject | |
{ | |
public function onGet(int $year, int $month, int $day): static | |
{ | |
$dateTime = DateTimeImmutable::createFromFormat('Y-m-d', "$year-$month-$day"); | |
if (! $dateTime instanceof DateTimeImmutable) { | |
throw new InvalidDateTime("$year-$month-$day"); | |
} | |
$weekday = $dateTime->format('D'); | |
$this->body = ['weekday' => $weekday]; | |
return $this; | |
} | |
} | |
EOF | |
) | |
# テストコード | |
mkdir -p tests/Resource/App | |
$(cat << 'EOF' > tests/Resource/App/WeekdayTest.php | |
<?php | |
declare(strict_types=1); | |
namespace MyVendor\Weekday\Resource\App; | |
use BEAR\Resource\ResourceInterface; | |
use MyVendor\Weekday\Injector; | |
use PHPUnit\Framework\TestCase; | |
class WeekdayTest extends TestCase | |
{ | |
private ResourceInterface $resource; | |
protected function setUp(): void | |
{ | |
$injector = Injector::getInstance('app'); | |
$this->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 | |
<?php | |
declare(strict_types=1); | |
namespace MyVendor\Weekday\Module; | |
use BEAR\Dotenv\Dotenv; | |
use BEAR\Package\AbstractAppModule; | |
use BEAR\Package\PackageModule; | |
use BEAR\Package\Provide\Router\AuraRouterModule; | |
use function dirname; | |
class AppModule extends AbstractAppModule | |
{ | |
protected function configure(): void | |
{ | |
(new Dotenv())->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 | |
<?php | |
/** | |
* @see http://bearsunday.github.io/manuals/1.0/ja/router.html | |
* @var \Aura\Router\Map $map | |
*/ | |
$map->route('/weekday', '/weekday/{year}/{month}/{day}'); | |
EOF | |
) | |
# ここまではOK | |
php bin/app.php get /weekday/1981/09/08 | |
# MyLoggerInterface | |
$(cat << 'EOF' > src/MyLoggerInterface.php | |
<?php | |
declare(strict_types=1); | |
namespace MyVendor\Weekday; | |
interface MyLoggerInterface | |
{ | |
public function log(string $message): void; | |
} | |
EOF | |
) | |
# リソース | |
mkdir -p src/Resource/App/ | |
$(cat << 'EOF' > src/Resource/App/Weekday.php | |
<?php | |
namespace MyVendor\Weekday\Resource\App; | |
use BEAR\Resource\ResourceObject; | |
use MyVendor\Weekday\MyLoggerInterface; | |
class Weekday extends ResourceObject | |
{ | |
public function __construct(public MyLoggerInterface $logger) | |
{ | |
} | |
public function onGet(int $year, int $month, int $day) : ResourceObject | |
{ | |
$weekday = \DateTime::createFromFormat('Y-m-d', "$year-$month-$day")->format('D'); | |
$this->body = [ | |
'weekday' => $weekday | |
]; | |
$this->logger->log("$year-$month-$day {$weekday}"); | |
return $this; | |
} | |
} | |
EOF | |
) | |
# MyLogger実装 | |
$(cat << 'EOF' > src/MyLogger.php | |
<?php | |
declare(strict_types=1); | |
namespace MyVendor\Weekday; | |
use BEAR\AppMeta\AbstractAppMeta; | |
use function error_log; | |
use const PHP_EOL; | |
class MyLogger implements MyLoggerInterface | |
{ | |
private string $logFile; | |
public function __construct(AbstractAppMeta $meta) | |
{ | |
$this->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 | |
<?php | |
declare(strict_types=1); | |
namespace MyVendor\Weekday\Module; | |
use BEAR\Dotenv\Dotenv; | |
use BEAR\Package\AbstractAppModule; | |
use BEAR\Package\PackageModule; | |
use BEAR\Package\Provide\Router\AuraRouterModule; | |
use MyVendor\Weekday\MyLogger; | |
use MyVendor\Weekday\MyLoggerInterface; | |
use function dirname; | |
class AppModule extends AbstractAppModule | |
{ | |
protected function configure(): void | |
{ | |
(new Dotenv())->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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment