The Artisan CLI Internals
How the command-line interface boots and operates outside the web server
While most of your interaction with DALT happens through a web browser, the Artisan CLI (artisan file in your root) provides a powerful command-line interface for development tasks.
Artisan is unique because it is a second entry point into your application. It doesn't use public/index.php or the Router.
1. The Shebang Line
The very first line of the artisan file is:
#!/usr/bin/env phpThis tells your terminal to use the PHP interpreter to run this file. This is why you can run ./artisan directly (if permissions are set) instead of typing php artisan.
2. Bootstrapping the CLI
Unlike the web entry point which starts sessions and parses URLs, Artisan's bootstrap process is focused on file paths and database configuration:
function load_env_and_boot(): array {
if (!defined('BASE_PATH')) {
define('BASE_PATH', __DIR__ . '/');
}
require base_path('vendor/autoload.php');
// Load .env variables
$dotenv = Dotenv\Dotenv::createImmutable(base_path(''));
$dotenv->safeLoad();
// Return DB config for commands that need it
return require base_path('config/database.php');
}Wait, why doesn't it use bootstrap.php?
Artisan is designed to be lightweight. It only loads what it needs for the specific command you ran.
3. Command Resolution (The Switch Loop)
DALT's Artisan uses a simple switch statement to route commands.
$command = $argv[1] ?? 'help';
switch ($command) {
case 'serve':
// Start PHP server logic
break;
case 'migrate':
// Database migration logic
break;
case 'make:migration':
// File generation logic
break;
}This is intentionally kept in one file so you can read the entire "brain" of the CLI in under 10 minutes.
4. Key Command Implementations
The serve Command
This command is a wrapper for PHP's built-in web server. It includes a clever port-discovery loop:
while (!$isFree($host, $chosenPort)) {
$chosenPort++; // Automatically find next available port
}
passthru("php -S {$host}:{$chosenPort} -t public public/router.php");The make:migration Command
Instead of using complex stubs, this command uses a Heredoc to generate SQL files on the fly. It even detects whether you are using SQLite or PostgreSQL and adjusts the id column syntax (AUTOINCREMENT vs SERIAL) automatically.
Key Takeaways
- Independent Entry Point: Artisan is a standalone script that shares only the Core classes and configuration with the web app.
- Port Discovery: The
servecommand is smart enough to find free ports, preventing "Address already in use" errors during development. - Dual Syntax: Artisan's
make:migrationallows DALT to be cross-compatible between SQLite and Postgres without the user needing to know the specific dialect differences for basic tables.
What's Good Here
✅ Zero-dependency CLI (only uses native PHP and the autoloader)
✅ Extremely fast boot time
✅ Self-contained logic makes it easy to add custom developer commands
✅ Simple enough to understand completely in one sitting
Design Note
The artisan file is intentionally kept in one file for educational purposes. You can see the entire CLI system at once. For a production framework, you'd split this into Command classes, but for learning, simplicity wins.
Navigation
You've now seen both the Web and CLI entry points. The "Deep Dive" into the DALT.PHP framework internals is complete!