ããã°ãã¯ãããããå¥åº·è¨ºæãªã®ã§ãã¤ã¨ããããããã¨æã£ã¦ã@madapajaã§ãã
ç¹ã«Symfonyçéãªã©ã§è©±é¡ã«ãªã£ã¦ãããPHP5.4+åãã«éçºãè¡ããã¦ãããªã½ã¼ã¹æåãã¬ã¼ã ã¯ã¼ã¯BEAR.Sundayã¯çãããã§ãã¯ããã¦ã¾ããï¼
ãã®ãã¬ã¼ã ã¯ã¼ã¯ã¯PHP5.2+åãã®ãªã½ã¼ã¹æåãã¬ã¼ã ã¯ã¼ã¯BEAR.Saturdayã®æ°ãã¼ã¸ã§ã³ã«ããããã®ã§ãè¨è¨ã®å ¨ã¦ãè¦ç´ãã¦@koriymæ°ã«ãã£ã¦éçºãé²ãããã¦ãã¾ãã
ãªã½ã¼ã¹æåããéçºææ³ãªã©ã«ã¤ãã¦ã¯ã¨ã£ã¦ãããã§ã¯æ¸ãåããªãã®ã§ BEAR Blogãªã©ããã§ãã¯ãã¦ããã ããã°ã¨ã
ãã¦ããããªBEAR.Sundayã§ãããã¾ã éçºä¸ã®ãã¬ã¼ã ã¯ã¼ã¯ã§ãã®ã§å®éã«ä½¿ãä¸ã§è¶³ããªãæ©è½ãããããããã¾ãããã¢ããªã±ã¼ã·ã§ã³ã³ã³ããã¼ã©ãªã©ãæ©è½è¿½å ãä»æ§å¤æ´ãããããããããè¡ããããã¨ã«ãªãããã§ãã
ã ãã©ãBear.Sunday Meetup #0 ãéå¬ãããããã ã¨ããæ å ±ãåºåã£ã¦ãããã®ã§ããããã ãã§ã触ã£ã¦ã¿ããï¼
ã¨ãããã¨ã§ãä»å¾ãä»æ§ã®å¤æ´ã¯ã»ã¨ãã©èããããªã Ray.Di 㨠Ray.Aopã使ã£ã¦ã¿ãããã¨ããä¼ç»ã§ãã
Rayã£ã¦ä½?
Rayã¯ãGoogle Guiceã¨ããJavaãã¬ã¼ã ã¯ã¼ã¯ã®PHPçã§ãBEAR.Sundayã¨åãã@koriymæ°ã«ãã£ã¦éçºããã¦ãã¾ãã
Rayã¯2ã¤ã®ããã±ã¼ã¸ãä¸ã¤ã¯DIï¼Dependency Injectionï¼ãè¡ãããã®Ray.Diãããä¸ã¤ã¯ã¢ã¹ãã¯ããæ§æããããã®ãã¬ã¼ã ã¯ã¼ã¯Ray.Aopããæ§æããã¦ãã¾ãã
ä»åã¯Ray.Aopã«ãããµã³ãã«ãå ã«Rayã§ã©ã®ããã«ã¢ããªã±ã¼ã·ã§ã³ãæ§æã§ããã®ããè¦ã¦ããããã¨æãã¾ãã
ãªããä»åã®ãµã³ãã«ã®å®æçã¯ä¸è¨ã«ããã¦ããã¾ãã®ã§åèã«ã©ããã
ãªãããã®ãã¥ã¼ããªã¢ã«ï¼ï¼ï¼ã§ã¯GitãPHP5.4ãPDO SQLiteæ¡å¼µãå¿ è¦ã§ãã
RayãComposerã§ã¤ã³ã¹ãã¼ã«
Ray/Diã¨Ray/Aopãrequireã«æå®ãã¦composer install ããã°OKï¼ã¨æã£ãã®ã§ãããããã¯åå±ãå¸ãã¦ããã¾ããã§ããã
試è¡é¯èª¤ã®çµæãä¸è¨ã®å 容ã§ã¤ã³ã¹ãã¼ã«ã§ããã®ã§å®éã«ãã£ã¦ã¿ãæ¹ã¯ã³ããããã®ãåãã¨ã
ã¤ãã§ã«ãèªåã®æ¸ãã½ã¼ã¹ã®autoloadè¨å®ã追å ãã¦ããã¾ãã
ç§ã¯åå空éã¯"Madapaja\Ray\Di\Sample01"ã¨ãã¦srcãã£ã¬ã¯ããªé
ä¸ã«ç½®ããã¨ã«ãã¾ããã
composer.json
{
"require": {
"php": ">=5.4.0",
"ext-pdo": "*",
"ext-pdo_sqlite": "*",
"Ray/Di": ">=1.0.x-dev",
"doctrine/common": "2.3.x-dev",
"Ray/Aop": ">=1.0.x-dev",
"Aura/Di": ">=1.0.x-dev"
},
"autoload": {
"psr-0": {
"Aura\\Di": "vendor/Aura/Di/src/",
"Madapaja\\Ray\\Di\\Sample01": "src"
}
}
}
ä¸è¨ãã¡ã¤ã«ãåºæ¥ãã
curl -s http://getcomposer.org/installer | php
php composer.phar install
ã¨ãã¦ãã©ã¤ãã©ãªãã¤ã³ã¹ãã¼ã«ãã¾ãããã
å®è¡ãã¦ãããããã¼ã£ã¨æåãåºã¦ãã¦æå¾ã«ãGenerating autoload filesãã¨è¡¨ç¤ºãããã°OKã
ã§ã¯ãå®éã«ã½ã¼ã¹ãæ¸ãå§ãã¾ãã
æ¸ãå§ãã
ãã£ãããRayã使ãããã¨ããã§ããããã£ã¨ææ ¢ãã¦æ®éã®ã¢ããªã±ã¼ã·ã§ã³ã®éçºã¨åããã¢ãã«ãæ¸ãã¨ããããå§ãã¾ãã
Userã¯ã©ã¹ãä¸è¨ã®ããã«ä½ãã¾ããã
src/Madapaja/Ray/Di/Sample01/Model/User.php
namespace Madapaja\Ray\Di\Sample01\Model;
class User
{
private $db;
public function __construct(\PDO $pdo)
{
$this->db = $pdo;
}
public function init()
{
return $this->db->query('CREATE TABLE User (Id INTEGER PRIMARY KEY, Name TEXT, Age INTEGER)');
}
public function createUser($name, $age)
{
$sth = $this->db->prepare('INSERT INTO User (Name, Age) VALUES (:name, :age)');
return $sth->execute(array(':name' => $name, ':age' => $age));
}
public function readUsers()
{
$sth = $this->db->query('SELECT Name, Age FROM User');
$result = $sth->fetchAll(\PDO::FETCH_ASSOC);
return $result;
}
}
ãªãã¦ãã¨ã¯ãªãDBã®Userãã¼ãã«ãæä½ããæ®éã®ã¯ã©ã¹ï¼Modelï¼ã§ããå¼·ãã¦è¨ãã°ãä¾åé¢ä¿ã¯èªåã§è§£æ±ºããã«ãDIã«ãã£ã¦å¾ã§è¯ãã«è¨ãã£ã¦ããããããã«ãã³ã³ã¹ãã©ã¯ã¿ã§PDOã¤ã³ã¹ã¿ã³ã¹ãåãåãå½¢ã«ãããããã§ãã
ãã¡ãããã®æ®µéã§ãã®ã¯ã©ã¹ã«å¯¾ãã¦ãã¹ããæ¸ãã®ãç°¡åã§ãããä»åã¯ãããªæãã§æ¸ãã¦ã¿ã¾ããã
ããããRayã®åºçªã§ã
ã¨ããæãªã®ã§ãããäºæ³ä»¥ä¸ã«é·ããªã£ã¦ããï¼ã¨ãããç²ããï¼ã®ã§ãå ¶ã®å¼ã«ç¶ãã¾ãï¼
å®éã®Rayã触ãã®ã¯æ¬¡åã«ãªã£ã¦ãã¾ãã¾ããããå¾ ã¡ãããªããã俺ã¯ä»ãããããã ããã¨ããæ¹ã¯madapaja/ray-di-sample ã®ã³ãããå±¥æ´ãªãããåèã«ãªãããããã¾ããã
ï¼7/11追è¨ï¼å ¶ã®å¼æ¸ãã¾ããï¼
åèãªã³ã¯
PHP5.4+ãã¬ã¼ã ã¯ã¼ã¯ BEAR.Sundayãç解ããããã«Ray.Diã触ã£ã¦ã¿ãã®å·»ã·ãªã¼ãº