AppserverIo\Appserver\PersistenceContainer\PersistenceManager::registerEntityManagers PHP Method

registerEntityManagers() public method

Registers the entity managers at startup.
public registerEntityManagers ( AppserverIo\Psr\Application\ApplicationInterface $application ) : void
$application AppserverIo\Psr\Application\ApplicationInterface The application instance
return void
    public function registerEntityManagers(ApplicationInterface $application)
    {
        // build up META-INF directory var
        $metaInfDir = $this->getWebappPath() . DIRECTORY_SEPARATOR . 'META-INF';
        // check if we've found a valid directory
        if (is_dir($metaInfDir) === false) {
            return;
        }
        // check META-INF + subdirectories for XML files with MQ definitions
        /** @var \AppserverIo\Appserver\Core\Api\DeploymentService $service */
        $service = $application->newService('AppserverIo\\Appserver\\Core\\Api\\DeploymentService');
        $xmlFiles = $service->globDir(AppEnvironmentHelper::getEnvironmentAwareGlobPattern($this->getWebappPath(), 'META-INF' . DIRECTORY_SEPARATOR . 'persistence'));
        // load the configuration service instance
        /** @var \AppserverIo\Appserver\Core\Api\ConfigurationService $configurationService */
        $configurationService = $application->newService('AppserverIo\\Appserver\\Core\\Api\\ConfigurationService');
        // load the container node to initialize the system properties
        /** @var \AppserverIo\Appserver\Core\Api\Node\ContainerNodeInterface $containerNode */
        $containerNode = $application->getContainer()->getContainerNode();
        // gather all the deployed web applications
        foreach ($xmlFiles as $file) {
            try {
                // validate the file here, but skip if the validation fails
                $configurationService->validateFile($file, null, true);
                // load the system properties
                $properties = $service->getSystemProperties($containerNode);
                // append the application specific properties
                $properties->add(SystemPropertyKeys::WEBAPP, $webappPath = $application->getWebappPath());
                $properties->add(SystemPropertyKeys::WEBAPP_NAME, basename($webappPath));
                $properties->add(SystemPropertyKeys::WEBAPP_CACHE, $application->getCacheDir());
                $properties->add(SystemPropertyKeys::WEBAPP_SESSION, $application->getSessionDir());
                // create a new persistence manager node instance and replace the properties
                $persistenceNode = new PersistenceNode();
                $persistenceNode->initFromFile($file);
                $persistenceNode->replaceProperties($properties);
                // register the entity managers found in the configuration
                foreach ($persistenceNode->getPersistenceUnits() as $persistenceUnitNode) {
                    $this->registerEntityManager($application, $persistenceUnitNode);
                }
            } catch (InvalidConfigurationException $e) {
                // try to load the system logger instance
                /** @var \Psr\Log\LoggerInterface $systemLogger */
                if ($systemLogger = $this->getApplication()->getInitialContext()->getSystemLogger()) {
                    $systemLogger->error($e->getMessage());
                    $systemLogger->critical(sprintf('Persistence configuration file %s is invalid, needed entity managers might be missing.', $file));
                }
            } catch (\Exception $e) {
                // try to load the system logger instance
                /** @var \Psr\Log\LoggerInterface $systemLogger */
                if ($systemLogger = $this->getApplication()->getInitialContext()->getSystemLogger()) {
                    $systemLogger->error($e->__toString());
                }
            }
        }
    }