Installation Add the package via Composer:
composer require draw/application
Publish the config file (if needed):
php artisan vendor:publish --provider="Draw\Application\ApplicationServiceProvider"
First Use Case: Versioning
Register the version handler in AppServiceProvider:
use Draw\Application\Versioning\VersionHandler;
public function register()
{
$this->app->singleton(VersionHandler::class, function ($app) {
return new VersionHandler($app['config']['application.version']);
});
}
Access the version anywhere in your app:
$version = app(VersionHandler::class)->getVersion();
First Use Case: Configuration
Define a custom config key in config/application.php:
'custom' => [
'setting' => 'default_value',
],
Access it via the facade:
use Draw\Application\Facades\Application;
$value = Application::config('custom.setting');
CronManager to register and manage cron jobs programmatically:
use Draw\Application\Cron\CronManager;
$manager = app(CronManager::class);
$manager->addJob('send-reports', 'php artisan reports:send', '0 0 * * *');
app/Console/Kernel.php.Application::setConfig('custom.setting', 'new_value');
ConfigResolver to merge environment-specific configs:
$resolver = new \Draw\Application\Config\ConfigResolver();
$resolver->resolve(['env' => 'production']);
public function handle($request, Closure $next)
{
if (app(VersionHandler::class)->isOutdated()) {
return response()->view('updates.available');
}
return $next($request);
}
config/application.php and fetch them dynamically:
$notes = Application::config('releases.' . $version . '.notes');
$this->app->bind('App\Services\CustomService', function ($app) {
return new \App\Services\CustomService(
$app['config']['application.custom']
);
});
VersionUpdated) from the VersionHandler.Config Overrides
Application::setConfig() sparingly; prefer environment-specific configs or cached configs.if (config('app.debug')) {
\Log::debug('Config updated:', ['key' => $key, 'value' => $value]);
}
Cron Job Conflicts
CronManager is instantiated multiple times.CronManager is a singleton in the service container:
$this->app->singleton(CronManager::class, function ($app) {
return new CronManager($app['config']['application.cron']);
});
Version Handling
app(VersionHandler::class)->getVersion() instead of raw strings.AppServiceProvider:
if (!function_exists('version')) {
function version() {
return app(\Draw\Application\Versioning\VersionHandler::class)->getVersion();
}
}
Config Dumping
Use the Application facade to dump the entire config:
dd(Application::getConfig());
Cron Job Logging
Enable logging for cron jobs by extending CronManager:
class CustomCronManager extends CronManager {
public function addJob($name, $command, $schedule, $log = true) {
if ($log) {
\Log::info("Cron job added: {$name} - {$command}");
}
parent::addJob($name, $command, $schedule);
}
}
Custom Config Resolvers
Extend \Draw\Application\Config\ConfigResolver to support additional resolution strategies (e.g., database-backed configs):
class DatabaseConfigResolver extends ConfigResolver {
public function resolve(array $overrides = []) {
$dbConfigs = $this->fetchFromDatabase();
return parent::resolve(array_merge($overrides, $dbConfigs));
}
}
Version Strategies
Override VersionHandler to fetch versions from an API or Git tags:
class ApiVersionHandler extends VersionHandler {
public function getVersion() {
return $this->fetchFromApi();
}
}
Cron Job Storage
Store cron jobs in a database instead of the config file by implementing \Draw\Application\Cron\CronStorageInterface:
class DatabaseCronStorage implements CronStorageInterface {
public function getJobs() {
return DB::table('cron_jobs')->get();
}
public function saveJobs(array $jobs) {
DB::table('cron_jobs')->delete();
collect($jobs)->each(function ($job) {
DB::table('cron_jobs')->insert($job);
});
}
}
How can I help you explore Laravel packages today?