laractions 0.1.0

Actions for Laravel Rust: encapsulate business logic in classes that run now or on the queue.
Documentation
use laravel::container::{Container, Registrar};
use laravel::foundation::ServiceProvider;

use crate::registry;

/// Wires Laractions into the application: the `make:action` and `list:actions` commands, and the
/// `action_traces` migration when you ask for it.
///
/// ```ignore
/// Application::configure().with_providers(|providers| {
///     providers.add(LaractionsServiceProvider::new());
/// })
/// ```
#[derive(Debug, Clone, Default)]
pub struct LaractionsServiceProvider {
    migrations: bool,
}

impl LaractionsServiceProvider {
    /// A provider with the commands and no migration.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Register the `action_traces` migration, which PHP publishes with the `laractions-migrations`
    /// tag. Needed only for `trace()`.
    #[must_use]
    pub fn with_migrations(mut self) -> Self {
        self.migrations = true;

        self
    }
}

impl ServiceProvider for LaractionsServiceProvider {
    fn register(&self, app: &mut Registrar) {
        app.discover(env!("CARGO_CRATE_NAME"));

        if self.migrations {
            app.migrations().add(laravel::db::migrations!("database/migrations"));
        }
    }

    async fn boot(&self, _app: &Container) -> laravel::Result<()> {
        registry::check_names().map_err(|message| laravel::Error::msg(message))
    }
}