PHP code example of ryunosuke / castella
1. Go to this page and download the library: Download ryunosuke/castella 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/ */
ryunosuke / castella example snippets
# $container->uke\castella\Container $this
*/
return [
'env' => [
'name' => 'local',
'origin' => 'http://localhost',
'loglevel' => LOG_INFO,
'logdir' => '/var/log/app',
'rundir' => '/var/run/app',
'datadir' => '/var/opt/app',
'extension' => ['js', 'es', 'ts'],
],
'database' => [
'driver' => 'pdo_mysql',
'host' => '127.0.0.1',
'port' => 3306,
'dbname' => 'app',
'user' => 'user',
'password' => 'password',
'charset' => 'utf8mb4',
'driverOptions' => [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
],
],
's3' => [
'config' => [
'region' => 'ap-northeast-1',
'version' => 'latest',
],
'client' => $this->static(S3Client::class),
],
'storage' => [
'private' => #[Factory] fn($key) => new Storage($this['s3.client'], $key),
'protect' => #[Factory] fn($key) => new Storage($this['s3.client'], $key),
'public' => #[Factory] fn($key) => new Storage($this['s3.client'], $key),
],
];
# $container->uke\castella\Container $this
*/
return [
'env' => [
'origin' => 'http://myself',
'loglevel' => LOG_DEBUG,
'extension' => $this->array(['php']),
],
'database' => [
'host' => 'docker-mysql',
'driverOptions' => [
\PDO::ATTR_EMULATE_PREPARES => false,
],
],
's3' => [
'config' => [
'credentials' => [
'key' => 'minio',
'secret' => 'minio123',
],
'endpoint' => 'http://minio.localhost',
],
],
];
$container = new \ryunosuke\castella\Container([
'debugInfo' => null,
'delimiter' => '.',
'closureAsFactory' => true,
'autowiring' => true,
'constructorInjection' => true,
'propertyInjection' => true,
'resolver' => $container->resolve(...),
]);
$container->extends([
'a' => [
'b' => [
'c' => 'X',
],
],
]);
$container->extends([
'closure' => fn() => "これはただのクロージャです",
'factory' => #[Factory] fn(...$keys) => "これはファクトリです",
]);
$container->extends([
'a' => [
'b' => [
'c abc' => 'X',
],
],
]);
return [
'hoge' => 1,
'fuga' => $this['hoge'],
'piyo' => static fn($c) => $c['hoge'],
];
$container->cache('/path/to/cached.php', function () use ($container) {
// 下記は2回目以降は呼ばれない
$container->mount('/path/to/config'); // mount したり
$container->ner['debugMode'];
});
$container->extends([
'array' => [
'hoge' => 'HOGE',
'fuga' => 'FUGA',
'nest' => [
'hoge' => 'HOGE',
'fuga' => 'FUGA',
],
],
]);
$container->extends([
'array' => [
'hoge' => $container->unset(),
'fuga' => 'FUGA',
'nest' => [
'hoge' => $container->unset(),
'fuga' => 'FUGA',
],
],
]);
return [
'hoge' => $this->const('HOGE', 'CONST_NAME'),
'fuga' => 'FUGA',
'nest' => [
'hoge' => $this->const('HOGE'),
'fuga' => 'FUGA',
],
];
return [
'tmpbyenv' => $this->env('TMP', 'TEMP', 'TMPDIR') ?? '/path/to/default',
];
return [
'hoge_arg1' => 1,
'hoge_arg2' => 2,
'hoge1' => new Hoge(1, 2),
'hoge2' => $this->new(Hoge::class),
];
return [
'hoge_arg1' => 1,
'hoge_arg2' => 2,
'hoge1' => fn($c): Hoge => new Hoge($c['hoge_arg1'], $c['hoge_arg2']),
'hoge2' => $this->yield(Hoge::class),
'hoge3' => $this->yield(Hoge::class, [1 => $this['hoge_arg2']]),
];
return [
'hoge_arg1' => 1,
'hoge_arg2' => 2,
'hoge1' => static fn($c): Hoge => new Hoge($c['hoge_arg1'], $c['hoge_arg2']),
'hoge2' => $this->static(Hoge::class),
];
# parent.php
return [
'array' => ['a', 'b'],
'object' => new Something(),
];
# child.php
return [
'array' => $this->parent(fn($parent) => array_merge($parent, ['c'])),
'object' => $this->parent(fn($parent) => $parent->setSomething()),
];
return [
'callable1' => static fn(): Closure => fn() => 'something',
'callable2' => $this->callable(fn() => 'something'),
];
return [
'array1' => fn(): array => ['php', 'js'],
'array2' => $this->array(['php', 'js']),
];
./mount
├── .php
├── com
│ ├── .php
│ └── example
│ ├── .php
│ └── host.php
├── net.example.host.php
├── net.example.php
├── net.php
└── org.example
├── .php
└── host.php