Application and Service Providers
An Atom application extends Atom\Application.
Each application hook configures a part of startup.
rootPath()andconfigurePaths()control path aliasesenvironmentFiles()customizes.envfiles loaded before config and servicesservices()registers providers and bindingsjobs()registers application background job typesschedule()registers application scheduled tasksmodules()registers framework and application modulescomponents()registers application component tagspages()registers page directoriesbootstrap()performs runtime setup such as model database configuration and API routes
Application Example
namespace App;
use App\Components\Table;
use App\Controllers\ApiController;
use Atom\Database\DatabaseServices;
use Atom\Di\Injector;
use Atom\Di\ServiceProviderRegistry;
use Atom\Module\ModuleRegistry;
use Atom\Modules\Client\Client;
use Atom\Page\PageRegistry;
use Atom\Router\Route;
use Atom\View\Component\ComponentRegistry;
final class Application extends \Atom\Application
{
protected function rootPath(): string
{
return dirname(__DIR__);
}
protected function services(ServiceProviderRegistry $providers): void
{
$providers->add(DatabaseServices::fromConfig($this->getConfig(), $this->getPaths()));
}
protected function modules(ModuleRegistry $modules): void
{
$modules->add(Client::module());
}
protected function pages(PageRegistry $pages): void
{
$pages->directory("@app/Pages");
}
protected function components(ComponentRegistry $components): void
{
$components->register("Table", Table::class);
}
protected function bootstrap(Injector $injector): void
{
Route::attach(ApiController::class);
}
}
Framework Defaults
The base application registers framework services for:
- console
- dispatcher
- pages and view rendering
- legacy PHP views
- router
- request and response wrappers
- lazy, request-scoped sessions and flash data
- session-backed CSRF tokens and route middleware
Application providers are added on top of those defaults.
Handling Requests
Smoke-test the homepage from the command line
Use Application::handle() in a test script to dispatch a request and inspect the returned Response. The script checks the homepage status and heading, then requests an unknown URL and verifies its status.
Save check-routes.php beside composer.json in an installed Atom Starter app. If you have customized the homepage, replace the expected heading with text from your page.
<?php
declare(strict_types=1);
use App\Application;
use Atom\Http\Request;
require __DIR__ . "/vendor/autoload.php";
$app = new Application();
$home = $app->handle(new Request("GET", "/"));
if ($home->getStatus() !== 200) {
fwrite(STDERR, "Expected GET / to return 200, got " . $home->getStatus() . PHP_EOL);
exit(1);
}
if (!str_contains($home->getContent(), "A small foundation for your next website.")) {
fwrite(STDERR, "Homepage heading missing." . PHP_EOL);
exit(1);
}
$missing = $app->handle(new Request("GET", "/missing-route-check"));
if ($missing->getStatus() !== 404) {
fwrite(STDERR, "Expected missing route to return 404, got " . $missing->getStatus() . PHP_EOL);
exit(1);
}
echo "Homepage rendered and missing route returned 404.", PHP_EOL;
Run php check-routes.php from the project root. On success it prints Homepage rendered and missing route returned 404. A failed check prints the reason to standard error and exits with code 1, so the same command can run in CI.
Use run() in a front controller. It calls handle(), emits the response, and returns it:
(new App\Application())->run();
Bootstrappers
Service providers can contribute runtime bootstrappers by implementing BootstrapProviderInterface.
Bootstrappers run after modules, components, and pages are registered, and before the application bootstrap() hook. DatabaseServices uses this to configure the Model base class automatically.
Profiling
Profiler is bound in the application container and supports begin/end spans plus callback measurement:
use Atom\Profiler\Profile;
$span = Profile::begin("view.render");
try {
// work
} finally {
$span->end();
}
$result = Profile::measure("request.dispatch", fn() => $handler->handle($request));
Application responses include grouped timing headers such as X-Atom-Time, X-Atom-Time-App-Initialize, X-Atom-Time-Request-Dispatch, X-Atom-Time-Page-Render, X-Atom-Time-View-Parse, and X-Atom-Time-View-Render. Repeated spans include a count, for example 0.42ms; count=3.
Measure repeated work in one request
Suppose a stock report reads two suppliers' JSON feeds. To see how many times the route decodes a feed and how long decoding takes in total, wrap each decode in Profile::measure() with the same name.
In a Starter app, import Atom\Router\Route and Atom\Profiler\Profile in app/Application.php. Add this route inside bootstrap(). If the method is missing, use the signature in Application Example. The two sample feeds keep the report runnable offline.
Route::get("/reports/stock", static function (): array {
$feeds = ['{"units":12}', '{"units":8}'];
$units = 0;
foreach ($feeds as $feed) {
$stock = Profile::measure("stock.decode", static fn(): array =>
json_decode($feed, true, flags: JSON_THROW_ON_ERROR)
);
$units += $stock["units"];
}
return ["units" => $units];
});
Start php atom serve from the project root. In another terminal, request the report with its response headers.
curl -i http://127.0.0.1:8020/reports/stock
The body reads {"units":20}. Find X-Atom-Time-Stock-Decode in the headers. Its millisecond value adds up the time spent in both callbacks, and count=2 confirms both feeds were decoded. Try removing the second feed and repeat the request. The body now reads {"units":12} and the timing header shows only the duration, because Atom includes a count for repeated spans.
Service Providers
Service providers group DI registrations:
use Atom\Di\Bindings;
use Atom\Di\ServiceProviderInterface;
final class BlogServices implements ServiceProviderInterface
{
public function register(Bindings $bindings): void
{
$bindings->bind(PostService::class)
->toSelf()
->singleton();
}
}
Register a provider class:
$providers->add(BlogServices::class);
Register a configured provider instance:
$providers->add(new DatabaseServices(new SqliteDriver($path)));
Register the product catalogue
The product page receives a ProductService in its constructor. In this version of the example, ProductService is an interface. The page asks for a product by ID, and the application chooses which class supplies it.
Create these files under app/Catalog in Atom Starter. If you already have the example's Product class, use it in place of the first file.
<?php
namespace App\Catalog;
final readonly class Product
{
public function __construct(public string $name, public bool $inStock)
{
}
}
<?php
namespace App\Catalog;
interface ProductService
{
public function getById(string $id): Product;
}
<?php
namespace App\Catalog;
final class SampleProductService implements ProductService
{
public function getById(string $id): Product
{
return match ($id) {
"42" => new Product("Desk lamp", true),
"43" => new Product("Floor lamp", false),
default => throw new \OutOfBoundsException("Unknown sample product."),
};
}
}
The injector can construct a concrete class when it can resolve its dependencies. For an interface such as ProductService, it needs a binding that identifies the implementation. Create the provider below to supply that binding.
<?php
namespace App\Providers;
use App\Catalog\ProductService;
use App\Catalog\SampleProductService;
use Atom\Di\Bindings;
use Atom\Di\ServiceProviderInterface;
final class CatalogServiceProvider implements ServiceProviderInterface
{
public function register(Bindings $bindings): void
{
$bindings->bind(ProductService::class)->to(SampleProductService::class);
}
}
Import App\Providers\CatalogServiceProvider in app/Application.php and add this line inside the existing services() method.
$providers->add(CatalogServiceProvider::class);
Use the service-backed page class and template. When Atom creates ProductPage, it resolves the constructor's ProductService type through the binding and supplies a SampleProductService. Open /products/42 to see Desk lamp and In stock, then /products/43 to see Floor lamp and Sold out. To use a different catalogue, change the provider's binding to another implementation of the interface.
Console Providers
Providers can also expose console command discovery paths by implementing ConsoleCommandProviderInterface:
use Atom\Console\ConsoleCommandProviderInterface;
use Atom\Console\ConsoleCommandSources;
use Atom\Di\Bindings;
use Atom\Di\ServiceProviderInterface;
final class BlogServices implements ServiceProviderInterface, ConsoleCommandProviderInterface
{
public function register(Bindings $bindings): void
{
}
public function consoleCommands(ConsoleCommandSources $commands): void
{
$commands->add(__DIR__ . "/Commands", "App\\Blog\\Commands");
}
}
The application console discovers these command sources automatically.