Created
October 27, 2011 23:43
-
-
Save k-holy/1321216 to your computer and use it in GitHub Desktop.
Silex RedBeanServiceProvider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE profiles | |
( | |
id INTEGER NOT NULL PRIMARY KEY, | |
name VARCHAR(20) NOT NULL, | |
notes TEXT | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE tree_nodes | |
( | |
id INTEGER NOT NULL PRIMARY KEY, | |
profile_id INTEGER NOT NULL, | |
path VARCHAR(255) NOT NULL, | |
order_no REAL NOT NULL, | |
FOREIGN KEY(profile_id) REFERENCES profiles(id) | |
); | |
CREATE INDEX tree_nodes_idx_path ON tree_nodes (path); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PHP versions 5 | |
* | |
* @copyright 2011 k-holy <[email protected]> | |
* @author [email protected] | |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) | |
*/ | |
require_once realpath(__DIR__ . '/silex.phar'); | |
use Silex\Application; | |
use Holy\Silex\Provider\RedBeanServiceProvider; | |
$app = new Application(); | |
$app['debug'] = true; | |
$app['autoloader']->registerNamespace('Holy', | |
realpath(__DIR__ . '/vendor/k-holy/src')); | |
$app->register(new RedBeanServiceProvider(), array( | |
'db.redbean.class_path' => realpath(__DIR__ . '/vendor/redbean'), | |
'db.options' => array( | |
'dsn' => sprintf('sqlite:%s', realpath(__DIR__ . '/app/data/profiles.sqlite')), | |
), | |
)); | |
$app['db']->debug(true); | |
$app->get('/', function() use ($app) { | |
return sprintf('<pre>%s</pre>', | |
print_r($app['db']->find('profiles', ' 1 ORDER BY id'), true)); | |
/** | |
SELECT * FROM `profiles` WHERE 1 ORDER BY idArray ( ) | |
resultset: 24 rows | |
Array | |
( | |
[1] => RedBean_OODBBean Object | |
( | |
[null:RedBean_OODBBean:private] => | |
[properties:RedBean_OODBBean:private] => Array | |
( | |
[id] => 1 | |
[name] => 三好長之 | |
[notes] => | |
) | |
[__info:RedBean_OODBBean:private] => Array | |
( | |
[type] => profiles | |
[sys.idfield] => id | |
[tainted] => | |
) | |
[beanHelper:RedBean_OODBBean:private] => RedBean_BeanHelperFacade Object | |
( | |
) | |
) | |
... | |
**/ | |
}); | |
$app->get('/{id}', function($id) use ($app) { | |
return sprintf('<pre>%s</pre>', | |
print_r($app['db']->load('profiles', $id), true)); | |
/** | |
SELECT * FROM `profiles` WHERE ( `id` IN ( ?) ) Array ( [0] => 1 ) | |
resultset: 1 rows | |
RedBean_OODBBean Object | |
( | |
[null:RedBean_OODBBean:private] => | |
[properties:RedBean_OODBBean:private] => Array | |
( | |
[id] => 1 | |
[name] => 三好長之 | |
[notes] => | |
) | |
[__info:RedBean_OODBBean:private] => Array | |
( | |
[type] => profiles | |
[sys.idfield] => id | |
[tainted] => | |
) | |
[beanHelper:RedBean_OODBBean:private] => RedBean_BeanHelperFacade Object | |
( | |
) | |
) | |
**/ | |
}); | |
$app->run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSERT INTO profiles (id, name) VALUES ( 1, '三好長之'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES ( 1, 1, '/1/' , 1); | |
INSERT INTO profiles (id, name) VALUES ( 2, '三好之長'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES ( 2, 2, '/1/2/', 1); | |
INSERT INTO profiles (id, name) VALUES ( 3, '三好勝時'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES ( 3, 3, '/1/3/', 2); | |
INSERT INTO profiles (id, name) VALUES ( 4, '三好一秀'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES ( 4, 4, '/1/4/', 3); | |
INSERT INTO profiles (id, name) VALUES ( 5, '三好勝宗'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES ( 5, 5, '/1/5/', 4); | |
INSERT INTO profiles (id, name) VALUES ( 6, '三好長秀'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES ( 6, 6, '/1/2/6/', 1); | |
INSERT INTO profiles (id, name) VALUES ( 7, '三好頼澄'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES ( 7, 7, '/1/2/7/', 2); | |
INSERT INTO profiles (id, name) VALUES ( 8, '芥川長光'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES ( 8, 8, '/1/2/8/', 3); | |
INSERT INTO profiles (id, name) VALUES ( 9, '三好長則'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES ( 9, 9, '/1/2/9/', 4); | |
INSERT INTO profiles (id, name) VALUES (10, '三好新五郎'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (10,10, '/1/3/10/', 1); | |
INSERT INTO profiles (id, name) VALUES (11, '三好勝長'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (11,11, '/1/3/11/', 2); | |
INSERT INTO profiles (id, name) VALUES (12, '三好政長'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (12,12, '/1/3/12/', 3); | |
INSERT INTO profiles (id, name) VALUES (13, '三好元長'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (13,13, '/1/2/6/13/', 1); | |
INSERT INTO profiles (id, name) VALUES (14, '三好康長'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (14,14, '/1/2/6/14/', 2); | |
INSERT INTO profiles (id, name) VALUES (15, '三好政成'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (15,15, '/1/2/7/15/', 1); | |
INSERT INTO profiles (id, name) VALUES (16, '三好政康'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (16,16, '/1/2/7/16/', 2); | |
INSERT INTO profiles (id, name) VALUES (17, '芥川孫十郎'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (17,17, '/1/2/8/17/', 1); | |
INSERT INTO profiles (id, name) VALUES (18, '三好長逸'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (18,18, '/1/2/9/18/', 1); | |
INSERT INTO profiles (id, name) VALUES (19, '三好政勝'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (19,19, '/1/3/12/19/', 1); | |
INSERT INTO profiles (id, name) VALUES (20, '三好長慶'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (20,20, '/1/2/6/13/20/', 1); | |
INSERT INTO profiles (id, name) VALUES (21, '三好義賢'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (21,21, '/1/2/6/13/21/', 2); | |
INSERT INTO profiles (id, name) VALUES (22, '安宅冬康'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (22,22, '/1/2/6/13/22/', 3); | |
INSERT INTO profiles (id, name) VALUES (23, '十河一存'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (23,23, '/1/2/6/13/23/', 4); | |
INSERT INTO profiles (id, name) VALUES (24, '三好可政'); | |
INSERT INTO tree_nodes (id, profile_id, path, order_no) VALUES (24,24, '/1/3/12/19/24/', 1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PHP versions 5 | |
* | |
* @copyright 2011 k-holy <[email protected]> | |
* @author [email protected] | |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) | |
*/ | |
namespace Holy\Silex\Provider; | |
use Silex\Application; | |
use Silex\ServiceProviderInterface; | |
/** | |
* Holy\Silex\Provider\RedBeanServiceProvider | |
* | |
* @author [email protected] | |
*/ | |
class RedBeanServiceProvider implements ServiceProviderInterface | |
{ | |
public function register(Application $app) | |
{ | |
$app['db'] = $app->share(function() use ($app) { | |
if (isset($app['db.redbean.class_path'])) { | |
$redbean_path = $app['db.redbean.class_path'] . DIRECTORY_SEPARATOR . 'rb.php'; | |
if ('\\' === DIRECTORY_SEPARATOR) { | |
$redbean_path = str_replace('\\', '/', $redbean_path); | |
} | |
include_once $redbean_path; | |
} | |
$default_options = array( | |
'dsn' => null, | |
'username' => null, | |
'password' => null, | |
'frozen' => false, | |
); | |
$databases = \R::setupMultiple(array('default' => (isset($app['db.options'])) | |
? array_replace($default_options, $app['db.options']) : array())); | |
return $databases['default']; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
複数データベースには未対応。
RedBeanへの引数の渡し方は分かるけど、Silex\Application(というかPimple)がArrayAccessなのでforeach() array_replace()で動的にデフォルトオプションをマージできない…。