private function getConnection() { $dir = __DIR__; $config = "driver=pdo_sqlite&path={$dir}/../var/db/todo.sqlite3"; $injector = new Injector(new DbalModule($config)); return $injector->getInstance(Connection::class); }
protected function setUp() { parent::setUp(); $injector = Injector::create([new TestModule()], false); $app = $injector->getInstance('BEAR\\Sunday\\Application\\Context'); $this->resource = $app->resource; }
protected function setUp() { static $app; parent::setUp(); if (!$app) { $injector = Injector::create([new AppModule()]); $app = $injector->getInstance('BEAR\\Sunday\\Application\\Context'); } $this->resource = $app->resource; }
/** * Return Injector instance * * @param array $modules * @param Cache $cache * @param string $tmpDir * * @return Injector */ public function newInstance(array $modules = [], Cache $cache = null, $tmpDir = null) { (new AopClassLoader())->register($tmpDir); $annotationReader = (new Locator())->getAnnotationReader(); $config = $this->config ?: new Config(new Annotation(new Definition(), $annotationReader)); $logger = $this->logger ?: new Logger(); $tmpDir = $tmpDir ?: sys_get_temp_dir(); $injector = new Injector(new Container(new Forge($config)), new EmptyModule(), new Bind(), new Compiler($tmpDir, new PHPParser_PrettyPrinter_Default()), $logger); if (count($modules) > 0) { $module = array_shift($modules); if ($cache instanceof Cache && $module instanceof ModuleCacheModule) { $module = $module->get($cache); } foreach ($modules as $extraModule) { /* @var $module AbstractModule */ $module->install($extraModule); } $injector->setModule($module); } return $injector; }
<?php /** * Sandbox * * @package App.Sandbox */ use Helloworld\Module\AppModule; use Ray\Di\Injector; require_once __DIR__ . '/load.php'; $app = apc_fetch('app-helloworld'); if ($app) { return $app; } $injector = Injector::create([new AppModule()]); $app = $injector->getInstance('BEAR\\Sunday\\Application\\Context'); apc_store('app-helloworld', $app); return $app;
/** * @test */ public function test_rollbacking_nested_transaction() { $dir = __DIR__; $config = "driver=pdo_sqlite&path={$dir}/../var/db/todo.sqlite3"; $injector = new Injector(new TestModule($config)); $annotation = new Transactional(); $annotation->txType = TransactionScope::REQUIRES_NEW; $trans = new DbalTransaction($injector->getInstance(Connection::class), $annotation); $scope = new TransactionScope($trans, $annotation); $model = $injector->getInstance(TodoModel::class); $scope->runInto(function () use($model) { $this->assertTrue($model->select(999) === false); $this->assertTrue($model->select(888) === false); try { $model->insertRecordAllowingNestedTransaction(999, 'test memo'); $model->insertFailAllowingNestedTransaction(888, 'failed', function ($m) { $row = $m->select(888); $this->assertFalse($row === false); $this->assertEquals(888, $row['id']); $this->assertEquals('failed', $row['todo']); }); } catch (\LogicException $ex) { } $row = $model->select(999); $this->assertFalse($row === false); $this->assertEquals(999, $row['id']); $this->assertEquals('test memo', $row['todo']); // For latter transaction, Rollback is not executed. $this->assertTrue($model->select(888) === false); }); $row = $model->select(999); $this->assertFalse($row === false); $this->assertEquals(999, $row['id']); $this->assertEquals('test memo', $row['todo']); // For latter transaction, Rollback is not executed. $this->assertTrue($model->select(888) === false); }
use Ray\Di\ApcConfig; use Ray\Di\Annotation; use Ray\Di\Definition; use Doctrine\Common\Annotations\AnnotationReader; use BEAR\Package\Exception\InvalidMode; // init umask(0); // mode $mode = isset($mode) ? $mode : 'Prod'; // cached application ? $cacheKey = __NAMESPACE__ . PHP_SAPI . $mode; // load require_once __DIR__ . '/load.php'; $app = apc_fetch($cacheKey); if ($app) { return $app; } $moduleName = __NAMESPACE__ . '\\Module\\' . $mode . 'Module'; if (!class_exists($moduleName)) { throw new InvalidMode("Invalid mode [{$mode}], check [{$moduleName}]"); } // create application object $injector = new Injector(new Container(new Forge(new ApcConfig(new Annotation(new Definition(), new AnnotationReader())))), new $moduleName()); $app = $injector->getInstance('BEAR\\Sunday\\Application\\Context'); // log binding info $logFile = dirname(__DIR__) . "/data/log/module.{$cacheKey}.log"; file_put_contents($logFile, (string) $injector); // store apc_store($cacheKey, $app); apc_store($cacheKey . '-injector', $injector); return $app;
/** * @param $apps * @param $context * @param $tmpDir * * @return \BEAR\Sunday\Extension\Application\AppInterface */ public static function getApp($apps, $context, $tmpDir) { $injector = Injector::create([new AppModule($context, $apps)]); $app = $injector->getInstance('BEAR\\Sunday\\Extension\\Application\\AppInterface'); return $app; }
<?php use BEAR\Package\Dev\Dev; use Ray\Di\Injector; use My\Hello\Module\AppModule; error_reporting(E_ALL); ini_set('xdebug.max_nesting_level', 300); // load $loader = (require dirname(__DIR__) . '/vendor/autoload.php'); /** @var $loader \Composer\Autoload\ClassLoader */ $loader->addPsr4('My\\Hello\\', __DIR__); // set the application path into the globals so we can access it in the tests. $_ENV['APP_DIR'] = dirname(__DIR__); // clear cache files require $_ENV['APP_DIR'] . '/bin/clear.php'; // set the resource client $GLOBALS['RESOURCE'] = Injector::create([new AppModule('test')])->getInstance('\\BEAR\\Resource\\ResourceInterface');
<?php require 'vendor/autoload.php'; date_default_timezone_set('Europe/Amsterdam'); # Include the task/task library and your dependencies. use PhotoOrganize\Application\DefaultModule; use PhotoOrganize\Application\SymlinkUseCase; use Ray\Di\Injector; use Rx\Observable; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Task\Project; $defaultModule = new DefaultModule(); $injector = new Injector($defaultModule); $project = new Project('photo_organize'); /** * @param OutputInterface $output * @return \Rx\Observer\CallbackObserver */ function outputObserver(OutputInterface $output) { return new Rx\Observer\CallbackObserver(function ($value) use($output) { $time = date("Y-m-d H:i:s"); $output->writeln("{$time} | {$value}"); }, function (Exception $error) use($output) { $output->writeln(date("Y-m-d H:i:s") . " | Exception: " . $error->getMessage()); }, function () use($output) { $output->writeln(date("Y-m-d H:i:s") . " | Complete!"); }); } $project->addTask('symlink', function () use($injector) {
/** * @param callable $moduleProvider * @param Cache $cache * @param string $cacheKey * @param string $tmpDir * * @return Injector */ public static function create(callable $moduleProvider, Cache $cache, $cacheKey, $tmpDir) { $module = new ModuleCacheModule($moduleProvider, $cacheKey, $tmpDir); $injector = Injector::create([$module], $cache, $tmpDir)->enableBindCache(); return $injector; }
public function testBindApp() { $actual = $this->injector->getInstance('BEAR\\Sunday\\Extension\\Application\\AppInterface'); $this->assertInstanceOf('My\\Hello\\App', $actual); }