When accessing Hiraeth over HTTP (any standard web server), you will be hitting the application through the public/index.php
file. This file is provided by hiraeth/bootstrap
, though more specifically through hiraeth/http
. If you’ve not installed these packages or are integrating your own libraries, you can create your own custom entry point.
We’ll deconstruct the entry point for hiraeth/http
to get a better idea of the steps involved.
Finding the Application Root
In order to boot Hiraeth\Application
we’ll need to know what our application root is. The way this is achieved in default Hiraeth entry points is by simply tracking back through parent directories until we find our composer.json
.
for (
$root_path = __DIR__;
$root_path != '/' && !is_file($root_path . DIRECTORY_SEPARATOR . 'composer.json');
$root_path = realpath($root_path . DIRECTORY_SEPARATOR . '..')
);
The above allows the entry point to be moved into any sub-directory of the application root and still function as expected without having to change a static or relative root path.
Get Autoloader and Application
Once we know our application root, we can load composer’s autoloader and get our application instance:
$loader = require $root_path . '/vendor/autoload.php';
$hiraeth = new Hiraeth\Application($root_path);
In addition to the $root_path
you can also specify an environment file path and a release file path as the second and third argument respectively.
Execute Hiraeth
Finally, once we have our application instance, we can execute Hiraeth:
$hiraeth->exec(function() {
//
// Do application stuff
//
});
The $hiraeth->exec()
method will return whatever value is returned from the callback passed, so for console based entry points you may want to wrap it in an exit()
in order to exit with the return code. Within the context of the callback, $this
refers to the application instance itself. Additionally dependencies can be dependency injected by typehinting them as arguments to the callback.