ããã«ã¡ã¯ãå¤ã¯ãã£ã±ãçãã¼ã«ã@madapajaã§ãã
PHP5.4+ãã¬ã¼ã ã¯ã¼ã¯ BEAR.Sundayãç解ããããã«Ray.Diã触ã£ã¦ã¿ãã®å·» å
¶ã®å£± : ä»æ¥ãé©å½ãã¤ã¢ãªã¼ ã®ç¶ãã§ãã
PHP5.4+ãã¬ã¼ã ã¯ã¼ã¯ BEAR.Sundayãç解ããããã«Ray.Diã触ã£ã¦ã¿ãã®å·»ã·ãªã¼ãº
ååã¯ãRay ã Composer çµç±ã§ã¤ã³ã¹ãã¼ã«ã㦠User
ã¯ã©ã¹ãä½ãã¾ããã
Ray ã使ããã«æ¸ãã¦ã¿ã
Ray ã使ã£ã¦ã¿ãåã«ãæ®éã« PHP ãæ¸ã㦠User
ã¯ã©ã¹ãå®è¡ãã¦ã¿ããã¨æãã¾ããä»å㯠Madapaja\Ray\Di\Sample01\App
ã¯ã©ã¹ãä¸è¨ã®ããã«æ¸ãã¾ããã
src/Madapaja/Ray/Di/Sample01/App.php
namespace Madapaja\Ray\Di\Sample01;
use Madapaja\Ray\Di\Sample01\Model\User;
class App
{
public function run()
{
$pdo = new \PDO('sqlite::memory:', null, null);
$user = new User($pdo);
$user->init();
$user->createUser('Koriym', mt_rand(18,35));
$user->createUser('Bear', mt_rand(18,35));
$user->createUser('Yoshi', mt_rand(18,35));
$users = $user->readUsers();
print_r($users);
}
}
ããã¦ä¸ã® App
ã¯ã©ã¹ãå®è¡ããããã«ä¸è¨ã®ãã¡ã¤ã«ãä½ãã¾ããå
容ã¯ãªã¼ããã¼ããèªã¿è¾¼ã㧠App::run()
ãå®è¡ããã ãã§ãã
main.php
require __DIR__.'/vendor/autoload.php';
use Madapaja\Ray\Di\Sample01\App;
(new App())->run();
ããã¾ã§åºæ¥ãã php main.php
ã¨ãã¦å®è¡ãã¦ã¿ã¾ãã
Array
(
[0] => Array
(
[Name] => Koriym
[Age] => 24
)
[1] => Array
(
[Name] => Bear
[Age] => 18
)
[2] => Array
(
[Name] => Yoshi
[Age] => 30
)
)
ã®ããã«ãçµæã表示ãããã§ããããï¼ç°¡åã§ãããã¨ããããæ®éã®PHPãªã®ã§å½ããåã§ããã
Ray.Di ã§ã¤ã³ã¸ã§ã¯ããã¦ã¿ãã
ãã£ã¨ãRayã®åºçªã§ããRay.Diã使ã£ã¦ä¾åæ§ã注å
¥ãã¦ã¿ã¾ããä»åã®å ´åã¯ã User
ã¯ã©ã¹ã¸ PDO
ã注å
¥ãã¾ãã
Ray ã§ã¯ DI ã AOP ã®è¨å®ãã¢ã¸ã¥ã¼ã«ã¨ããåä½ï¼ã¯ã©ã¹ï¼ã§è¡ãã¾ããã¢ã¸ã¥ã¼ã«ã¯ã¢ããªã±ã¼ã·ã§ã³ããã¬ã¼ã ã¯ã¼ã¯ãæ§æããéã«ä½¿ãããç¥èãæã£ã¦ããããã®ç¥èããã¨ã«ã¤ã³ã¸ã§ã¯ã¿ã¼ãä¾åã注å
¥ãããã¨ã§ãé©åãªãªãã¸ã§ã¯ãã°ã©ããçæããããã¨ã«ãªãã¾ãã
ã¨ãããããã¢ã¸ã¥ã¼ã«ã§ DI 㨠AOP ã®è¨å®ãããããã¨ãããã¨ãåããã°OKã§ãã
ã¢ããã¼ã·ã§ã³ã®è¿½å
ã¢ã¸ã¥ã¼ã«ãä½ãåã«ãUser
ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã¸ã¤ã³ã¸ã§ã¯ããè¡ããã¨ã示ãããã«ãã¢ããã¼ã·ã§ã³ã追å ãã¾ãã
src/Madapaja/Ray/Di/Sample01/Model/User.php
namespace Madapaja\Ray\Di\Sample01\Model;
use Ray\Di\Di\Inject;
use Ray\Di\Di\Named;
class User
{
private $db;
/**
* @Inject
* @Named("pdo=pdo_user")
*/
public function __construct(\PDO $pdo)
{
$this->db = $pdo;
}
// ...
13è¡ç®ã§ @Inject
ã¢ããã¼ã·ã§ã³ã«ãã£ã¦ã¤ã³ã¸ã§ã¯ããã¤ã³ãã ã¨ãããã¨ã示ãã14è¡ç®ã§ @Named
ã¢ããã¼ã·ã§ã³ã§ pdo_user
ã¨ããååã§ï¼pdo
ãï¼ãã¤ã³ããããã¨ã示ãã¾ãã
ãªãã5ï½6è¡ç®ã¯ @Inject
㨠@Named
ã¢ããã¼ã·ã§ã³ã®åå空éã解決ãããããã« use
ãã¦ãã¾ãã
UserModule ãä½ã
ã¨ãããã¨ã§ãUser
ã¯ã©ã¹ã®æ§æè¨å®ãè¡ã UserModule
ãä½ãã¾ãã ã¢ã¸ã¥ã¼ã«ã¯ Ray\Di\AbstractModule
ãç¶æ¿ã configure()
ã¡ã½ããå
ã§æ§æã®è¨å®ãè¡ãã¾ãã
src/Madapaja/Ray/Di/Sample01/Module/UserModule.php
namespace Madapaja\Ray\Di\Sample01\Module;
use Ray\Di\AbstractModule;
class UserModule extends AbstractModule
{
protected function configure()
{
// bind dependency @Inject @Named("pdo_user")
$pdo = new \PDO('sqlite::memory:', null, null);
$this->bind('PDO')->annotatedWith('pdo_user')->toInstance($pdo);
}
}
DI ã®è¨å®ã¯ 13 è¡ç®ã§è¡ã£ã¦ãããPDO
ã¯ã©ã¹ã® pdo_user
ã¨ããååã®ç®æã« PDO ã¤ã³ã¹ã¿ã³ã¹ï¼$pdo
ï¼ããã¤ã³ããã¦ãã¾ãã ï¼æ¬æ¥ã¯ã¤ã³ã¿ã¼ãã§ã¼ã¹ã§ã¯ãªã PDO ã¯ã©ã¹ãããã®å®ä½ï¼ã¤ã³ã¹ã¿ã³ã¹ï¼ã使ç¨ããªãæ¹ãããã®ã§ãããããã§ã¯ã³ã¼ããç°¡åã«ããããã«è¨±ãã¦ãããã¾ããï¼
ã¤ã³ã¸ã§ã¯ã¿çµç±ã§ã¤ã³ã¹ã¿ã³ã¹ãåå¾ãã
ããã§æºåãæ´ãã¾ããã App
ã¯ã©ã¹ãæ¸ãå¤ãã¦ã¤ã³ã¸ã§ã¯ã¿ãå©ç¨ãã¦ã¿ã¾ãã
src/Madapaja/Ray/Di/Sample01/App.php
namespace Madapaja\Ray\Di\Sample01;
use Ray\Di\Injector;
use Madapaja\Ray\Di\Sample01\Module\UserModule;
class App
{
public function run()
{
$di = Injector::create([new UserModule]);
$user = $di->getInstance('Madapaja\Ray\Di\Sample01\Model\User');
$user->init();
// ...
12è¡ç®ã§ãä»ä½ã£ã UserModule
ãæå®ãã¦ã¤ã³ã¸ã§ã¯ã¿ãçæãã13è¡ç®ã§å®éã« User
ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ãã¦ãã¾ãã ããã§ãä¾åããããªã«è§£æ±ºãããããã«ãªãã¾ããã
ã¢ããã¼ã·ã§ã³ã®ãªã¼ããã¼ããè¨å®
ãã£ããå®è¡ãã¦ã¿ããã¨ããã§ãããã¢ããã¼ã·ã§ã³ã®ãªã¼ããã¼ãã®ããã«ããã²ã¨ã¤å¦çã追å ããå¿ è¦ãããã¾ãã ãã㯠BEAR.Sunday ã§ã¯ãã¬ã¼ã ã¯ã¼ã¯å´ã§ãã£ã¦ããã¾ãã®ã§ãä»åã®ããã« Ray ãã¹ã¿ã³ãã¢ãã³ã§å©ç¨ããå ´åã«ã®ã¿å¿ è¦ãªè¨å®ã§ãã
main.php ã§ãªã¼ããã¼ãã¼ãåå¾ãã¦ãã¢ããã¼ã·ã§ã³ã¬ã¸ã¹ããªã«ãã¼ãã¼ãç»é²ãã¾ãã å®éã®ã½ã¼ã¹ã¯ä¸è¨ã®ããã«ãªãã¾ãã
main.php
use Doctrine\Common\Annotations\AnnotationRegistry;
use Madapaja\Ray\Di\Sample01\App;
$loader = require __DIR__.'/vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, "loadClass"));
(new App())->run();
ããã§ãã£ã¨å®è¡ã§ããããã«ãªãã¾ãããphp main.php
ãå®è¡ããã¦åé¡ãªãåãã®ã確èªãã¦ã¿ã¾ãããã
@PostConstruct ã¢ããã¼ã·ã§ã³ã«ããåæåã¡ã½ããã®å®ç¾©
ããã¾ã§User
ã¯ã©ã¹ãåæåããããã«ã App
ã¯ã©ã¹ã§ User::init()
ã¡ã½ãããå¼ã³åºãã¦ãã¾ããã ããã @PostConstruct
ã¢ããã¼ã·ã§ã³ãããã¨ã§ãã¤ã³ã¸ã§ã¯ã¿ã«åæåã¡ã½ããã®å¼ã³åºããä»»ãããã¨æãã¾ãã
src/Madapaja/Ray/Di/Sample01/App.php
// ...
class App
{
public function run()
{
$di = Injector::create([new UserModule]);
$user = $di->getInstance('Madapaja\Ray\Di\Sample01\Model\User');
// $user->init();
// ...
App.phpã®ä¿®æ£ã¯ã12è¡ç®ãåé¤ããã ãã§ãã
次㫠User
ã¯ã©ã¹ã«ã¢ããã¼ã·ã§ã³ã追å ãã¾ãã
src/Madapaja/Ray/Di/Sample01/Model/User.php
namespace Madapaja\Ray\Di\Sample01\Model;
use Ray\Di\Di\Inject;
use Ray\Di\Di\Named;
use Ray\Di\Di\PostConstruct;
class User
{
// ...
/**
* @PostConstruct
*/
public function init()
{
return $this->db->query('CREATE TABLE User (Id INTEGER PRIMARY KEY, Name TEXT, Age INTEGER)');
}
// ...
åå空éã use
ãã¦ã@PostConstruct
ã¢ããã¼ã·ã§ã³ã追å ããã ãã§ãã ããã ãã§ãã¤ã³ã¸ã§ã¯ã¿ã® getInstance
ã¡ã½ããã§ã¤ã³ã¹ã¿ã³ã¹ãåå¾ããéãã³ã³ã¹ãã©ã¯ã¿å¾ã«ã¢ããã¼ã·ã§ã³ããã¡ã½ãããå¼ã³åºãããããã«ãªãã¾ãã
ãããã Ray.Aop ã®ç»æ¿ï¼
ã¨ããæãªã®ã§ãããã¾ãããé·ããªã£ã¦ããï¼ã¨ãããç²ããï¼ã®ã§ãå ¶ã®åã«ç¶ãã¾ãï¼ã«ãã«ãï¼
ï¼7/12追è¨ï¼ç¶ãæ¸ãã¾ããï¼
åèãªã³ã¯
PHP5.4+ãã¬ã¼ã ã¯ã¼ã¯ BEAR.Sundayãç解ããããã«Ray.Diã触ã£ã¦ã¿ãã®å·»ã·ãªã¼ãº