app = $application; $this->applicationBox = $appbox; $this->connectionFactory = $connectionFactory; $this->databoxRepository = $databoxRepository; $this->configuration = $defaultDbConfiguration; $this->rootPath = $rootPath; } /** * @param User $owner * @param string $databaseName * @param string $dataTemplate * @param DataboxConnectionSettings|null $connectionSettings * @return \databox */ public function createDatabox( $databaseName, $dataTemplate, User $owner, DataboxConnectionSettings $connectionSettings = null ) { $this->validateDatabaseName($databaseName); $dataTemplate = new \SplFileInfo($this->rootPath . '/lib/conf.d/data_templates/' . $dataTemplate . '.xml'); $connectionSettings = $connectionSettings ?: DataboxConnectionSettings::fromArray( $this->configuration->get(['main', 'database']) ); $factory = $this->connectionFactory; /** @var Connection $connection */ $connection = $factory([ 'host' => $connectionSettings->getHost(), 'port' => $connectionSettings->getPort(), 'user' => $connectionSettings->getUser(), 'password' => $connectionSettings->getPassword(), 'dbname' => $databaseName ]); $connection->connect(); $databox = \databox::create($this->app, $connection, $dataTemplate); $databox->registerAdmin($owner); $connection->close(); return $databox; } /** * @param string $databaseName * @param User $owner * @param DataboxConnectionSettings $connectionSettings * @return \databox */ public function mountDatabox($databaseName, User $owner, DataboxConnectionSettings $connectionSettings = null) { $this->validateDatabaseName($databaseName); $connectionSettings = $connectionSettings ?: DataboxConnectionSettings::fromArray( $this->configuration->get(['main', 'database']) ); $this->applicationBox->get_connection()->beginTransaction(); try { $databox = \databox::mount( $this->app, $connectionSettings->getHost(), $connectionSettings->getPort(), $connectionSettings->getUser(), $connectionSettings->getPassword(), $databaseName ); $databox->registerAdmin($owner); $this->applicationBox->get_connection()->commit(); return $databox; } catch (\Exception $exception) { $this->applicationBox->get_connection()->rollBack(); throw new \RuntimeException($exception->getMessage(), 0, $exception); } } private function validateDatabaseName($databaseName) { if (trim($databaseName) == '') { throw new \InvalidArgumentException('Database name cannot be empty.', self::EMPTY_DB_NAME); } if (\p4string::hasAccent($databaseName)) { throw new \InvalidArgumentException( 'Database name cannot contain special characters.', self::INVALID_DB_NAME ); } } }