Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Application Laravel Package

draw/application

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require draw/application
    

    Publish the config file (if needed):

    php artisan vendor:publish --provider="Draw\Application\ApplicationServiceProvider"
    
  2. 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();
    
  3. 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');
    

Implementation Patterns

1. Domain Logic Centralization

  • Cron Jobs: Use the 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 * * *');
    
  • Workflow: Centralize cron logic in a single service, reducing clutter in app/Console/Kernel.php.

2. Configuration Management

  • Dynamic Config: Override config values at runtime:
    Application::setConfig('custom.setting', 'new_value');
    
  • Environment-Based Config: Use the ConfigResolver to merge environment-specific configs:
    $resolver = new \Draw\Application\Config\ConfigResolver();
    $resolver->resolve(['env' => 'production']);
    

3. Versioning and Releases

  • Version Checks: Integrate version checks in middleware or controllers:
    public function handle($request, Closure $next)
    {
        if (app(VersionHandler::class)->isOutdated()) {
            return response()->view('updates.available');
        }
        return $next($request);
    }
    
  • Release Notes: Store release notes in config/application.php and fetch them dynamically:
    $notes = Application::config('releases.' . $version . '.notes');
    

4. Integration with Laravel Services

  • Service Providers: Extend Laravel’s service container by binding domain-specific services:
    $this->app->bind('App\Services\CustomService', function ($app) {
        return new \App\Services\CustomService(
            $app['config']['application.custom']
        );
    });
    
  • Event Listeners: Dispatch domain events (e.g., VersionUpdated) from the VersionHandler.

Gotchas and Tips

Pitfalls

  1. Config Overrides

    • Issue: Accidentally overwriting config values globally.
    • Fix: Use Application::setConfig() sparingly; prefer environment-specific configs or cached configs.
    • Tip: Add a debug log when config values change:
      if (config('app.debug')) {
          \Log::debug('Config updated:', ['key' => $key, 'value' => $value]);
      }
      
  2. Cron Job Conflicts

    • Issue: Duplicate cron jobs if CronManager is instantiated multiple times.
    • Fix: Ensure CronManager is a singleton in the service container:
      $this->app->singleton(CronManager::class, function ($app) {
          return new CronManager($app['config']['application.cron']);
      });
      
  3. Version Handling

    • Issue: Hardcoding version strings in multiple places.
    • Fix: Always use app(VersionHandler::class)->getVersion() instead of raw strings.
    • Tip: Add a helper function in AppServiceProvider:
      if (!function_exists('version')) {
          function version() {
              return app(\Draw\Application\Versioning\VersionHandler::class)->getVersion();
          }
      }
      

Debugging Tips

  1. Config Dumping Use the Application facade to dump the entire config:

    dd(Application::getConfig());
    
  2. 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);
        }
    }
    

Extension Points

  1. 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));
        }
    }
    
  2. Version Strategies Override VersionHandler to fetch versions from an API or Git tags:

    class ApiVersionHandler extends VersionHandler {
        public function getVersion() {
            return $this->fetchFromApi();
        }
    }
    
  3. 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);
            });
        }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator
equipc/laravel-email-database-log
adedsec/nova-translated-labels
wazza/laravel-db-encryption
hokny/laravel-generator
vonsogt/backup
jengo/queues
jeremykenedy/laravel-observability
coleus/frame
thtg88/journalism
filagora/filagora-ai-forums