Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bootstrap application cache #175

Merged
merged 3 commits into from
Feb 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}