mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-09 23:24:06 -08:00
Break out service providers by responsibility
This commit is contained in:
parent
cceeb5c8a2
commit
231dea0ebc
|
@ -1,9 +1,8 @@
|
|||
<?php
|
||||
namespace App\Providers;
|
||||
|
||||
use Validator;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use DB;
|
||||
use Log;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use App\Observers\AssetObserver;
|
||||
|
@ -19,7 +18,7 @@ use App\Models\Component;
|
|||
|
||||
|
||||
/**
|
||||
* This service provider handles a few custom validation rules.
|
||||
* This service provider handles setting the observers on models
|
||||
*
|
||||
* PHP version 5.5.9
|
||||
* @version v3.0
|
||||
|
@ -42,82 +41,7 @@ class AppServiceProvider extends ServiceProvider
|
|||
Component::observe(ComponentObserver::class);
|
||||
Consumable::observe(ConsumableObserver::class);
|
||||
License::observe(LicenseObserver::class);
|
||||
|
||||
// Email array validator
|
||||
Validator::extend('email_array', function ($attribute, $value, $parameters, $validator) {
|
||||
$value = str_replace(' ', '', $value);
|
||||
$array = explode(',', $value);
|
||||
|
||||
foreach ($array as $email) { //loop over values
|
||||
$email_to_validate['alert_email'][]=$email;
|
||||
}
|
||||
|
||||
$rules = array('alert_email.*'=>'email');
|
||||
$messages = array(
|
||||
'alert_email.*'=>trans('validation.email_array')
|
||||
);
|
||||
|
||||
$validator = Validator::make($email_to_validate, $rules, $messages);
|
||||
|
||||
return $validator->passes();
|
||||
|
||||
});
|
||||
|
||||
// Unique only if undeleted
|
||||
// This works around the use case where multiple deleted items have the same unique attribute.
|
||||
// (I think this is a bug in Laravel's validator?)
|
||||
Validator::extend('unique_undeleted', function ($attribute, $value, $parameters, $validator) {
|
||||
|
||||
if (count($parameters)) {
|
||||
$count = DB::table($parameters[0])->select('id')->where($attribute, '=', $value)->whereNull('deleted_at')->where('id', '!=', $parameters[1])->count();
|
||||
return $count < 1;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
// Yo dawg. I heard you like validators.
|
||||
// This validates the custom validator regex in custom fields.
|
||||
// We're just checking that the regex won't throw an exception, not
|
||||
// that it's actually correct for what the user intended.
|
||||
|
||||
Validator::extend('valid_regex', function ($attribute, $value, $parameters, $validator) {
|
||||
|
||||
// Make sure it's not just an ANY format
|
||||
if ($value!='') {
|
||||
|
||||
// Check that the string starts with regex:
|
||||
if (strpos($value, 'regex:') === FALSE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$test_string = 'My hovercraft is full of eels';
|
||||
|
||||
// We have to stip out the regex: part here to check with preg_match
|
||||
$test_pattern = str_replace('regex:','', $value);
|
||||
|
||||
try {
|
||||
preg_match($test_pattern, $test_string, $matches);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
// Share common setting variables with all views.
|
||||
view()->composer('*', function ($view) {
|
||||
$view->with('snipeSettings', \App\Models\Setting::getSettings());
|
||||
});
|
||||
|
||||
// Set the monetary locale to the configured locale to make helper::parseFloat work.
|
||||
setlocale(LC_MONETARY, config('app.locale'));
|
||||
setlocale(LC_NUMERIC, config('app.locale'));
|
||||
|
||||
}
|
||||
|
||||
|
|
121
app/Providers/SettingsServiceProvider.php
Normal file
121
app/Providers/SettingsServiceProvider.php
Normal file
|
@ -0,0 +1,121 @@
|
|||
<?php
|
||||
namespace App\Providers;
|
||||
|
||||
use Validator;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use DB;
|
||||
use Log;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* This service provider handles sharing the snipeSettings variable, and sets
|
||||
* some common upload path and image urls.
|
||||
*
|
||||
* PHP version 5.5.9
|
||||
* @version v3.0
|
||||
*/
|
||||
|
||||
class SettingsServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Custom email array validation
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @since [v3.0]
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
|
||||
|
||||
// Share common setting variables with all views.
|
||||
view()->composer('*', function ($view) {
|
||||
$view->with('snipeSettings', \App\Models\Setting::getSettings());
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Set some common variables so that they're globally available.
|
||||
* The paths should always be public (versus private uploads)
|
||||
*/
|
||||
// Model paths and URLs
|
||||
\App::singleton('models_upload_path', function(){
|
||||
return public_path('/uploads/models/');
|
||||
});
|
||||
|
||||
\App::singleton('models_upload_url', function(){
|
||||
return url('/'.'uploads/models/');
|
||||
});
|
||||
|
||||
// Categories
|
||||
\App::singleton('categories_upload_path', function(){
|
||||
return public_path('/uploads/categories/');
|
||||
});
|
||||
|
||||
\App::singleton('categories_upload_url', function(){
|
||||
return url('/'.'uploads/categories/');
|
||||
});
|
||||
|
||||
// Locations
|
||||
\App::singleton('locations_upload_path', function(){
|
||||
return public_path('/uploads/locations/');
|
||||
});
|
||||
|
||||
\App::singleton('locations_upload_url', function(){
|
||||
return url('/'.'uploads/locations/');
|
||||
});
|
||||
|
||||
// Users
|
||||
\App::singleton('users_upload_path', function(){
|
||||
return public_path('/uploads/users/');
|
||||
});
|
||||
|
||||
\App::singleton('users_upload_url', function(){
|
||||
return url('/'.'uploads/users/');
|
||||
});
|
||||
|
||||
// Manufacturers
|
||||
\App::singleton('manufacturers_upload_path', function(){
|
||||
return public_path('/uploads/manufacturers/');
|
||||
});
|
||||
|
||||
\App::singleton('manufacturers_upload_url', function(){
|
||||
return url('/'.'uploads/manufacturers/');
|
||||
});
|
||||
|
||||
// Suppliers
|
||||
\App::singleton('suppliers_upload_path', function(){
|
||||
return public_path('/uploads/suppliers/');
|
||||
});
|
||||
|
||||
\App::singleton('suppliers_upload_url', function(){
|
||||
return url('/'.'uploads/suppliers/');
|
||||
});
|
||||
|
||||
// Departments
|
||||
\App::singleton('departments_upload_path', function(){
|
||||
return public_path('/uploads/departments/');
|
||||
});
|
||||
|
||||
\App::singleton('departments_upload_url', function(){
|
||||
return url('/'.'uploads/departments/');
|
||||
});
|
||||
|
||||
|
||||
// Set the monetary locale to the configured locale to make helper::parseFloat work.
|
||||
setlocale(LC_MONETARY, config('app.locale'));
|
||||
setlocale(LC_NUMERIC, config('app.locale'));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Register any application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
117
app/Providers/ValidationServiceProvider.php
Normal file
117
app/Providers/ValidationServiceProvider.php
Normal file
|
@ -0,0 +1,117 @@
|
|||
<?php
|
||||
namespace App\Providers;
|
||||
|
||||
use Validator;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use DB;
|
||||
use Log;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use App\Observers\AssetObserver;
|
||||
use App\Observers\LicenseObserver;
|
||||
use App\Observers\AccessoryObserver;
|
||||
use App\Observers\ConsumableObserver;
|
||||
use App\Observers\ComponentObserver;
|
||||
use App\Models\Asset;
|
||||
use App\Models\License;
|
||||
use App\Models\Accessory;
|
||||
use App\Models\Consumable;
|
||||
use App\Models\Component;
|
||||
|
||||
|
||||
/**
|
||||
* This service provider handles a few custom validation rules.
|
||||
*
|
||||
* PHP version 5.5.9
|
||||
* @version v3.0
|
||||
*/
|
||||
|
||||
class ValidationServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Custom email array validation
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @since [v3.0]
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
|
||||
// Email array validator
|
||||
Validator::extend('email_array', function ($attribute, $value, $parameters, $validator) {
|
||||
$value = str_replace(' ', '', $value);
|
||||
$array = explode(',', $value);
|
||||
|
||||
foreach ($array as $email) { //loop over values
|
||||
$email_to_validate['alert_email'][]=$email;
|
||||
}
|
||||
|
||||
$rules = array('alert_email.*'=>'email');
|
||||
$messages = array(
|
||||
'alert_email.*'=>trans('validation.email_array')
|
||||
);
|
||||
|
||||
$validator = Validator::make($email_to_validate, $rules, $messages);
|
||||
|
||||
return $validator->passes();
|
||||
|
||||
});
|
||||
|
||||
|
||||
// Unique only if undeleted
|
||||
// This works around the use case where multiple deleted items have the same unique attribute.
|
||||
// (I think this is a bug in Laravel's validator?)
|
||||
Validator::extend('unique_undeleted', function ($attribute, $value, $parameters, $validator) {
|
||||
|
||||
if (count($parameters)) {
|
||||
$count = DB::table($parameters[0])->select('id')->where($attribute, '=', $value)->whereNull('deleted_at')->where('id', '!=', $parameters[1])->count();
|
||||
return $count < 1;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
// Yo dawg. I heard you like validators.
|
||||
// This validates the custom validator regex in custom fields.
|
||||
// We're just checking that the regex won't throw an exception, not
|
||||
// that it's actually correct for what the user intended.
|
||||
|
||||
Validator::extend('valid_regex', function ($attribute, $value, $parameters, $validator) {
|
||||
|
||||
// Make sure it's not just an ANY format
|
||||
if ($value!='') {
|
||||
|
||||
// Check that the string starts with regex:
|
||||
if (strpos($value, 'regex:') === FALSE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$test_string = 'My hovercraft is full of eels';
|
||||
|
||||
// We have to stip out the regex: part here to check with preg_match
|
||||
$test_pattern = str_replace('regex:','', $value);
|
||||
|
||||
try {
|
||||
preg_match($test_pattern, $test_string, $matches);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Register any application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
|
@ -297,6 +297,8 @@ return [
|
|||
App\Providers\AuthServiceProvider::class,
|
||||
App\Providers\EventServiceProvider::class,
|
||||
App\Providers\RouteServiceProvider::class,
|
||||
App\Providers\SettingsServiceProvider::class,
|
||||
App\Providers\ValidationServiceProvider::class,
|
||||
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in a new issue