PHP code example of tobento / app
1. Go to this page and download the library: Download tobento/app 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' );
tobento / app example snippets
use Tobento \App \AppFactory ;
$app = (new AppFactory())->createApp();
$app->boot(\Tobento\App\Boot\App::class);
$app->boot(\Tobento\App\Boot\ErrorHandling::class);
$app->run();
use Tobento \App \AppFactory ;
use Tobento \App \AppFactoryInterface ;
use Tobento \App \AppInterface ;
use Tobento \Service \Resolver \ResolverFactoryInterface ;
use Tobento \Service \Booting \BooterInterface ;
use Tobento \Service \Dir \DirsInterface ;
$appFactory = new AppFactory();
var_dump($appFactory instanceof AppFactoryInterface);
$app = $appFactory->createApp(
resolverFactory: null ,
booter: null ,
dirs: null ,
);
var_dump($app instanceof AppInterface);
use Tobento \App \AppFactory ;
class Foo {}
$app = (new AppFactory())->createApp();
var_dump($app->has(Bar::class));
var_dump($app->get(Foo::class));
use Tobento \App \AppFactory ;
use Tobento \Service \Resolver \DefinitionInterface ;
class Foo
{
public function __construct (
protected string $name
) {}
}
$app = (new AppFactory())->createApp();
$definition = $app->set(Foo::class)->construct('name' );
var_dump($definition instanceof DefinitionInterface);
use Tobento \App \AppFactory ;
class Foo
{
public function __construct (
private Bar $bar,
private string $name
) {}
}
class Bar {}
$app = (new AppFactory())->createApp();
$foo = $app->make(Foo::class, ['name' => 'value' ]);
use Tobento \App \AppFactory ;
class Foo
{
public function index (Bar $bar, string $name) : string
{
return $name;
}
}
class Bar {}
$app = (new AppFactory())->createApp();
$name = $app->call([Foo::class, 'index' ], ['name' => 'value' ]);
var_dump($name);
use Tobento \App \AppFactory ;
class AdminUser {}
class GuestUser {}
$app = (new AppFactory())->createApp();
$app->on(AdminUser::class, GuestUser::class);
$user = $app->get(AdminUser::class);
var_dump($user);
use Tobento \App \AppFactory ;
use Tobento \App \Boot ;
interface ServiceInterface {}
class Service implements ServiceInterface {}
class ServiceBoot extends Boot
{
public function boot ()
{
$this ->app->set(ServiceInterface::class, Service::class);
}
}
$app = (new AppFactory())->createApp();
$app->boot(ServiceBoot::class);
var_dump($app->has(ServiceInterface::class));
$app->booting();
var_dump($app->has(ServiceInterface::class));
$app->run();
use Tobento \App \AppFactory ;
use Tobento \Service \Dir \DirsInterface ;
$app = (new AppFactory())->createApp();
$app->dirs()
->dir(dir: 'path/to/config' , name: 'config' , group: 'config' )
->dir(dir: 'path/to/view' , name: 'view' );
var_dump($app->dir(name: 'view' ));
var_dump($app->dirs() instanceof DirsInterface);
use Tobento \App \AppFactory ;
use Psr \Clock \ClockInterface ;
$app = (new AppFactory())->createApp();
var_dump($app->clock() instanceof ClockInterface);
var_dump($app->get(ClockInterface::class) instanceof ClockInterface);
use Tobento \App \AppFactory ;
$app = (new AppFactory())->createApp();
$app->addMacro('lowercase' , function (string $string) : string {
return strtolower($string);
});
var_dump($app->lowercase('Lorem' ));
use Tobento \App \AppFactory ;
$app = (new AppFactory())->createApp();
$app->boot(\Tobento\App\Boot\App::class);
$app->run();
use Tobento \App \AppFactory ;
use Tobento \Service \Config \ConfigInterface ;
$app = (new AppFactory())->createApp();
$app->dirs()->dir(
dir: 'path/to/config' ,
name: 'config' ,
group: 'config'
);
$app->boot(\Tobento\App\Boot\Config::class);
$app->booting();
$value = $app->get(ConfigInterface::class)->get(
key: 'app.key' ,
default : 'default' ,
locale: 'de'
);
$value = $app->config('app.key' , 'default' );
var_dump($value);
$app->run();
use Tobento \App \AppFactory ;
use Tobento \App \AppInterface ;
use function Tobento \App \{app , directory , config };
$app = (new AppFactory())->createApp();
$app->boot(\Tobento\App\Boot\Functions::class);
$app->booting();
var_dump(app() instanceof AppInterface);
$app->dirs()->dir('dir/to/foo' , 'foo' );
var_dump(directory('foo' ));
$app->boot(\Tobento\App\Boot\Config::class);
$app->booting();
var_dump(config(key: 'foo' , default : 'foo' ));
$app->run();
use Tobento \App \AppFactory ;
$app = (new AppFactory())->createApp();
$app->boot(\Tobento\App\Boot\ErrorHandling::class);
$app->run();
use Tobento \App \AppFactory ;
use Tobento \Service \Dater \DateFormatter ;
$app = (new AppFactory())->createApp();
$app->boot(\Tobento\App\Boot\Dater::class);
$app->booting();
$df = $app->get(DateFormatter::class);
var_dump($df->date('now' ));
$app->run();
use Tobento \App \AppFactory ;
use Tobento \App \Boot ;
use Tobento \App \BootErrorHandlersInterface ;
use Tobento \App \BootErrorHandlers ;
use Tobento \Service \ErrorHandler \AutowiringThrowableHandlerFactory ;
class CausesErrorBoot extends Boot
{
public function boot () : void
{
echo $test();
}
}
$app = (new AppFactory())->createApp();
$app->set(BootErrorHandlersInterface::class, function () use ($app) {
$handlers = new BootErrorHandlers(
new AutowiringThrowableHandlerFactory($app->container())
);
$handlers->add(function (Throwable $t) : mixed {
return null ;
});
return $handlers;
});
$app->boot(\Tobento\App\Boot\ErrorHandling::class);
$app->boot(CausesErrorBoot::class);
$app->run();
use Tobento \App \Boot ;
use Tobento \App \Http \Boot \Http ;
use Psr \Http \Message \ResponseFactoryInterface ;
class CustomBoot extends Boot
{
public function boot () : void
{
$this ->app->on(
ResponseFactoryInterface::class,
\Laminas\Diactoros\ResponseFactory::class
);
}
}
$app = (new AppFactory())->createApp();
$app->boot(CustomBoot::class);
$app->run();
use Tobento \App \Boot ;
use Tobento \App \Http \Boot \Http ;
use Tobento \App \Migration \Boot \Migration ;
use Psr \Http \Message \ResponseFactoryInterface ;
class CustomHttpBoot extends Http
{
public function boot (Migration $migration) : void
{
parent ::boot($migration);
$this ->app->set(
ResponseFactoryInterface::class,
\Laminas\Diactoros\ResponseFactory::class
);
}
}
$app = (new AppFactory())->createApp();
$app->on(Http::class, CustomHttpBoot::class);
$app->run();
use Tobento \App \Boot ;
use Tobento \App \Http \Boot \Http ;
use Psr \Http \Message \ResponseFactoryInterface ;
class CustomBoot extends Boot
{
public const BOOT = [
Http::class,
];
public function boot (Http $http) : void
{
$this ->app->set(
ResponseFactoryInterface::class,
\Laminas\Diactoros\ResponseFactory::class
);
}
}
$app = (new AppFactory())->createApp();
$app->boot(CustomBoot::class);
$app->run();