Skip to content

Commit 743d5ca

Browse files
committedApr 10, 2014
initial commit
0 parents  commit 743d5ca

14 files changed

+590
-0
lines changed
 

‎.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
nbproject
2+
._*
3+
.~lock.*
4+
.buildpath
5+
.DS_Store
6+
.idea
7+
.project
8+
.settings
9+
*/logs/*
10+
*/cache/*
11+
12+
vendor/*
13+
composer.phar

‎Licence.txt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Copyright (c) 2014 GOTO Hidenori <hidenorigoto@gmail.com>,
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice,
8+
this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
13+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
17+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23+
POSSIBILITY OF SUCH DAMAGE.

‎README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Configurationサンプル
2+
3+
Symfony/Configコンポーネントを使ったコンフィギュレーションの例。
4+
5+
- [Symfony/Config](https://github.com/akanehara/ginq)
6+
7+
サンプルの設定ファイルは、以下のブログにあったものを利用させて頂きました。
8+
9+
- [設定の仕様をドキュメントに書くのではなく、テストにしてしまう - $shibayu36->blog;](http://shibayu36.hatenablog.com/entry/2014/04/10/100242)
10+
11+
## 著作権
12+
13+
Copyright (c) 2014 GOTO Hidenori &lt;hidenorigoto@gmail.com&gt;, All rights reserved.
14+
15+
## ライセンス
16+
17+
[修正 BSD ライセンス](http://www.opensource.org/licenses/bsd-license.php)

‎bin/.placeholder

Whitespace-only changes.

‎bin/dump.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
use Example\Config\BlogConfiguration;
3+
use Symfony\Component\Config\Definition\Dumper\YamlReferenceDumper;
4+
5+
require_once __DIR__.'/../vendor/autoload.php';
6+
7+
$config = new BlogConfiguration();
8+
$dumper = new YamlReferenceDumper();
9+
echo $dumper->dump($config);

‎bin/test_json.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
3+
use Symfony\Component\Config\FileLocator;
4+
use Symfony\Component\Config\Loader\DelegatingLoader;
5+
use Symfony\Component\Config\Loader\LoaderResolver;
6+
7+
use Example\Config\JsonFileLoader;
8+
9+
require_once __DIR__.'/../vendor/autoload.php';
10+
11+
$locator = new FileLocator(__DIR__.'/../config');
12+
13+
$loaderResolver = new LoaderResolver([new JsonFileLoader($locator)]);
14+
$delegatingLoader = new DelegatingLoader($loaderResolver);
15+
16+
try {
17+
$config = $delegatingLoader->load('config.json');
18+
var_dump($config);
19+
} catch (InvalidConfigurationException $e)
20+
{
21+
echo "コンフィギュレーション構文エラー".PHP_EOL;
22+
echo $e->getMessage().PHP_EOL;
23+
}

‎bin/test_yaml.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
3+
use Symfony\Component\Config\FileLocator;
4+
use Symfony\Component\Config\Loader\DelegatingLoader;
5+
use Symfony\Component\Config\Loader\LoaderResolver;
6+
7+
use Example\Config\YamlFileLoader;
8+
9+
require_once __DIR__.'/../vendor/autoload.php';
10+
11+
$locator = new FileLocator(__DIR__.'/../config');
12+
13+
$loaderResolver = new LoaderResolver([new YamlFileLoader($locator)]);
14+
$delegatingLoader = new DelegatingLoader($loaderResolver);
15+
16+
try {
17+
$config = $delegatingLoader->load('config.yml');
18+
var_dump($config);
19+
} catch (InvalidConfigurationException $e)
20+
{
21+
echo "コンフィギュレーション構文エラー".PHP_EOL;
22+
echo $e->getMessage().PHP_EOL;
23+
}

‎composer.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "example/config",
3+
"description":" ",
4+
"keywords":[
5+
],
6+
"config":{
7+
"bin-dir": "bin"
8+
},
9+
"license": "BSD-3-Clause",
10+
"autoload":{
11+
"psr-4":{
12+
"Example\\Config\\": "src/"
13+
}
14+
},
15+
"require": {
16+
"symfony/config": "~2.4",
17+
"symfony/yaml": "~2.4",
18+
"respect/validation": "~0.5"
19+
}
20+
}

‎composer.lock

+292
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎config/config.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[
2+
{
3+
"blog_url" : "http://shibayu36.hatenablog.com/",
4+
"permission" : "hogehoge",
5+
"can_be_edited_by" : [
6+
"shiba_yu36"
7+
]
8+
},
9+
{
10+
"blog_url" : "http://shibayu36-private.hatenablog.com/",
11+
"permission" : "private",
12+
"can_be_edited_by" : [
13+
"shiba_yu36",
14+
"shiba_yu37"
15+
]
16+
},
17+
{
18+
"blog_url" : "http://blog.example.com/",
19+
"permission" : "public",
20+
"can_be_edited_by" : [
21+
"example-user"
22+
]
23+
}
24+
]

‎config/config.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-
2+
blog_url: http://shibayu36.hatenablog.com/
3+
permission: public
4+
can_be_edited_by: [ shiba_yu36 ]
5+
-
6+
blog_url: http://shibayu36-private.hatenablog.com/
7+
permission: private
8+
can_be_edited_by: [ shiba_yu36, shiba_yu37 ]
9+
-
10+
blog_url: http://blog.example.com/
11+
permission: public
12+
can_be_edited_by: [ example-user ]

‎src/BlogConfiguration.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
namespace Example\Config;
3+
4+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
5+
use Symfony\Component\Config\Definition\ConfigurationInterface;
6+
use Respect\Validation\Validator as v;
7+
8+
class BlogConfiguration implements ConfigurationInterface
9+
{
10+
public function getConfigTreeBuilder()
11+
{
12+
$treeBuilder = new TreeBuilder();
13+
$rootNode = $treeBuilder->root('blog');
14+
15+
$rootNode
16+
->children()
17+
->arrayNode('user_blogs')
18+
->isRequired()
19+
->requiresAtLeastOneElement()
20+
->prototype('array')
21+
->children()
22+
->scalarNode('blog_url')
23+
->info('設定したいブログのURL')
24+
->isRequired()
25+
->validate()
26+
->ifTrue(function ($value) {
27+
return !v::call(
28+
'parse_url',
29+
v::arr()->key('scheme', v::startsWith('http'))
30+
->key('host', v::domain())
31+
)->validate($value);
32+
})
33+
->thenInvalid('ブログURLが無効: %s')
34+
->end()
35+
->end()
36+
->enumNode('permission')
37+
->info('publicなら公開、privateなら非公開')
38+
->values(['public', 'private'])
39+
->isRequired()
40+
->end()
41+
->arrayNode('can_be_edited_by')
42+
->info('編集権限を持つユーザ')
43+
->isRequired()
44+
->requiresAtLeastOneElement()
45+
->prototype('scalar')
46+
->end()
47+
->end()
48+
->end()
49+
->end();
50+
51+
return $treeBuilder;
52+
}
53+
}

‎src/JsonFileLoader.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace Example\Config;
3+
4+
use Symfony\Component\Config\Definition\Processor;
5+
use Symfony\Component\Config\Loader\FileLoader;
6+
7+
class JsonFileLoader extends FileLoader
8+
{
9+
public function load($resource, $type = null)
10+
{
11+
$path = $this->locator->locate($resource);
12+
$config = ['blog' => ['user_blogs' => $this->loadFile($path)]];
13+
14+
if (null === $config) {
15+
return;
16+
}
17+
18+
$configuration = new BlogConfiguration();
19+
$processor = new Processor();
20+
$processedConfig = $processor->processConfiguration(
21+
$configuration,
22+
$config
23+
);
24+
25+
return $processedConfig;
26+
}
27+
28+
public function supports($resource, $type = null)
29+
{
30+
return is_string($resource) && 'json' === pathinfo(
31+
$resource,
32+
PATHINFO_EXTENSION
33+
);
34+
}
35+
36+
private function loadFile($path)
37+
{
38+
return json_decode(file_get_contents($path), true);
39+
}
40+
}

‎src/YamlFileLoader.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace Example\Config;
3+
4+
use Symfony\Component\Config\Definition\Processor;
5+
use Symfony\Component\Config\Loader\FileLoader;
6+
use Symfony\Component\Yaml\Yaml;
7+
8+
class YamlFileLoader extends FileLoader
9+
{
10+
public function load($resource, $type = null)
11+
{
12+
$path = $this->locator->locate($resource);
13+
$config = ['blog' => ['user_blogs' => $this->loadFile($path)]];
14+
15+
if (null === $config) {
16+
return;
17+
}
18+
19+
$configuration = new BlogConfiguration();
20+
$processor = new Processor();
21+
$processedConfig = $processor->processConfiguration(
22+
$configuration,
23+
$config
24+
);
25+
26+
return $processedConfig;
27+
}
28+
29+
public function supports($resource, $type = null)
30+
{
31+
return is_string($resource) && 'yml' === pathinfo(
32+
$resource,
33+
PATHINFO_EXTENSION
34+
);
35+
}
36+
37+
private function loadFile($path)
38+
{
39+
return Yaml::parse($path);
40+
}
41+
}

0 commit comments

Comments
 (0)
Please sign in to comment.