This page explains the basic steps for creating a new package. For the complete result of this example, see the GitLab repository. Note that this repository contains content for other examples as well, as the documentation uses a single package for all examples.
On This Page
Metadata
Select or create an empty directory of your choice. In this directory, create the
production file
fusion.json
with the following content:
{
"name": "Package",
"description": "Basic example package.",
"id": "valvoid/package",
"version": "0.1.0",
"structure": {
"/cache": "cache"
},
"environment": {
"php": {
"version": "8.1.0"
}
}
}
Note that, in particular, the inline
logical expression 8.1.0
and the cache
indicator differ
from the schema. This is due to the parsing process. Metadata files are optimized for
developer input, while the schema represents the output for the logic. The
normalized output data is essential because, as explained in the "Extensions"
chapter, the Fusion logic is customizable.
Therefore, the optimized developer input properties are:
- The
cache
indicator is always a value of a nested directory key. - The directory in general:
- Is always an associative key.
- Starts with a leading forward slash
/
. - Segments are separated by forward slash.
- Relative to the package root.
- The inline expression schema examples
inside paragraphs, such as:
==8.1.0 || (>=8.2.0 && !=8.2.5)
>=8.1.0 && <9.0.0
By the way, if your package also has cacheable content, such as logs, you can
set the Fusion cache
to a nested /cache/fusion
directory and share or recycle
the structure. Just customize the directory as needed.
Code
Actually, the metadata file already represents a processable package, but like in
most use cases, let's add some code. In the root directory, create the /src
directory and within it, create the file Package.php
with the following
lazy code content:
<?php
namespace Valvoid\Package;
class Package
{
public function sayHi(): void
{
echo "Hi, lazy package here!";
}
}
For the nested ASAP code, create
the /Util
directory within the /src
directory and within it, create the file
util.php
with the following content:
<?php
namespace Valvoid\Package\Util
{
function sayHi(): void
{
echo "Hi, ASAP package util here!";
}
}
Individual Usage
In your terminal, set the working directory to any directory within the package
and execute the following
inflate
command:
php fusion inflate
After running the command, the /cache
directory will include the /loadable
directory, containing the following individual cached files:
lazy.php
- Lazy code registry:
<?php
// Auto-generated by Fusion package manager.
// Do not modify.
return [
'Valvoid\Package\Package' => '/src/Package.php',
];
asap.php
- As soon as possible (ASAP) code registry:
<?php
// Auto-generated by Fusion package manager.
// Do not modify.
return [
'/src/Util/util.php',
];
These files contain individual package code registries and are useful, for
example, when setting up internal scripts. To see how this works, create a
script.php
file in the root directory with the following content:
<?php
use Valvoid\Package\Package;
$asap = require __DIR__ . "/cache/loadable/asap.php";
$lazy = require __DIR__ . "/cache/loadable/lazy.php";
foreach ($asap as $file)
require __DIR__ . $file;
spl_autoload_register(function (string $loadable) use ($lazy)
{
require __DIR__ . $lazy[$loadable];
});
$package = new Package;
$package->sayHi();
echo "\n";
Valvoid\Package\Util\sayHi();
Running this script should output:
Hi, lazy package here!
Hi, ASAP package util here!
Common Usage
In your terminal, set the working directory to any directory within the package
and execute the following
register
command:
php fusion register
After running the command, the /cache
directory will include the Autoloader.php
file, which contains the inflated code of all packages. This file is useful for
integrating the code of multiple packages into a single project, such as a website.
To see this in action, create a /public
directory in the root directory, and
within it, create an index.php
file with the following content:
<?php
use Valvoid\Package\Package;
require __DIR__ . "/../cache/Autoloader.php";
$autoloader = Autoloader::init();
$package = new Package;
$package->sayHi();
echo "\n";
Valvoid\Package\Util\sayHi();
Running this script should produce the following output:
Hi, lazy package here!
Hi, ASAP package util here!