Refactor Appbox

This commit is contained in:
Romain Neutron
2012-02-21 16:11:38 +01:00
parent e217a49f6a
commit a21d830f61
3 changed files with 113 additions and 105 deletions

View File

@@ -42,11 +42,11 @@ class Core extends \Pimple
{ {
protected static $availableLanguages = array( protected static $availableLanguages = array(
'ar_SA' => 'العربية' 'ar_SA' => 'العربية'
, 'de_DE' => 'Deutsch' , 'de_DE' => 'Deutsch'
, 'en_GB' => 'English' , 'en_GB' => 'English'
, 'es_ES' => 'Español' , 'es_ES' => 'Español'
, 'fr_FR' => 'Français' , 'fr_FR' => 'Français'
); );
/** /**
@@ -58,12 +58,14 @@ class Core extends \Pimple
public function __construct($environement = null) public function __construct($environement = null)
{ {
$appConf = new Core\Configuration\Application(); $appConf = new Core\Configuration\Application();
$parser = new Core\Configuration\Parser\Yaml(); $parser = new Core\Configuration\Parser\Yaml();
$handler = new Core\Configuration\Handler($appConf, $parser); $handler = new Core\Configuration\Handler($appConf, $parser);
$this->configuration = new Core\Configuration($handler, $environement); $this->configuration = new Core\Configuration($handler, $environement);
$core = $this;
/** /**
* Cache Autoload if it's not debug mode * Cache Autoload if it's not debug mode
*/ */
@@ -75,16 +77,16 @@ class Core extends \Pimple
* Set version * Set version
*/ */
$this['Version'] = $this->share(function() $this['Version'] = $this->share(function()
{ {
return new Core\Version(); return new Core\Version();
}); });
if ($this->configuration->isInstalled()) if ($this->configuration->isInstalled())
{ {
$this['Registry'] = $this->share(function() $this['Registry'] = $this->share(function() use ($core)
{ {
return \registry::get_instance(); return \registry::get_instance($core);
}); });
\phrasea::start(); \phrasea::start();
@@ -94,58 +96,75 @@ class Core extends \Pimple
{ {
$this['Registry'] = $this->share(function() $this['Registry'] = $this->share(function()
{ {
return new \Setup_Registry(); return new \Setup_Registry();
}); });
} }
$core = $this; $this['CacheService'] = $this->share(function() use ($core)
$cacheManager = $this->getCacheManager(); {
if (!file_exists(__DIR__ . '/../../../tmp/cache_registry.yml'))
{
touch(__DIR__ . '/../../../tmp/cache_registry.yml');
}
$file = new \SplFileObject(__DIR__ . '/../../../tmp/cache_registry.yml');
$parser = new Core\Configuration\Parser\Yaml();
return new \Alchemy\Phrasea\Cache\Manager($core, $file, $parser);
});
/** /**
* Set Entity Manager using configuration * Set Entity Manager using configuration
*/ */
$this['EM'] = $this->share(function() use ($core, $cacheManager) $this['EM'] = $this->share(function() use ($core)
{ {
$serviceName = $core->getConfiguration()->getOrm(); $serviceName = $core->getConfiguration()->getOrm();
$configuration = $core->getConfiguration()->getService($serviceName); $configuration = $core->getConfiguration()->getService($serviceName);
$serviceBuilder = new Core\ServiceBuilder\Orm($serviceName, $configuration); $Service = Core\Service\Builder::create($core, $serviceName, $configuration);
$service = $serviceBuilder->buildService(); return $Service->getDriver();
});
foreach ($service->getCacheServices()->all() as $cacheKey => $cacheService)
{
$cacheManager($cacheKey, $cacheService);
}
return $service->getService(); $this['Cache'] = $this->share(function() use ($core)
}); {
$serviceName = $core->getConfiguration()->getCache();
return $core['CacheService']->get('MainCache', $serviceName)->getDriver();
});
$this['OpcodeCache'] = $this->share(function() use ($core)
{
$serviceName = $core->getConfiguration()->getOpcodeCache();
return $core['CacheService']->get('OpcodeCache', $serviceName)->getDriver();
});
$this["Twig"] = $this->share(function() use ($core) $this["Twig"] = $this->share(function() use ($core)
{ {
$serviceName = $core->getConfiguration()->getTemplating(); $serviceName = $core->getConfiguration()->getTemplating();
$configuration = $core->getConfiguration()->getService($serviceName);
$configuration = $core->getConfiguration()->getService($serviceName); $Service = Core\Service\Builder::create($core, $serviceName, $configuration);
$serviceBuilder = new Core\ServiceBuilder\TemplateEngine( return $Service->getDriver();
$serviceName, $configuration });
);
return $serviceBuilder->buildService()->getService();
});
$this['Serializer'] = $this->share(function() $this['Serializer'] = $this->share(function()
{ {
$encoders = array( $encoders = array(
'json' => new Serializer\Encoder\JsonEncoder() 'json' => new Serializer\Encoder\JsonEncoder()
); );
return new Serializer\Serializer(array(), $encoders); return new Serializer\Serializer(array(), $encoders);
}); });
self::initPHPConf(); self::initPHPConf();
@@ -186,32 +205,6 @@ class Core extends \Pimple
} }
} }
private function getCacheManager()
{
return function($cacheKey, $service)
{
$driverType = $service->getType();
$driver = $service->getService();
if(!file_exists(__DIR__ . '/../../../tmp/cache_registry.yml'))
{
touch(__DIR__ . '/../../../tmp/cache_registry.yml');
}
$file = new \SplFileObject(__DIR__ . '/../../../tmp/cache_registry.yml');
$parser = new Core\Configuration\Parser\Yaml();
$cacheManager = new \Alchemy\Phrasea\Cache\Manager($file, $parser);
if ($cacheManager->hasChange($cacheKey, $driverType))
{
$driver->flushAll();
$cacheManager->save($cacheKey, $driverType);
}
};
}
/** /**
* Getter * Getter
* *
@@ -222,16 +215,16 @@ class Core extends \Pimple
return $this['Registry']; return $this['Registry'];
} }
/**
* Getter
*
* @return \Registry
*/
public function getCache() public function getCache()
{ {
return $this['Cache']; return $this['Cache'];
} }
public function getOpcodeCache()
{
return $this['OpcodeCache'];
}
/** /**
* Getter * Getter
* *
@@ -279,7 +272,7 @@ class Core extends \Pimple
*/ */
public function isAuthenticated() public function isAuthenticated()
{ {
$session = \Session_Handler::getInstance(\appbox::get_instance()); $session = \Session_Handler::getInstance(\appbox::get_instance($this));
return $session->is_authenticated(); return $session->is_authenticated();
} }
@@ -291,7 +284,7 @@ class Core extends \Pimple
*/ */
public function getAuthenticatedUser() public function getAuthenticatedUser()
{ {
$appbox = \appbox::get_instance(); $appbox = \appbox::get_instance($this);
$session = \Session_Handler::getInstance($appbox); $session = \Session_Handler::getInstance($appbox);
return \User_Adapter::getInstance($session->get_usr_id(), $appbox); return \User_Adapter::getInstance($session->get_usr_id(), $appbox);
@@ -338,9 +331,14 @@ class Core extends \Pimple
return; return;
} }
public function getLocale()
{
return $this->getRequest()->getLocale();
}
protected function enableEvents() protected function enableEvents()
{ {
$events = \eventsmanager_broker::getInstance(\appbox::get_instance(), $this); $events = \eventsmanager_broker::getInstance(\appbox::get_instance($this), $this);
$events->start(); $events->start();
return; return;
@@ -386,7 +384,7 @@ class Core extends \Pimple
protected function detectLanguage() protected function detectLanguage()
{ {
$this->getRequest()->setDefaultLocale( $this->getRequest()->setDefaultLocale(
$this->getRegistry()->get('GV_default_lng', 'en_GB') $this->getRegistry()->get('GV_default_lng', 'en_GB')
); );
$cookies = $this->getRequest()->cookies; $cookies = $this->getRequest()->cookies;
@@ -415,7 +413,7 @@ class Core extends \Pimple
{ {
require_once __DIR__ . '/Loader/CacheAutoloader.php'; require_once __DIR__ . '/Loader/CacheAutoloader.php';
$prefix = 'class_'; $prefix = 'class_';
$namespace = md5(__DIR__); $namespace = md5(__DIR__);
$loader = new Loader\CacheAutoloader($prefix, $namespace); $loader = new Loader\CacheAutoloader($prefix, $namespace);
@@ -433,30 +431,30 @@ class Core extends \Pimple
//Register prefixes //Register prefixes
$loader->registerPrefixes(array( $loader->registerPrefixes(array(
'Twig' => realpath(__DIR__ . '/../../vendor/Twig/lib')) 'Twig' => realpath(__DIR__ . '/../../vendor/Twig/lib'))
); );
$loader->registerPrefixes(array( $loader->registerPrefixes(array(
'Twig_Extensions' => realpath(__DIR__ . '/../../vendor/Twig-extensions/lib')) 'Twig_Extensions' => realpath(__DIR__ . '/../../vendor/Twig-extensions/lib'))
); );
//Register namespaces //Register namespaces
$loader->registerNamespaces(array( $loader->registerNamespaces(array(
'Alchemy' => realpath(__DIR__ . '/../..'), 'Alchemy' => realpath(__DIR__ . '/../..'),
'Symfony' => realpath(__DIR__ . '/../../vendor/symfony/src'), 'Symfony' => realpath(__DIR__ . '/../../vendor/symfony/src'),
'Doctrine\\ORM' => realpath(__DIR__ . '/../../vendor/doctrine2-orm/lib'), 'Doctrine\\ORM' => realpath(__DIR__ . '/../../vendor/doctrine2-orm/lib'),
'Doctrine\\DBAL' => realpath(__DIR__ . '/../../vendor/doctrine2-orm/lib/vendor/doctrine-dbal/lib'), 'Doctrine\\DBAL' => realpath(__DIR__ . '/../../vendor/doctrine2-orm/lib/vendor/doctrine-dbal/lib'),
'Doctrine\\Common' => realpath(__DIR__ . '/../../vendor/doctrine2-orm/lib/vendor/doctrine-common/lib'), 'Doctrine\\Common' => realpath(__DIR__ . '/../../vendor/doctrine2-orm/lib/vendor/doctrine-common/lib'),
'Doctrine\\Common\\DataFixtures' => realpath(__DIR__ . '/../../vendor/data-fixtures/lib'), 'Doctrine\\Common\\DataFixtures' => realpath(__DIR__ . '/../../vendor/data-fixtures/lib'),
'Entities' => realpath(__DIR__ . '/../../Doctrine/'), 'Entities' => realpath(__DIR__ . '/../../Doctrine/'),
'Repositories' => realpath(__DIR__ . '/../../Doctrine/'), 'Repositories' => realpath(__DIR__ . '/../../Doctrine/'),
'Proxies' => realpath(__DIR__ . '/../../Doctrine/'), 'Proxies' => realpath(__DIR__ . '/../../Doctrine/'),
'Doctrine\\Logger' => realpath(__DIR__ . '/../../'), 'Doctrine\\Logger' => realpath(__DIR__ . '/../../'),
'Monolog' => realpath(__DIR__ . '/../../vendor/Silex/vendor/monolog/src'), 'Monolog' => realpath(__DIR__ . '/../../vendor/Silex/vendor/monolog/src'),
'Gedmo' => realpath(__DIR__ . '/../../vendor/doctrine2-gedmo/lib'), 'Gedmo' => realpath(__DIR__ . '/../../vendor/doctrine2-gedmo/lib'),
'Events' => realpath(__DIR__ . '/../../Doctrine'), 'Events' => realpath(__DIR__ . '/../../Doctrine'),
'DoctrineExtensions' => realpath(__DIR__ . "/../../vendor/doctrine2-beberlei/lib"), 'DoctrineExtensions' => realpath(__DIR__ . "/../../vendor/doctrine2-beberlei/lib"),
'Types' => realpath(__DIR__ . "/../../Doctrine"), 'Types' => realpath(__DIR__ . "/../../Doctrine"),
'PhraseaFixture' => realpath(__DIR__ . "/../../conf.d"), 'PhraseaFixture' => realpath(__DIR__ . "/../../conf.d"),
)); ));
$loader->register(); $loader->register();

View File

@@ -134,7 +134,8 @@ class PDF
protected function print_thumbnailGrid($links=false) protected function print_thumbnailGrid($links=false)
{ {
$appbox = \appbox::get_instance(); $core = \bootstrap::getCore();
$appbox = \appbox::get_instance($core);
$user = \User_Adapter::getInstance($appbox->get_session()->get_usr_id(), $appbox); $user = \User_Adapter::getInstance($appbox->get_session()->get_usr_id(), $appbox);
$NDiapoW = 3; $NDiapoW = 3;
@@ -331,7 +332,8 @@ class PDF
protected function print_preview($withtdm) protected function print_preview($withtdm)
{ {
$appbox = \appbox::get_instance(); $core = \bootstrap::getCore();
$appbox = \appbox::get_instance($core);
$registry = \registry::get_instance(); $registry = \registry::get_instance();
$user = \User_Adapter::getInstance($appbox->get_session()->get_usr_id(), $appbox); $user = \User_Adapter::getInstance($appbox->get_session()->get_usr_id(), $appbox);

View File

@@ -49,7 +49,9 @@ class UserProvider implements ControlProviderInterface
*/ */
public function find($query, \User_Adapter $for_user, \databox $on_databox = null) public function find($query, \User_Adapter $for_user, \databox $on_databox = null)
{ {
$user_query = new \User_Query(\appbox::get_instance()); $Core = \bootstrap::getCore();
$user_query = new \User_Query(\appbox::get_instance($Core));
$users = $user_query $users = $user_query
->like(\User_Query::LIKE_EMAIL, $query) ->like(\User_Query::LIKE_EMAIL, $query)
@@ -81,9 +83,11 @@ class UserProvider implements ControlProviderInterface
*/ */
public function validate($id) public function validate($id)
{ {
$Core = \bootstrap::getCore();
try try
{ {
\User_Adapter::getInstance($id, \appbox::get_instance()); \User_Adapter::getInstance($id, \appbox::get_instance($Core));
return true; return true;
} }
@@ -102,7 +106,9 @@ class UserProvider implements ControlProviderInterface
*/ */
public function getValue($id) public function getValue($id)
{ {
$user = \User_Adapter::getInstance($id, \appbox::get_instance()); $Core = \bootstrap::getCore();
$user = \User_Adapter::getInstance($id, \appbox::get_instance($Core));
return $user->get_display_name(); return $user->get_display_name();
} }
@@ -114,7 +120,9 @@ class UserProvider implements ControlProviderInterface
*/ */
public function getRessource($id) public function getRessource($id)
{ {
return \User_Adapter::getInstance($id, \appbox::get_instance()); $Core = \bootstrap::getCore();
return \User_Adapter::getInstance($id, \appbox::get_instance($Core));
} }
} }