/** * Creates a new entity manager instance based on the passed configuration. * * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance to create the entity manager for * @param \AppserverIo\Appserver\Core\Api\Node\PersistenceUnitNodeInterface $persistenceUnitNode The datasource configuration * * @return object The entity manager instance */ public static function factory(ApplicationInterface $application, PersistenceUnitNodeInterface $persistenceUnitNode) { // register additional annotation libraries foreach ($persistenceUnitNode->getAnnotationRegistries() as $annotationRegistry) { AnnotationRegistry::registerAutoloadNamespace($annotationRegistry->getNamespace(), $annotationRegistry->getDirectoriesAsArray($application->getWebappPath())); } // globally ignore configured annotations to ignore foreach ($persistenceUnitNode->getIgnoredAnnotations() as $ignoredAnnotation) { AnnotationReader::addGlobalIgnoredName($ignoredAnnotation->getNodeValue()->__toString()); } // load the metadata configuration $metadataConfiguration = $persistenceUnitNode->getMetadataConfiguration(); // prepare the setup properties $absolutePaths = $metadataConfiguration->getDirectoriesAsArray($application->getWebappPath()); $proxyDir = $metadataConfiguration->getParam(MetadataConfigurationNode::PARAM_PROXY_DIR); $isDevMode = $metadataConfiguration->getParam(MetadataConfigurationNode::PARAM_IS_DEV_MODE); $useSimpleAnnotationReader = $metadataConfiguration->getParam(MetadataConfigurationNode::PARAM_USE_SIMPLE_ANNOTATION_READER); // load the factory method from the available mappings $factoryMethod = EntityManagerFactory::$metadataMapping[$metadataConfiguration->getType()]; // create the database configuration and initialize the entity manager $configuration = Setup::$factoryMethod($absolutePaths, $isDevMode, $proxyDir, null, $useSimpleAnnotationReader); // load the datasource node $datasourceNode = null; foreach ($application->getInitialContext()->getSystemConfiguration()->getDatasources() as $datasourceNode) { if ($datasourceNode->getName() === $persistenceUnitNode->getDatasource()->getName()) { break; } } // throw a exception if the configured datasource is NOT available if ($datasourceNode == null) { throw new \Exception(sprintf('Can\'t find a datasource node for persistence unit %s', $persistenceUnitNode->getName())); } // load the database node $databaseNode = $datasourceNode->getDatabase(); // throw an exception if the configured database is NOT available if ($databaseNode == null) { throw new \Exception(sprintf('Can\'t find database node for persistence unit %s', $persistenceUnitNode->getName())); } // load the driver node $driverNode = $databaseNode->getDriver(); // throw an exception if the configured driver is NOT available if ($driverNode == null) { throw new \Exception(sprintf('Can\'t find driver node for persistence unit %s', $persistenceUnitNode->getName())); } // initialize and return a entity manager decorator instance return new DoctrineEntityManagerDecorator(EntityManager::create(ConnectionUtil::get($application)->fromDatabaseNode($databaseNode), $configuration)); }
/** * Deploys the entity manager described by the passed XML node. * * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance * @param \AppserverIo\Appserver\Core\Api\PersistenceUnitNodeInterface $persistenceUnitNode The XML node that describes the entity manager * * @return void */ public function registerEntityManager(ApplicationInterface $application, PersistenceUnitNodeInterface $persistenceUnitNode) { // initialize the the entity manager instance $this->entityManagers[$lookupName = $persistenceUnitNode->getName()] = $persistenceUnitNode; // bind the callback for the entity manager instance to the // naming directory => necessary for DI provider $application->getNamingDirectory()->bind(sprintf('php:global/%s/%s', $application->getUniqueName(), $lookupName), array(&$this, 'lookup'), array($lookupName)); }
/** * Creates a new entity manager instance based on the passed configuration. * * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance to create the entity manager for * @param \AppserverIo\Appserver\Core\Api\Node\PersistenceUnitNodeInterface $persistenceUnitNode The datasource configuration * * @return object The entity manager instance */ public static function factory(ApplicationInterface $application, PersistenceUnitNodeInterface $persistenceUnitNode) { // register additional annotation libraries foreach ($persistenceUnitNode->getAnnotationRegistries() as $annotationRegistry) { AnnotationRegistry::registerAutoloadNamespace($annotationRegistry->getNamespace(), $annotationRegistry->getDirectoriesAsArray($application->getWebappPath())); } // load the metadata configuration $metadataConfiguration = $persistenceUnitNode->getMetadataConfiguration(); // prepare the setup properties $absolutePaths = $metadataConfiguration->getDirectoriesAsArray($application->getWebappPath()); $proxyDir = $metadataConfiguration->getParam(MetadataConfigurationNode::PARAM_PROXY_DIR); $isDevMode = $metadataConfiguration->getParam(MetadataConfigurationNode::PARAM_IS_DEV_MODE); $useSimpleAnnotationReader = $metadataConfiguration->getParam(MetadataConfigurationNode::PARAM_USE_SIMPLE_ANNOTATION_READER); // load the factory method from the available mappings $factoryMethod = EntityManagerFactory::$metadataMapping[$metadataConfiguration->getType()]; // create the database configuration and initialize the entity manager $configuration = Setup::$factoryMethod($absolutePaths, $isDevMode, $proxyDir, null, $useSimpleAnnotationReader); // load the datasource node $datasourceNode = null; foreach ($application->getInitialContext()->getSystemConfiguration()->getDatasources() as $datasourceNode) { if ($datasourceNode->getName() === $persistenceUnitNode->getDatasource()->getName()) { break; } } // throw a exception if the configured datasource is NOT available if ($datasourceNode == null) { throw new \Exception(sprintf('Can\'t find a datasource node for persistence unit %s', $persistenceUnitNode->getName())); } // load the database node $databaseNode = $datasourceNode->getDatabase(); // throw an exception if the configured database is NOT available if ($databaseNode == null) { throw new \Exception(sprintf('Can\'t find database node for persistence unit %s', $persistenceUnitNode->getName())); } // load the driver node $driverNode = $databaseNode->getDriver(); // throw an exception if the configured driver is NOT available if ($driverNode == null) { throw new \Exception(sprintf('Can\'t find driver node for persistence unit %s', $persistenceUnitNode->getName())); } // initialize the connection parameters with the mandatory driver $connectionParameters = array('driver' => $databaseNode->getDriver()->getNodeValue()->__toString()); // initialize the path/memory to the database when we use sqlite for example if ($pathNode = $databaseNode->getPath()) { $connectionParameters['path'] = $application->getWebappPath() . DIRECTORY_SEPARATOR . $pathNode->getNodeValue()->__toString(); } elseif ($memoryNode = $databaseNode->getMemory()) { $connectionParameters['memory'] = Boolean::valueOf(new String($memoryNode->getNodeValue()->__toString()))->booleanValue(); } else { // do nothing here, because there is NO option } // add username, if specified if ($userNode = $databaseNode->getUser()) { $connectionParameters['user'] = $userNode->getNodeValue()->__toString(); } // add password, if specified if ($passwordNode = $databaseNode->getPassword()) { $connectionParameters['password'] = $passwordNode->getNodeValue()->__toString(); } // add database name if using another PDO driver than sqlite if ($databaseNameNode = $databaseNode->getDatabaseName()) { $connectionParameters['dbname'] = $databaseNameNode->getNodeValue()->__toString(); } // add database host if using another PDO driver than sqlite if ($databaseHostNode = $databaseNode->getDatabaseHost()) { $connectionParameters['host'] = $databaseHostNode->getNodeValue()->__toString(); } // add charset, if specified if ($charsetNode = $databaseNode->getCharset()) { $connectionParameters['charset'] = $charsetNode->getNodeValue()->__toString(); } // add driver options, if specified if ($driverOptionsNode = $databaseNode->getDriverOptions()) { // explode the raw options separated with a semicolon $rawOptions = explode(';', $driverOptionsNode->getNodeValue()->__toString()); // prepare the array with the driver options key/value pair (separated with a =) $options = array(); foreach ($rawOptions as $rawOption) { list($key, $value) = explode('=', $rawOption); $options[$key] = $value; } // set the driver options $connectionParameters['driverOptions'] = $options; } // add driver options, if specified if ($unixSocketNode = $databaseNode->getUnixSocket()) { $connectionParameters['unix_socket'] = $unixSocketNode->getNodeValue()->__toString(); } // initialize and return a entity manager decorator instance return new DoctrineEntityManagerDecorator(EntityManager::create($connectionParameters, $configuration)); }
/** * Convert's the passed persistence unit node into a DTO. * * @param \AppserverIo\Appserver\Core\Api\Node\PersistenceUnitNodeInterface $persistenceUnitNode The persistence unit node to convert * * @return \AppserverIo\Apps\Api\TransferObject\PersistenceUnitOverviewData The DTO */ public function toPersistenceUnitOverviewData(PersistenceUnitNodeInterface $persistenceUnitNode) { $overviewData = new PersistenceUnitOverviewData(); $overviewData->setId($persistenceUnitNode->getPrimaryKey()); $overviewData->setName($persistenceUnitNode->getName()); return $overviewData; }
/** * Factory method to create a new cache instance from the passed configuration. * * @param \AppserverIo\Appserver\Core\Api\Node\PersistenceUnitNodeInterface $persistenceUnit The persistence unit node * @param \AppserverIo\Appserver\PersistenceContainer\Doctrine\V2\CacheFactory\CacheConfigurationNodeInterface $cacheConfiguration The cache configuration * * @return \Doctrine\Common\Cache\CacheProvider The cache instance */ public static function getCacheImpl(PersistenceUnitNodeInterface $persistenceUnit, CacheConfigurationNodeInterface $cacheConfiguration) { // load the factory class $factory = $cacheConfiguration->getFactory(); // create a cache instance $cache = $factory::get($cacheConfiguration->getParams()); $cache->setNamespace(sprintf('dc2_%s_', md5($persistenceUnit->getName()))); // return the cache instance return $cache; }