Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated readme with examples, addressed two real-world issues. #7

Merged
merged 1 commit into from
Dec 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 88 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,91 @@ MANIFOLD_PRODUCT_ID=YOUR-PRODUCT-ID
```

## Usage
Once installed and configured, your project/resource variables from Manifold
will be pulled into your Laravel application as configurations. Your variables
can be accessed using the `config` helper, using the key as it exists in
Manifold. For example, a variable stored in your Manifold resource named
`API_KEY` can be accessed using `config('API_KEY')`.

Note that keys are case sensitive. Also note that when variable keys conflict,
either with each other or with other Laravel configs, the last value with the
given key will be the one added to the configuration.
Once installed and configured, your project/resource credentials from Manifold
will be pulled into your Laravel application as configurations. Your credentials
can be accessed using the `config` helper, using the label of the resource and
the key of credential, as they exist in Manifold, using dot-notation. For
example, if you have a Mailgun resource setup with a credential named `API_KEY`,
it can be accessed using `config('mailgun.API_KEY')` (where `mailgun` is the
resource's label).

Note that keys are case sensitive. Also note that when configurations conflict
with other Laravel configs, the Manifold configurations will take priority.

## Aliases
In some cases, you may wish to use credentials from Manifold within
configuration files (inside your `/config` directory). The most obvious use case
would be database credentials that are needed within `/config/database.php`.
Since you cannot reliably access configurations from within configuration files
using the `config()` helper, aliases may be defined in your `config/manifold.php`
file. Aliases can be defined in arrays (as with standard configurations) or
using dot-notation for array keys. Define the existing config as the key and
the Manifold credential as the value. For example, pulling a PostgreSQL password
from a custom PostgreSQL service in Manifold could look like this:
`'database.connections.pgsql.password' => 'custom-pgsql.DB_PASSWORD'`. This will
pull the `DB_PASSWORD` credential from the `custom-pgsql` resource and assign
its value to `database.connections.pgsql.password`, so there is no need to
manipulate your existing `config/database.php` file. If the given
resource/credential combination does not exist, whatever was already defined in
`config/database.php` will be used.

## Examples
1. You have a project in Manifold with an ID of `01234567890123456789012345678`.
You want your Mailgun API key available in a controller method. Your Mailgun
resource is named `mailgun` and the API key credential is `API_KEY`.

Add the following to `.env`
```
MANIFOLD_API_TOKEN=0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789AB
MANIFOLD_PROJECT_ID=01234567890123456789012345678
```

In your controller's php file:
```
class MyController extends Controller{
public function process_mail(){

$mailgun_key = config('mailgun.API_KEY');

//mail processing logic here
}
}
```

2. You have a project in Manifold with an ID of `01234567890123456789012345678`.
You want your PostgreSQL credentials from your custom service stored in Manifold.
Your custom service is named `custom-pgsql` and the PostgreSQL credential keys
are `DB_HOST`, `DB_DATABASE`, `DB_USERNAME`, and `DB_PASSWORD`.

Add the following to `.env`
```
MANIFOLD_API_TOKEN=0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789AB
MANIFOLD_PROJECT_ID=01234567890123456789012345678
```

In your `config/manifold.php`:
```
return [
'token' => env('MANIFOLD_API_TOKEN', null),
'resource_id' => env('MANIFOLD_RESOURCE_ID', null),
'project_id' => env('MANIFOLD_PROJECT_ID', null),
'product_id' => env('MANIFOLD_PRODUCT_ID', null),
'aliases' => [
/*
note that while you can mix and match array syntax and dot-notation
it's best to use one or the other, this merely illustrates that both
are possible
*/
'database.connections.pgsql.password' => 'custom-pgsql.DB_PASSWORD',
'database' => [
'connections' => [
'pgsql' => [
'host' => 'custom-pgsql.DB_HOST',
'database' => 'custom-pgsql.DB_DATABASE',
'username' => 'custom-pgsql.DB_USERNAME',
]
]
]
],
];
```
28 changes: 16 additions & 12 deletions src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,24 @@ public function resources($query_string = null){
}

public function load_resource($resource_id){
$resources = $this->server_request('credentials?resource_id=' . $resource_id);

$response = [];

if(!is_null($resources)){
$resources = collect(json_decode($resources));
$resources->each(function($resource) use(&$response){
if($resource && isset($resource->body) && isset($resource->body->values)){
collect($resource->body->values)->each(function($value, $key) use(&$response){
$response[$key] = $value;
});
}
});
$resource = json_decode($this->server_request('resources/' . $resource_id));
if(isset($resource->body) && isset($resource->body->label)){
$label = $resource->body->label;

$resources = $this->server_request('credentials?resource_id=' . $resource_id);

if(!is_null($resources)){
$resources = collect(json_decode($resources));
$resources->each(function($resource) use(&$response, $label){
if($resource && isset($resource->body) && isset($resource->body->values)){
collect($resource->body->values)->each(function($value, $key) use(&$response, $label){
$response["$label.$key"] = $value;
});
}
});
}
}

return empty($response) ? null : $response;
Expand All @@ -66,7 +71,6 @@ public function server_request($url){

public function test_credentials(){
$response = json_decode($this->server_request('resources'));
// dd($response);
if((isset($response->type) && $response->type == 'unauthorized') || is_null($response)){
return false;
}else{
Expand Down
10 changes: 10 additions & 0 deletions src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public function load_configs(){
collect($configs)->each(function($value, $key){
config([$key => $value]);
});

$aliases = config('manifold.aliases');
if(!empty($aliases)){
$array_dots = collect(array_dot($aliases));
$array_dots->each(function($value, $key){
if(config($value))
config([$key => config($value)]);
});
}

return $configs;
}

Expand Down
3 changes: 3 additions & 0 deletions src/config/manifold.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
'resource_id' => env('MANIFOLD_RESOURCE_ID', null),
'project_id' => env('MANIFOLD_PROJECT_ID', null),
'product_id' => env('MANIFOLD_PRODUCT_ID', null),
'aliases' => [

],
];
3 changes: 0 additions & 3 deletions src/manifoldServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ public function boot()
}

$core = new Core;
//normal boot
$core->load_configs();
//forcing reload of fresh data
// $core->refresh();

}
/**
Expand Down