Files
InvoiceShelf/app/Providers/AppServiceProvider.php
Darko Gjorgjijoski 6d1816bd1b refactor: reorganize app/Services and app/Support by domain
The app/Services/ directory had grown into 22 flat files at the root plus 7 uneven subdirectories — finding anything required scrolling through an alphabetical mix of small CRUD services, infrastructure drivers, and install-time utilities. This commit groups services by domain, folds Backup into a new Storage namespace, and moves framework-infrastructure and install-time helpers out of Services and into Support where they belong.

New Services layout: Documents/ (Invoice, Estimate, RecurringInvoice, Payment, Expense, Transaction, DocumentItem, SerialNumber, Currency — matches the 'Documents' navigation group); Company/ (Company, Member, Invitation); Mail/ (MailConfiguration, CompanyMailConfig); Storage/ (FileDisk, plus Backup folded in). ExchangeRateProviderService moves next to its drivers in ExchangeRate/; FontService moves into Pdf/ where it belongs. CustomerService, ItemService, CustomFieldService stay at the Services root as standalone single-file domains.

Moves to Support/: Hashids/ (library wrapper — not business logic); Setup/ (one-shot install-time utilities — stateless helpers); Pdf/ (ImageUtils, PdfTemplateUtils, plus the existing PdfHtmlSanitizer consolidated into the same subdir). These are all framework infrastructure and stateless utilities — the 'service' label never really fit them.

Namespace declarations in 29 moved files updated to match new paths. 62 consumer files (controllers, other services, tests, database factories, seeders, routes, bootstrap/providers.php) have their use statements rewritten via a literal-string replacement script — no regex meant no risk of half-matching. Three Documents services needed an explicit 'use App\Services\Mail\CompanyMailConfigService' added because the same-namespace short reference they relied on no longer resolves after the split.

Verified: composer dump-autoload, 350 tests pass (850 assertions), vendor/bin/pint clean, npm run build succeeds.
2026-04-11 10:00:00 +02:00

192 lines
6.5 KiB
PHP

<?php
namespace App\Providers;
use App\Policies\CompanyPolicy;
use App\Policies\CustomerPolicy;
use App\Policies\DashboardPolicy;
use App\Policies\EstimatePolicy;
use App\Policies\ExpensePolicy;
use App\Policies\InvoicePolicy;
use App\Policies\ItemPolicy;
use App\Policies\ModulesPolicy;
use App\Policies\NotePolicy;
use App\Policies\OwnerPolicy;
use App\Policies\PaymentPolicy;
use App\Policies\RecurringInvoicePolicy;
use App\Policies\ReportPolicy;
use App\Policies\RolePolicy;
use App\Policies\SettingsPolicy;
use App\Policies\UserPolicy;
use App\Support\BouncerDefaultScope;
use App\Support\InstallWizardAuth;
use App\Support\Setup\InstallUtils;
use Gate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Broadcast;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\ServiceProvider;
use Laravel\Sanctum\Sanctum;
use Silber\Bouncer\Database\Models as BouncerModels;
use Silber\Bouncer\Database\Role;
use View;
class AppServiceProvider extends ServiceProvider
{
/**
* The path to your application's "home" route.
*
* Typically, users are redirected here after authentication.
*
* @var string
*/
public const HOME = '/admin/dashboard';
/**
* The path to the "customer home" route for your application.
*
* This is used by Laravel authentication to redirect customers after login.
*
* @var string
*/
public const CUSTOMER_HOME = '/customer/dashboard';
/**
* Bootstrap any application services.
*/
public function boot(): void
{
$this->configureInstallWizardTokenAuth();
if (InstallUtils::isDbCreated()) {
$this->addMenus();
}
Gate::policy(Role::class, RolePolicy::class);
View::addNamespace('pdf_templates', storage_path('app/templates/pdf'));
$this->bootAuth();
$this->bootBroadcast();
// In demo mode, prevent all outgoing emails and notifications
if (config('app.env') === 'demo') {
Mail::fake();
Notification::fake();
}
}
/**
* Register any application services.
*/
public function register(): void
{
BouncerModels::scope(new BouncerDefaultScope);
}
public function addMenus()
{
// main menu
\Menu::make('main_menu', function ($menu) {
foreach (config('invoiceshelf.main_menu') as $data) {
$this->generateMenu($menu, $data);
}
});
// admin menu (super admin mode)
\Menu::make('admin_menu', function ($menu) {
foreach (config('invoiceshelf.admin_menu') as $data) {
$this->generateMenu($menu, $data);
}
});
// setting menu
\Menu::make('setting_menu', function ($menu) {
foreach (config('invoiceshelf.setting_menu') as $data) {
$this->generateMenu($menu, $data);
}
});
\Menu::make('customer_portal_menu', function ($menu) {
foreach (config('invoiceshelf.customer_menu') as $data) {
$this->generateMenu($menu, $data);
}
});
}
public function generateMenu($menu, $data)
{
$menu->add($data['title'], $data['link'])
->data('icon', $data['icon'])
->data('name', $data['name'])
->data('owner_only', $data['owner_only'])
->data('super_admin_only', $data['super_admin_only'] ?? false)
->data('ability', $data['ability'])
->data('model', $data['model'])
->data('group', $data['group'])
->data('group_label', $data['group_label'] ?? '')
->data('priority', $data['priority'] ?? 100);
}
public function bootAuth()
{
Gate::define('create company', [CompanyPolicy::class, 'create']);
Gate::define('transfer company ownership', [CompanyPolicy::class, 'transferOwnership']);
Gate::define('delete company', [CompanyPolicy::class, 'delete']);
Gate::define('manage modules', [ModulesPolicy::class, 'manageModules']);
Gate::define('manage settings', [SettingsPolicy::class, 'manageSettings']);
Gate::define('manage company', [SettingsPolicy::class, 'manageCompany']);
Gate::define('manage backups', [SettingsPolicy::class, 'manageBackups']);
Gate::define('manage file disk', [SettingsPolicy::class, 'manageFileDisk']);
Gate::define('manage email config', [SettingsPolicy::class, 'manageEmailConfig']);
Gate::define('manage pdf config', [SettingsPolicy::class, 'managePDFConfig']);
Gate::define('manage notes', [NotePolicy::class, 'manageNotes']);
Gate::define('view notes', [NotePolicy::class, 'viewNotes']);
Gate::define('send invoice', [InvoicePolicy::class, 'send']);
Gate::define('send estimate', [EstimatePolicy::class, 'send']);
Gate::define('send payment', [PaymentPolicy::class, 'send']);
Gate::define('delete multiple items', [ItemPolicy::class, 'deleteMultiple']);
Gate::define('delete multiple customers', [CustomerPolicy::class, 'deleteMultiple']);
Gate::define('delete multiple users', [UserPolicy::class, 'deleteMultiple']);
Gate::define('delete multiple invoices', [InvoicePolicy::class, 'deleteMultiple']);
Gate::define('delete multiple estimates', [EstimatePolicy::class, 'deleteMultiple']);
Gate::define('delete multiple expenses', [ExpensePolicy::class, 'deleteMultiple']);
Gate::define('delete multiple payments', [PaymentPolicy::class, 'deleteMultiple']);
Gate::define('delete multiple recurring invoices', [RecurringInvoicePolicy::class, 'deleteMultiple']);
Gate::define('view dashboard', [DashboardPolicy::class, 'view']);
Gate::define('view report', [ReportPolicy::class, 'viewReport']);
Gate::define('owner only', [OwnerPolicy::class, 'managedByOwner']);
}
public function bootBroadcast()
{
Broadcast::routes(['middleware' => 'api.auth']);
}
private function configureInstallWizardTokenAuth(): void
{
Sanctum::authenticateAccessTokensUsing(function ($accessToken, bool $isValid): bool {
if (! $isValid) {
return false;
}
$request = request();
if (! $request instanceof Request || ! $request->attributes->get('install_wizard', false)) {
return $isValid;
}
return $accessToken->can(InstallWizardAuth::TOKEN_ABILITY);
});
}
}