Paths value object
- Comparing
- v1 to v2
- Current
- Read the document
Abstract
A single PathsThe engine value object holds every filesystem location the engine needsneeds its files in five places: configuration, application data, modulesinstalled
modules, cache
the cache and logs. I will give it one immutable object, Paths, that knows all five
and builds paths beneath them. Bootstrap constructs will create it once, from the arguments the panel is booted with, and it replaces the
configuration component's will replace ConfigPaths, which knows only where
configuration lives.
Motivation
The configuration loader receives its locations through ConfigPaths, which holds configuration locations and
nothing and nothing
else. More components need As more of the engine needs locations of their its own: modules, the modules, the
cache and logslogs each need a directory as well. A single object holding
holding every location, constructed filled in once during early in bootstrap, ,
is cleaner than a paths object for each componentpart of the engine that needs one.
Proposal
Concepts
| Term | Meaning |
|---|---|
| Root | An absolute directory under which the engine keeps one type of file. |
| Relative path | A path beneath a root, joined onto it by Paths. |
Components
| Component | Responsibility |
|---|---|
Paths |
Final readonly value object holding every root, and joining relative paths onto them. |
Roots
| Property | Holds |
|---|---|
config |
The configuration directory, holding config.toml, config.d/ and modules-enabled/. |
data |
The application data directory. |
modules |
The Composer project that external modules are installed into, per ADR-0012. |
cache |
The cache directory. |
logs |
The log directory. |
Each root is an absolute path. Paths holds the roots and does nothing else with them: it does not work out where
they are, check that they exist, or create them.
Paths will hold these five and nothing more. It will be a final, readonly value object that is handed absolute
paths and takes them as given: it will not work out where the directories are, check that they exist or create them.
Producing absolute paths is bootstrap's responsibility.
$paths = new Paths(
config: '/etc/tgp',
data: '/var/lib/tgp',
modules: '/var/lib/tgp/modules',
cache: '/var/lib/tgp/cache',
logs: '/var/log/tgp',
);
Joining pathsBuilding a path
Each root has root will have a method of the same name that joins named after it, such as config(string $path): string, which takes
a relative path path and returns it joined onto itthat root. The root's trailing separator and
the path's leading separator will be trimmed and a single separator put between them, so the path is always read as
relative, with or without a leading slash:
| Method | Returns |
|---|---|
config(string $path): string |
The path beneath the configuration directory. |
data(string $path): string |
The path beneath the data directory. |
modules(string $path): string |
The path beneath the modules directory. |
cache(string $path): string |
The path beneath the cache directory. |
logs(string $path): string |
The path beneath the log directory. |
The root's trailing separator and the path's leading separator are removed, and the two are joined with a single
separator. A path passed to a method is always treated as relative to the root, so $paths->config('config.d') and
$paths->config('/config.d') both return /etc/tgp/config.d.
Construction
Bootstrap constructs Paths once, during early bootstrap, before configuration is loaded or modules are discovered.
Its roots come from arguments given when the panel is booted, which are passed in during bootstrapping.
Configuration loading
TomlLoader::load() takes Paths in place of ConfigPaths, and reads from the configuration directory:
config.toml, config.d/ and modules-enabled/ beneath $paths->config. ConfigPaths is removed.
$paths->config('config.d'); // /etc/tgp/config.d
$paths->config('/config.d'); // /etc/tgp/config.d
Creating it
Bootstrap will create Paths once, early, before configuration is loaded or modules are
discovered. The roots will come from the arguments the panel is booted with.
Loading configuration
The configuration loader will take Paths in place of ConfigPaths, and find config.toml, config.d/ and
modules-enabled/ beneath the configuration root. Their names will be fixed relative to that
root rather than each given separately. ConfigPaths will be removed.
$tree = new TomlLoader()->load($paths);
Out of scope
- Populating the roots. How bootstrap takes the
rootsroots from the arguments the panel isbootedbooted with, andresolvesresolves them, belongs to bootstrapping.
Alternatives considered
A paths object for each componentThis design rests on ADR-0012, of which ConfigPaths is the existing one. Once modules, the cache and logs
need locations as well as configuration, installs
external modules into a single object holding every Composer project of their own and so gives modules a
root, constructed once during early
bootstrap, is cleaner.
No other alternatives were weighedA paths object for each component. ConfigPaths would stay as it is, and modules, the cache and logs would each
gain an object of their own describing their locations. I rejected this because once more than configuration needs a
location, one object holding every root, created once early in bootstrap, is cleaner.
Backwards compatibility
ConfigPathsis removed, andTomlLoader::load()takesPathsinstead.- The main configuration file is always
config.tomlin the configuration directory, and the drop-in and module configuration directories are alwaysconfig.d/andmodules-enabled/beside it.ConfigPathsheld each of the three as a path of its own.
Open questions
Changelog
Sources
- Issue #34, Engine - Paths Value Object, : the motivation, the single object and the per-component
objects it replaces, the five
rootsroots and their default locations,constructioncreation during early bootstrapbeforebefore configuration is loaded ormodulesmodules are discovered, and configuration loading reading from$paths->config. Thisisiscreated. Its tasks, tickedonon , include resolving the roots in layers from compiled defaults,, environment variables and command-lineline flags, and testing that resolution. Nothing at a50a9ab resolvesthethe roots, and that resolution is lefttoto bootstrapping. - PR #40, refactor(engine:config): Refactor ConfigPaths VO to Paths, merged and squashed as a50a9ab
: the: the implementation, whose merge isdecided.Paths, its methods andTomlLoader's use of it are described fromsrc/Config/Paths.phpat a50a9ab, whose docblock states that bootstrap is responsible for producing absolute paths,src/Config/TomlLoader.phpat a50a9ab andtests/Unit/Config/ConfigPathsTest.phpat a50a9ab. - The roots coming from arguments given when the panel is booted, and populating and resolving them belonging to bootstrapping: first written down on .
Source rfc/0005-paths-value-object.md · commit 9444a99 · published