Skip to content

Commit

Permalink
Merge pull request #175 from bearsunday/cache
Browse files Browse the repository at this point in the history
bootstrap application cache

bearsunday/BEAR.Sunday#59
  • Loading branch information
koriym committed Feb 26, 2015
2 parents 25dd2b8 + e36cd5d commit 9fb49cd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,56 @@

use BEAR\AppMeta\AbstractAppMeta;
use BEAR\Sunday\Extension\Application\AppInterface;
use Doctrine\Common\Cache\ApcCache;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\FilesystemCache;
use Ray\Di\AbstractModule;
use Ray\Di\Injector;

final class Bootstrap
{
const PACKAGE_MODULE_PATH = 'BEAR\Package\Context\\';

/**
* @param AbstractAppMeta $appMeta
* @param string $contexts
* @param Cache $cache
*
* @return AppInterface
*/
public function newApp(AbstractAppMeta $appMeta, $contexts, Cache $cache)
public function newApp(AbstractAppMeta $appMeta, $contexts, Cache $cache = null)
{
if (is_null($cache)) {
$cache = function_exists('apc_fetch') ? new ApcCache : new FilesystemCache($appMeta->tmpDir);
$cache->setNamespace(filemtime($appMeta->appDir . '/src/.'));
}
$app = $cache->fetch($contexts);
if ($app) {
return $app;
}
$contextsArray = array_reverse(explode('-', $contexts));
$app = $this->createAppInstance($appMeta ,$contexts);
$cache->save($contexts, $app);

return $app;
}

/**
* @param AbstractAppMeta $appMeta
* @param string $contexts
*
* @return AppInterface
*/
private function createAppInstance(AbstractAppMeta $appMeta ,$contexts)
{
$contextsArray = array_reverse(explode('-' ,$contexts));
$module = null;
foreach ($contextsArray as $context) {
$class = $appMeta->name . '\Module\\' . ucwords($context) . 'Module';
if (! class_exists($class)) {
$class = self::PACKAGE_MODULE_PATH . ucwords($context) . 'Module';
$class = 'BEAR\Package\Context\\' . ucwords($context) . 'Module';
}
$module = new $class($module);
/** @var $module AbstractModule */
$module = new $class($module);
}
$app = (new Injector($module, $appMeta->tmpDir))->getInstance(AppInterface::class);
$cache->save($contexts, $app);
$app = (new Injector($module ,$appMeta->tmpDir))->getInstance(AppInterface::class);

return $app;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BEAR\AppMeta\AppMeta;
use BEAR\Sunday\Extension\Application\AppInterface;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\FilesystemCache;
use FakeVendor\HelloWorld\Module\AppModule;

class BootstrapTest extends \PHPUnit_Framework_TestCase
Expand All @@ -27,4 +28,17 @@ public function testBuiltInCliModule()
$app = (new Bootstrap)->newApp(new AppMeta('FakeVendor\HelloWorld'), 'cli-app', new ArrayCache);
$this->assertInstanceOf(AppInterface::class, $app);
}

public function testContextCacheModule()
{
$app = (new Bootstrap)->newApp(new AppMeta('FakeVendor\HelloWorld'), 'app');
$this->assertInstanceOf(AppInterface::class, $app);
}

public function testCache()
{
$app1 = (new Bootstrap)->newApp(new AppMeta('FakeVendor\HelloWorld'), 'cli-app', new FilesystemCache(__DIR__ . '/tmp'));
$app2 = (new Bootstrap)->newApp(new AppMeta('FakeVendor\HelloWorld'), 'cli-app', new FilesystemCache(__DIR__ . '/tmp'));
$this->assertSame(serialize($app1), serialize($app2));
}
}

0 comments on commit 9fb49cd

Please sign in to comment.