1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chuckcms\Contacts; |
4
|
|
|
|
5
|
|
|
use Chuckcms\Contacts\Contracts\Contact as ContactContract; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\ServiceProvider; |
9
|
|
|
|
10
|
|
|
class ContactsServiceProvider extends ServiceProvider |
11
|
|
|
{ |
12
|
|
|
public function boot() |
13
|
|
|
{ |
14
|
|
|
$this->doPublishing(); |
15
|
|
|
|
16
|
|
|
$this->registerModelBindings(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function register() |
20
|
|
|
{ |
21
|
|
|
$this->mergeConfigFrom( |
22
|
|
|
__DIR__.'/../config/contacts.php', |
23
|
|
|
'contacts' |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function doPublishing() |
28
|
|
|
{ |
29
|
|
|
if (!function_exists('config_path')) { |
30
|
|
|
// function not available and 'publish' not relevant in Lumen (credit: Spatie) |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->publishes([ |
35
|
|
|
__DIR__.'/../config/contacts.php' => config_path('contacts.php'), |
36
|
|
|
], 'config'); |
37
|
|
|
|
38
|
|
|
$this->publishes([ |
39
|
|
|
__DIR__.'/../database/migrations/create_contacts_tables.php.stub' => $this->getMigrationFileName('create_contacts_tables.php'), |
40
|
|
|
], 'migrations'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function registerModelBindings() |
44
|
|
|
{ |
45
|
|
|
$config = $this->app->config['contacts.models']; |
|
|
|
|
46
|
|
|
|
47
|
|
|
$this->app->bind(ContactContract::class, $config['contact']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns existing migration file if found, else uses the current timestamp. |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getMigrationFileName($migrationFileName): string |
56
|
|
|
{ |
57
|
|
|
$timestamp = date('Y_m_d_His'); |
58
|
|
|
|
59
|
|
|
$filesystem = $this->app->make(Filesystem::class); |
60
|
|
|
|
61
|
|
|
return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR) |
|
|
|
|
62
|
|
|
->flatMap(function ($path) use ($filesystem, $migrationFileName) { |
63
|
|
|
return $filesystem->glob($path.'*_'.$migrationFileName); |
64
|
|
|
}) |
65
|
|
|
->push($this->app->databasePath()."/migrations/{$timestamp}_{$migrationFileName}") |
|
|
|
|
66
|
|
|
->first(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|