Skip to content

Commit 9021afe

Browse files
committedJan 16, 2017
add composer.json file
1 parent 5bb0513 commit 9021afe

7 files changed

+146
-16
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

‎composer.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "elimuswift/connection-resolver",
3+
"description": "A laravel package that dynamically switches between different database connections",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Master Weez",
9+
"email": "wizqydy@gmail.com"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"Elimuswift\\Connection": "src/"
15+
}
16+
},
17+
"minimum-stability": "dev",
18+
"require": {
19+
"laravel/framework": "~5.2"
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Elimuswift\Connection\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Elimuswift\Connection\SupervisorConfiguration;
7+
8+
class HandleResolver extends Command
9+
{
10+
/**
11+
* The supervisor config repository
12+
*
13+
* @var object
14+
**/
15+
protected $supervisor;
16+
17+
/**
18+
* The name and signature of the console command.
19+
*
20+
* @var string
21+
*/
22+
protected $signature = 'db-resolver:make-worker {tenant : The tenant\'s id or uuid or domain}';
23+
24+
/**
25+
* The console command description.
26+
*
27+
* @var string
28+
*/
29+
protected $description = 'Create a supervisor config file for the specified tenant';
30+
31+
/**
32+
* Create a new command instance.
33+
*
34+
* @return void
35+
*/
36+
public function __construct(SupervisorConfiguration $config)
37+
{
38+
$this->supervisor = $config;
39+
parent::__construct();
40+
}
41+
42+
/**
43+
* Execute the console command.
44+
*
45+
* @return mixed
46+
*/
47+
public function handle()
48+
{
49+
$this->supervisor->getTenant($this->argument('tenant'));
50+
$file = $this->supervisor->create();
51+
$this->info("Config file $file created");
52+
53+
}
54+
}

‎src/Connnection/ConnectionServiceProvider.php

+16-7
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,29 @@ public function register(){
1616
}
1717

1818
public function boot(Resolver $resolver){
19-
$this->publishes(array(
20-
realpath(__DIR__.'/../migrations') => database_path('migrations')
21-
));
22-
23-
//resolve tenant, catch PDOExceptions to prevent errors during migration
19+
$this->mergeConfigFrom(
20+
realpath(__DIR__ .'/../').'/config/elimuswift.php', 'elimuswift'
21+
);
22+
//resolve tenant, catch PDOExceptions to prevent errors during migration
2423
try {
25-
$resolver->resolveTenant();
24+
$resolver->resolveTenant();
2625
if(!$resolver->isResolved()){
2726
//abort('404');
2827
}
2928
} catch( \PDOException $e ) {
30-
//abort('404');
29+
throw new Exceptions\TenantNotResolvedException("Tenant not resolved or does not exist");
3130
}
3231

32+
$this->publishes(array(
33+
realpath(__DIR__.'/../migrations') => database_path('migrations')
34+
),'migrations');
35+
36+
if ($this->app->runningInConsole()) {
37+
$this->commands([
38+
Commands\HandleResolver::class,
39+
]);
40+
}
41+
3342
}
3443

3544
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace Elimuswift\Connection\Exceptions;
3+
4+
use Exception;
5+
6+
/**
7+
* Thrown when a tenant instance is not resolved
8+
*
9+
* @package Exception
10+
*
11+
**/
12+
class TenantNotResolvedException extends Exception
13+
{
14+
} // END class TenantNotResolvedException

‎src/Connnection/SupervisorConfiguration.php

+33-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @package supervisor-config
88
* @author The Weezqyd
99
**/
10+
use Exception;
11+
1012
class SupervisorConfiguration
1113
{
1214
/**
@@ -32,12 +34,6 @@ class SupervisorConfiguration
3234
stdout_logfile = {PATH}/storage/logs/worker.log';
3335

3436

35-
36-
public function __construct(Tenant $tenant)
37-
{
38-
$this->tenant = $tenant;
39-
}
40-
4137
/**
4238
* Set the config values
4339
*
@@ -63,10 +59,36 @@ private function set()
6359
public function create()
6460
{
6561
$configs = $this->set();
66-
$this->save($configs);
67-
return $configs;
62+
return $this->save($configs);
63+
64+
}
65+
/**
66+
* Set tenant object
67+
*
68+
* @return void
69+
* @param object $tenant
70+
**/
71+
private function setTenant(Tenant $tenant)
72+
{
73+
$this->tenant = $tenant;
6874
}
6975

76+
/**
77+
* Get tenant from storage
78+
*
79+
* @return void
80+
* @param mixed $tenant
81+
**/
82+
public function getTenant($tenant)
83+
{
84+
$model = new Tenant();
85+
86+
$instance = $model->whereId($tenant)->orWhere('uuid',$tenant)->orWhere('domain', $tenant)->first();
87+
if(is_null($instance))
88+
throw new Exceptions\TenantNotResolvedException("Tenant not resolved or does not exist");
89+
90+
$this->setTenant($instance);
91+
}
7092
/**
7193
* undocumented function
7294
*
@@ -75,6 +97,8 @@ public function create()
7597
**/
7698
protected function save($conf)
7799
{
78-
return file_put_contents(base_path('supervisor/elimuswift-'.$this->tenant->uuid.'.conf'), $conf);
100+
$file = base_path('supervisor/elimuswift-'.$this->tenant->uuid.'.conf');
101+
file_put_contents($file, $conf);
102+
return $file;
79103
}
80104
} // END class SupervisorConfiguration

‎src/config/elimuswift.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
return [
3+
'database' => [
4+
'default' => null,
5+
'tenant' => 'demo'
6+
],
7+
];

0 commit comments

Comments
 (0)
Please sign in to comment.