partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit e1faebe24a3f56f454955af8d009502d4813cb29
parent 19891b015e3e037237ca89a0531c20ad9b877d3d
Author: Felicitus <felicitus@felicitus.org>
Date:   Mon, 16 Dec 2013 20:41:17 +0100

Temporary injection for the EntityManager created by Sf2

Diffstat:
Mapp/AppKernel.php | 37+++++++++++++++++++++++++++++++++++++
Msrc/backend/PartKeepr/PartKeepr.php | 176+++----------------------------------------------------------------------------
2 files changed, 42 insertions(+), 171 deletions(-)

diff --git a/app/AppKernel.php b/app/AppKernel.php @@ -2,9 +2,46 @@ use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface; +use Symfony\Component\DependencyInjection\Container; class AppKernel extends Kernel { + /** + * @todo Remove this after successful migration so SF2 + * @var \AppKernel + */ + private static $kernel = null; + + /** + * Temporary constructor to inject the service container. + * + * @todo Remove this after successful migration to SF2 + * @param string $environment + * @param bool $debug + */ + public function __construct ($environment, $debug) { + parent::__construct($environment, $debug); + + AppKernel::$kernel = $this; + } + + /** + * Returns the service container + * + * @todo Remove this after successful migration to SF2 + * @return null|\Symfony\Component\DependencyInjection\ContainerInterface + */ + public static function getMigrationContainer () { + if (self::$kernel instanceof \AppKernel) { + if (!self::$kernel->getContainer() instanceof Container) { + self::$kernel->boot(); + } + return self::$kernel->getContainer(); + } + + return null; + } + public function registerBundles() { $bundles = array( diff --git a/src/backend/PartKeepr/PartKeepr.php b/src/backend/PartKeepr/PartKeepr.php @@ -1,9 +1,7 @@ <?php namespace PartKeepr; -use Doctrine\Common\ClassLoader, - PartKeepr\SystemNotice\SystemNoticeManager, - Doctrine\ORM\Configuration, +use PartKeepr\SystemNotice\SystemNoticeManager, Doctrine\ORM\EntityManager, PartKeepr\Util\Configuration as PartKeeprConfiguration; @@ -11,13 +9,6 @@ use Doctrine\Common\ClassLoader, class PartKeepr { /** - * - * Contains the doctrine entity manager. - * @var Doctrine\ORM\EntityManager - */ - private static $entityManager = null; - - /** * Initializes the PartKeepr system * * You *need* to call this method before doing anything else. @@ -26,44 +17,9 @@ class PartKeepr { * Usually, you don't need to pass anything here. * * @param $environment string The environment to use, null otherwise. - * @return nothing */ public static function initialize ($environment = null) { - self::initializeClassLoaders(); self::initializeConfig($environment); - self::initializeDoctrine(); - } - - /** - * Initializes the doctrine class loader and sets up the - * directories. - * - * @param none - * @return nothing - */ - public static function initializeClassLoaders() { - return; - require_once 'Doctrine/Common/ClassLoader.php'; - - $classLoader = new ClassLoader('PartKeepr', self::getRootDirectory() . "/src/backend"); - $classLoader->register(); - - $classLoader = new ClassLoader('Doctrine\ORM'); - $classLoader->register(); - - $classLoader = new ClassLoader('Doctrine\DBAL'); - $classLoader->register(); - - $classLoader = new ClassLoader('Doctrine\Common'); - $classLoader->register(); - - $classLoader = new ClassLoader('Symfony'); - $classLoader->register(); - - $classLoader = new ClassLoader("DoctrineExtensions\NestedSet", self::getRootDirectory() ."/3rdparty/doctrine2-nestedset/lib"); - $classLoader->register(); - - } /** @@ -89,7 +45,6 @@ class PartKeepr { * * * @param $environment string The environment to use, null otherwise. - * @return nothing */ public static function initializeConfig ($environment = null) { if ($environment != null) { @@ -152,127 +107,8 @@ class PartKeepr { } /** - * Initializes the doctrine framework and - * sets all required configuration options. - * - * @param none - * @return nothing - */ - public static function initializeDoctrine () { - $config = new Configuration; - - $driverImpl = $config->newDefaultAnnotationDriver( - array(__DIR__), - false - ); - $config->setMetadataDriverImpl($driverImpl); - - $connectionOptions = PartKeepr::createConnectionOptionsFromConfig(); - - switch (strtolower(PartKeeprConfiguration::getOption("partkeepr.cache.implementation", "default"))) { - case "apc": - $cache = new \Doctrine\Common\Cache\ApcCache(); - break; - case "xcache": - if (php_sapi_name() !== "cli") { - $cache = new \Doctrine\Common\Cache\XcacheCache(); - } else { - // For CLI SAPIs, revert to the ArrayCache as Xcache spits out strange warnings when running in CLI. - $cache = new \Doctrine\Common\Cache\ArrayCache(); - } - - break; - case "memcache": - $memcache = new \Memcache(); - $memcache->connect( PartKeeprConfiguration::getOption("partkeepr.cache.memcache.host", "localhost"), - PartKeeprConfiguration::getOption("partkeepr.cache.memcache.port", "11211")); - $cache = new \Doctrine\Common\Cache\MemcacheCache(); - $cache->setMemcache($memcache); - break; - case "default": - case "auto": - if (extension_loaded("xcache")) { - $cache = new \Doctrine\Common\Cache\XcacheCache(); - } else if (extension_loaded("apc")) { - $cache = new \Doctrine\Common\Cache\ApcCache(); - } else { - $cache = new \Doctrine\Common\Cache\ArrayCache(); - } - break; - case "none": - $cache = new \Doctrine\Common\Cache\ArrayCache(); - break; - } - - $config->setMetadataCacheImpl($cache); - - $config->setQueryCacheImpl($cache); - - $config->setProxyDir(self::getRootDirectory() . '/data/proxies'); - $config->setProxyNamespace('Proxies'); - $config->setEntityNamespaces(self::getEntityClasses()); - $config->setAutoGenerateProxyClasses(true); - - if (PartKeeprConfiguration::getOption("partkeepr.database.echo_sql_log", false) === true) { - $logger = new \Doctrine\DBAL\Logging\EchoSQLLogger(); - $config->setSQLLogger($logger); - } - - self::$entityManager = EntityManager::create($connectionOptions, $config); - } - - public static function createConnectionOptionsFromConfig () { - $connectionOptions = array(); - - $driver = PartKeeprConfiguration::getOption("partkeepr.database.driver"); - - switch ($driver) { - case "pdo_mysql": - // Force SET NAMES, as PHP/PDO <5.3.6 silently ignores "charset" - $connectionOptions["driverOptions"] = array(1002=>'SET NAMES utf8'); - case "pdo_pgsql": - case "pdo_oci": - case "oci8": - case "pdo_sqlsrv": - $connectionOptions["driver"] = $driver; - $connectionOptions["dbname"] = PartKeeprConfiguration::getOption("partkeepr.database.dbname", "partkeepr"); - $connectionOptions["user"] = PartKeeprConfiguration::getOption("partkeepr.database.username", "partkeepr"); - $connectionOptions["password"] = PartKeeprConfiguration::getOption("partkeepr.database.password", "partkeepr"); - $connectionOptions["charset"] = "utf8"; - /** - * Compatibility with older configuration files. We check for the key "hostname" as well as "host". - */ - if (PartKeeprConfiguration::getOption("partkeepr.database.hostname", null) !== null) { - $connectionOptions["host"] = PartKeeprConfiguration::getOption("partkeepr.database.hostname"); - } else { - $connectionOptions["host"] = PartKeeprConfiguration::getOption("partkeepr.database.host", "localhost"); - } - - - if (PartKeeprConfiguration::getOption("partkeepr.database.port") !== null) { - $connectionOptions["port"] = PartKeeprConfiguration::getOption("partkeepr.database.port"); - } - - if (PartKeeprConfiguration::getOption("partkeepr.database.mysql_socket", null) !== null) { - $connectionOptions["unix_socket"] = PartKeeprConfiguration::getOption("partkeepr.database.mysql_socket"); - } - break; - case "pdo_sqlite": - $connectionOptions["driver"] = $driver; - $connectionOptions["user"] = PartKeeprConfiguration::getOption("partkeepr.database.username", "partkeepr"); - $connectionOptions["password"] = PartKeeprConfiguration::getOption("partkeepr.database.password", "partkeepr"); - $connectionOptions["path"] = PartKeeprConfiguration::getOption("partkeepr.database.sqlite_path", PartKeepr::getRootDirectory() . "/data/partkeepr.sqlite"); - break; - default: - throw new \Exception(sprintf("Unknown driver %s", $driver)); - } - - return $connectionOptions; - } - - /** * Returns the EntityManager. Shortcut for getEntityManager(). - * @return \Doctrine\ORM\EntityManager The EntityManager + * @return EntityManager The EntityManager */ public static function getEM () { return self::getEntityManager(); @@ -284,13 +120,11 @@ class PartKeepr { /** * Returns the EntityManager. - * @return Doctrine\ORM\EntityManager The EntityManager + * @return EntityManager The EntityManager */ public static function getEntityManager () { - if (!self::$entityManager instanceof EntityManager) { - throw new \Exception("No EntityManager found. Make sure you called initializeDoctrine() or initialize()."); - } - return self::$entityManager; + $container = \AppKernel::getMigrationContainer(); + return $container->get('doctrine')->getManager(); } /**