Skip to content

Commit 7e67a94

Browse files
committed
initial commit
0 parents  commit 7e67a94

16 files changed

+683
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
composer.phar
3+
composer.lock
4+
.DS_Store

.travis.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
3+
php:
4+
- 5.3
5+
- 5.4
6+
- 5.5
7+
8+
before_script:
9+
- curl -s http://getcomposer.org/installer | php
10+
- php composer.phar install --dev
11+
12+
script: phpunit

CONTRIBUTING.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Contributing
2+
3+
If you want to add a new feature, make an issue rather than a pull request.
4+
5+
Code style is pretty much PSR-2, but with tabs instead of 4 spaces. Use common sense.
6+
7+
Make separate pull requests for separate changes/issues.
8+
9+
Please don't be offended if I don't merge a pull request :)

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Andreas Lutro
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Laravel Settings
2+
3+
Persistant settings for Laravel 4.
4+
5+
### Installation
6+
7+
`composer require anlutro/l4-settings` - pick the latest version from Packagist or the list of tags on Github.
8+
9+
Add `anlutro\LaravelSettings\ServiceProvider` to the array of providers in `app/config/app.php`.
10+
11+
Optional: add `'Settings' => 'anlutro\LaravelSettings\Facade'` to the array of aliases in the same file.
12+
13+
Publish the config file by running `php artisan config:publish anlutro/l4-settings`.
14+
15+
If you use the database store you need to create the table yourself. It needs two columns - key and value, both should be varchars - how long depends on the amount of data you plan to store there.
16+
17+
### Usage
18+
19+
You can either access the setting store via its facade or inject it by type-hinting towards the abstract class `anlutro\LaravelSettings\SettingStore`.
20+
21+
```php
22+
<?php
23+
Setting::set('foo', 'bar');
24+
Setting::get('foo');
25+
Setting::get('nested.element');
26+
?>
27+
```
28+
29+
You can call `Setting::save()` explicitly to save changes made, but the library makes sure to auto-save every time the application shuts down if anything has been changed.
30+
31+
This package uses the Laravel 4 Manager class under the hood, so it's easy to add your own custom session store driver if you want to store in some other way.
32+
33+
```php
34+
<?php
35+
class MyStore extends anlutro\LaravelSettings\SettingStore {
36+
// ...
37+
}
38+
Setting::extend('mystore', function($app) {
39+
return $app->make('MyStore');
40+
});
41+
?>
42+
```
43+
44+
## Contact
45+
46+
Open an issue on GitHub if you have any problems or suggestions.
47+
48+
## License
49+
50+
The contents of this repository is released under the [MIT license](http://opensource.org/licenses/MIT).

composer.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "anlutro/l4-settings",
3+
"description": "Persistant settings in Laravel 4.",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Andreas Lutro",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"require": {
12+
"php": ">=5.3.0",
13+
"illuminate/support": "4.1.*"
14+
},
15+
"suggest": {
16+
"illuminate/filesystem": "Save settings to a JSON file.",
17+
"illuminate/database": "Save settings to a database table."
18+
},
19+
"require-dev": {
20+
"mockery/mockery": "dev-master@dev"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"anlutro\\LaravelSettings\\": "src/"
25+
}
26+
},
27+
"minimum-stability": "stable"
28+
}

phpunit.xml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Package Test Suite">
15+
<directory suffix=".php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>

src/DatabaseSettingStore.php

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Laravel 4 - Persistant Settings
4+
*
5+
* @author Andreas Lutro <[email protected]>
6+
* @license http://opensource.org/MIT
7+
* @package l4-settings
8+
*/
9+
10+
namespace anlutro\LaravelSettings;
11+
12+
use Illuminate\Database\Connection;
13+
14+
class DatabaseSettingStore extends SettingStore
15+
{
16+
protected $connection;
17+
protected $table;
18+
19+
public function __construct(Connection $connection, $table = null)
20+
{
21+
$this->connection = $connection;
22+
$this->table = $table ?: 'persistant_settings';
23+
}
24+
25+
protected function write(array $data)
26+
{
27+
$this->newQuery()->truncate();
28+
$dbData = $this->prepareWriteData($this->data);
29+
$this->newQuery()->insert($dbData);
30+
}
31+
32+
/**
33+
* Transforms settings data into an array ready to be insterted into the
34+
* database.
35+
*
36+
* ['foo' => ['bar' => 1, 'baz', => 2]] is first transformed into
37+
* ['foo.bar' => 1, 'foo.baz' => 2] which is then transformed into
38+
* [['key' => 'foo.bar', 'value' => 1], ...]
39+
*
40+
* ['foo' => ['bar', 'baz']] is transformed into
41+
* ['foo.0' => 'bar', 'foo.1' => 'baz'] and so on.
42+
*
43+
* @param array $data
44+
*
45+
* @return array
46+
*/
47+
protected function prepareWriteData($data)
48+
{
49+
$data = array_dot($data);
50+
return array_map(function($key, $value) {
51+
return array('key' => $key, 'value' => $value);
52+
}, array_keys($data), array_values($data));
53+
}
54+
55+
protected function read()
56+
{
57+
return $this->parseReadData($this->newQuery()->get());
58+
}
59+
60+
public function parseReadData($data)
61+
{
62+
$results = array();
63+
64+
foreach ($data as $row) {
65+
if (is_array($row)) {
66+
$key = $row['key'];
67+
$value = $row['value'];
68+
} elseif (is_object($row)) {
69+
$key = $row->key;
70+
$value = $row->value;
71+
}
72+
73+
array_set($results, $key, $value);
74+
}
75+
76+
return $results;
77+
}
78+
79+
protected function newQuery()
80+
{
81+
return $this->connection->table($this->table);
82+
}
83+
}

src/Facade.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Laravel 4 - Persistant Settings
4+
*
5+
* @author Andreas Lutro <[email protected]>
6+
* @license http://opensource.org/MIT
7+
* @package l4-settings
8+
*/
9+
10+
namespace anlutro\LaravelSettings;
11+
12+
class Facade extends \Illuminate\Support\Facades\Facade
13+
{
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'anlutro\LaravelSettings\SettingStore';
17+
}
18+
}

src/JsonSettingStore.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Laravel 4 - Persistant Settings
4+
*
5+
* @author Andreas Lutro <[email protected]>
6+
* @license http://opensource.org/MIT
7+
* @package l4-settings
8+
*/
9+
10+
namespace anlutro\LaravelSettings;
11+
12+
use Illuminate\Filesystem\Filesystem;
13+
14+
class JsonSettingStore extends SettingStore
15+
{
16+
public function __construct(Filesystem $files, $path = null)
17+
{
18+
$this->files = $files;
19+
$this->path = $path ?: storage_path() . '/settings.json';
20+
}
21+
22+
protected function read()
23+
{
24+
if (!$this->files->exists($this->path)) {
25+
return array();
26+
}
27+
28+
$contents = $this->files->get($this->path);
29+
30+
$data = json_decode($contents, true);
31+
32+
if ($data === null) {
33+
throw new \RuntimeException("Invalid JSON in {$this->path}");
34+
}
35+
36+
return $data;
37+
}
38+
39+
protected function write(array $data)
40+
{
41+
$contents = json_encode($data);
42+
43+
$this->files->put($this->path, $contents);
44+
}
45+
}

src/ServiceProvider.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Laravel 4 - Persistant Settings
4+
*
5+
* @author Andreas Lutro <[email protected]>
6+
* @license http://opensource.org/MIT
7+
* @package l4-settings
8+
*/
9+
10+
namespace anlutro\LaravelSettings;
11+
12+
class ServiceProvider extends \Illuminate\Support\ServiceProvider
13+
{
14+
/**
15+
* This provider is deferred and should be lazy loaded.
16+
*
17+
* @var boolean
18+
*/
19+
protected $defer = true;
20+
21+
/**
22+
* Register IoC bindings.
23+
*/
24+
public function register()
25+
{
26+
/**
27+
* Bind the manager as a singleton on the container.
28+
*/
29+
$this->app->bindShared('anlutro\LaravelSettings\SettingsManager', function($app) {
30+
/**
31+
* When the class has been resolved once, make sure that settings
32+
* are saved when the application shuts down.
33+
*/
34+
$app->shutdown(function($app) {
35+
$app->make('anlutro\LaravelSettings\SettingStore')->save();
36+
});
37+
38+
/**
39+
* Construct the actual manager.
40+
*/
41+
return new SettingsManager($this->app);
42+
});
43+
44+
/**
45+
* Provide a shortcut to the SettingStore for injecting into classes.
46+
*/
47+
$this->app->bind('anlutro\LaravelSettings\SettingStore', function($app) {
48+
return $app->make('anlutro\LaravelSettings\SettingsManager')->driver();
49+
});
50+
}
51+
52+
/**
53+
* Boot the package.
54+
*/
55+
public function boot()
56+
{
57+
$this->app['config']->package(
58+
'anlutro/l4-settings', __DIR__ . '/config', 'anlutro/l4-settings'
59+
);
60+
}
61+
62+
/**
63+
* Which IoC bindings the provider provides.
64+
*
65+
* @return array
66+
*/
67+
public function provides()
68+
{
69+
return array(
70+
'anlutro\LaravelSettings\SettingsManager',
71+
'anlutro\LaravelSettings\SettingStore',
72+
);
73+
}
74+
}

0 commit comments

Comments
 (0)