public function setUp() { parent::setUp(); $conn = $this->getConnection(); $options = array('disable_fks' => $conn->getDatabasePlatform() instanceof SqlitePlatform); $schema = new RepositorySchema($options, $conn); // do not use reset as we want to ignore exceptions on drop foreach ($schema->toDropSql($conn->getDatabasePlatform()) as $statement) { try { $conn->exec($statement); } catch (\Exception $e) { // ignore } } foreach ($schema->toSql($conn->getDatabasePlatform()) as $statement) { $conn->exec($statement); } $transport = $this->getTransport(); $repository = new \Jackalope\Repository(null, $transport); $session = $repository->login(new \PHPCR\SimpleCredentials("user", "passwd"), $GLOBALS['phpcr.workspace']); $a = $session->getNode('/')->addNode('node-a'); $a->addNode('child-a')->setProperty('prop', 'aa'); $a->addNode('child-b')->setProperty('prop', 'ab'); $b = $session->getNode('/')->addNode('node-b'); $b->addNode('child-a')->setProperty('prop', 'ba'); $b->addNode('child-b')->setProperty('prop', 'bb'); $session->save(); }
public function setUp() { parent::setUp(); $conn = $this->getConnection(); $options = array('disable_fks' => $conn->getDatabasePlatform() instanceof SqlitePlatform); $schema = new RepositorySchema($options, $conn); // do not use reset as we want to ignore exceptions on drop foreach ($schema->toDropSql($conn->getDatabasePlatform()) as $statement) { try { $conn->exec($statement); } catch (\Exception $e) { // ignore } } foreach ($schema->toSql($conn->getDatabasePlatform()) as $statement) { $conn->exec($statement); } $this->transport = new \Jackalope\Transport\DoctrineDBAL\Client(new \Jackalope\Factory(), $conn); $this->transport->createWorkspace('default'); $this->repository = new \Jackalope\Repository(null, $this->transport); try { $this->transport->createWorkspace($GLOBALS['phpcr.workspace']); } catch (\PHPCR\RepositoryException $e) { if ($e->getMessage() != "Workspace '" . $GLOBALS['phpcr.workspace'] . "' already exists") { // if the message is not that the workspace already exists, something went really wrong throw $e; } } $this->session = $this->repository->login(new \PHPCR\SimpleCredentials("user", "passwd"), $GLOBALS['phpcr.workspace']); }
protected function loadFixtures(Connection $conn) { $options = array('disable_fks' => $conn->getDatabasePlatform() instanceof SqlitePlatform); $schema = new RepositorySchema($options, $conn); $tables = $schema->getTables(); foreach ($tables as $table) { $conn->exec('DELETE FROM ' . $table->getName()); } }
/** * {@inheritDoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $connection = $this->getHelper('jackalope-doctrine-dbal')->getConnection(); if (!$connection instanceof Connection) { $output->write(PHP_EOL . '<error>The provided connection is not an instance of the Doctrine DBAL connection.</error>' . PHP_EOL); throw new \InvalidArgumentException('The provided connection is not an instance of the Doctrine DBAL connection.'); } if (true !== $input->getOption('dump-sql') && !$input->getOption('force')) { $output->write('ATTENTION: This operation should not be executed in a production environment. Please use "--force" to execute the command.' . PHP_EOL . PHP_EOL); return self::RETURN_CODE_NO_FORCE; } $schema = new RepositorySchema(); try { if ($input->getOption('drop')) { try { foreach ($schema->toDropSql($connection->getDatabasePlatform()) as $sql) { if (true === $input->getOption('dump-sql')) { $output->writeln($sql); } else { $connection->exec($sql); } } } catch (TableNotFoundException $e) { if (false === $input->getOption('force')) { throw $e; } // remove this once we require Doctrine DBAL 2.5+ } catch (DBALException $e) { if (false === $input->getOption('force')) { throw $e; } else { $output->writeln($e->getMessage()); } } } foreach ($schema->toSql($connection->getDatabasePlatform()) as $sql) { if (true === $input->getOption('dump-sql')) { $output->writeln($sql); } else { $connection->exec($sql); } } } catch (\PDOException $e) { if ("42S01" === $e->getCode()) { $output->write(PHP_EOL . '<error>The tables already exist. Nothing was changed.</error>' . PHP_EOL . PHP_EOL); return self::RETURN_CODE_NOT_DROP; } throw $e; } if (true !== $input->getOption('dump-sql')) { $output->writeln("Jackalope Doctrine DBAL tables have been initialized successfully."); } return 0; }
private function loadPhpcrOdm() { $this['psi_content_type.storage.doctrine.phpcr_odm.property_encoder'] = function ($container) { return new PropertyEncoder('psict', 'https://github.com/psiphp/content-type'); }; $this['psi_content_type.storage.doctrine.phpcr_odm.field_mapper'] = function ($container) { return new FieldMapper($container['psi_content_type.storage.doctrine.phpcr_odm.property_encoder'], $container['psi_content_type.field_loader']); }; $this['psi_content_type.storage.doctrine.phpcr_odm.collection_updater'] = function ($container) { return new CollectionIdentifierUpdater($container['psi_content_type.metadata.factory'], $container['psi_content_type.storage.doctrine.phpcr_odm.property_encoder']); }; $this['doctrine_phpcr.document_manager'] = function ($container) { $registerNodeTypes = false; // automatically setup the schema if the db doesn't exist yet. if (!file_exists($container['config']['db_path'])) { if (!file_exists($dir = dirname($container['config']['db_path']))) { mkdir($dir); } $connection = $container['dbal.connection']; $schema = new RepositorySchema(); foreach ($schema->toSql($connection->getDatabasePlatform()) as $sql) { $connection->exec($sql); } $registerNodeTypes = true; } // register the phpcr session $factory = new RepositoryFactoryDoctrineDBAL(); $repository = $factory->getRepository(['jackalope.doctrine_dbal_connection' => $container['dbal.connection']]); $session = $repository->login(new SimpleCredentials(null, null), 'default'); if ($registerNodeTypes) { $typeRegistrator = new NodeTypeRegistrator(); $typeRegistrator->registerNodeTypes($session); $ctTypeRegistrator = new CtNodeTypeRegistrator($container['psi_content_type.storage.doctrine.phpcr_odm.property_encoder']); $ctTypeRegistrator->registerNodeTypes($session); } // annotation driver $annotationDriver = new AnnotationDriver($container['annotation_reader'], [__DIR__ . '/../../vendor/doctrine/phpcr-odm/lib/Doctrine/ODM/PHPCR/Document', __DIR__ . '/Example']); $xmlDriver = new XmlDriver([__DIR__ . '/mappings']); $annotationDriver = new AnnotationDriver($container['annotation_reader'], [__DIR__ . '/../../vendor/doctrine/phpcr-odm/lib/Doctrine/ODM/PHPCR/Document', __DIR__ . '/Example']); $chain = new MappingDriverChain(); $chain->addDriver($annotationDriver, 'Psi\\Bridge\\ContentType\\Doctrine\\PhpcrOdm\\Tests\\Functional\\Example'); $chain->addDriver($xmlDriver, 'Psi\\Component\\ContentType\\Tests\\Functional\\Example\\Model'); $chain->addDriver($annotationDriver, 'Doctrine'); $config = new Configuration(); $config->setMetadataDriverImpl($chain); $manager = DocumentManager::create($session, $config); $manager->getEventManager()->addEventSubscriber(new MetadataSubscriber($container['psi_content_type.metadata.factory'], $container['psi_content_type.field_loader'], $container['psi_content_type.storage.doctrine.phpcr_odm.field_mapper'])); $manager->getEventManager()->addEventSubscriber(new CollectionSubscriber($container['psi_content_type.metadata.factory'], $container['psi_content_type.storage.doctrine.phpcr_odm.property_encoder'])); return $manager; }; }
/** * {@inheritDoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $connection = $this->getHelper('jackalope-doctrine-dbal')->getConnection(); if (!$connection instanceof Connection) { $output->write(PHP_EOL.'<error>The provided connection is not an instance of the Doctrine DBAL connection.</error>'.PHP_EOL); throw new \InvalidArgumentException('The provided connection is not an instance of the Doctrine DBAL connection.'); } if (true !== $input->getOption('dump-sql')) { $output->write('ATTENTION: This operation should not be executed in a production environment.' . PHP_EOL . PHP_EOL); } $schema = new RepositorySchema; try { if ($input->getOption('drop')) { foreach ($schema->toDropSql($connection->getDatabasePlatform()) as $sql) { if (true === $input->getOption('dump-sql')) { $output->writeln($sql); } else { try { $connection->exec($sql); } catch (\Exception $e) { $output->writeln($e->getMessage()); } } } } foreach ($schema->toSql($connection->getDatabasePlatform()) as $sql) { if (true === $input->getOption('dump-sql')) { $output->writeln($sql); } else { $connection->exec($sql); } } } catch (\PDOException $e) { if ("42S01" == $e->getCode()) { $output->write(PHP_EOL.'<error>The tables already exist. Nothing was changed.</error>'.PHP_EOL.PHP_EOL); // TODO: add a parameter to drop old scheme first return; } throw $e; } if (true !== $input->getOption('dump-sql')) { $output->writeln("Jackalope Doctrine DBAL tables have been initialized successfully."); } }
private function getSession() { $connection = DriverManager::getConnection(array('driver' => 'pdo_sqlite', 'path' => ':memory:')); $factory = new RepositoryFactoryDoctrineDBAL(); $repository = $factory->getRepository(array('jackalope.doctrine_dbal_connection' => $connection)); $schema = new RepositorySchema(array('disable_fks' => true), $connection); $schema->reset(); $session = $repository->login(new \PHPCR\SimpleCredentials('', '')); $cnd = <<<CND <phpcr='http://www.doctrine-project.org/projects/phpcr_odm'> [phpcr:managed] mixin - phpcr:class (STRING) - phpcr:classparents (STRING) multiple CND; $session->getWorkspace()->getNodeTypeManager()->registerNodeTypesCnd($cnd, true); return $session; }