Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

Jackalope's Doctrine-Dbal implementation is great to use if you don't want to run a Jackrabbit server, however one of the difficulties for a first-time user is having to go through the bootstrapping process and initialising the repository. After a bit of poking around the command line utility code that comes with Jackalope I put together this class that detects if the repository hasn't been initialised and does the initialisation automatically for you. It also wraps the session so you're guaranteed a valid repository with an open session when you use this.

You still need to create the database yourself before using this class.

This is the SessionWrapper class...
 SessionWrapper.php
<?php
use Jackalope\Transport\DoctrineDBAL\RepositorySchema;
class SessionWrapper {
private $session = null;
private $active = false;
private $lastError;
public function initSession($dbConnProps) {
$this->connect($dbConnProps);
if (!$this->isActive()) {
$err = $this->getLastError();
// 42S02 means table not found so try to initialise the repository and connect
if (strpos($err, '42S02') === false) {
die('Could not access repository - ' . $err);
}
else {
$this->connect($dbConnProps, true);
if (!$this->isActive()) {
die('Could not access repository - ' . $this->getLastError());
}
}
}
return $this;
}
private function connect($dbConnProps, $init = false) {
try {
$dbConn = \Doctrine\DBAL\DriverManager::getConnection($dbConnProps);
// initialise the repository if required
if ($init) {
// create all the required tables and seed data
$schema = new RepositorySchema;
foreach ($schema->toSql($dbConn->getDatabasePlatform()) as $sql) {
$dbConn->exec($sql);
}
}
$factory = new \Jackalope\RepositoryFactoryDoctrineDBAL();
$repository = $factory->getRepository(
array('jackalope.doctrine_dbal_connection' => $dbConn));
// dummy credentials to comply with the API
$credentials = new \PHPCR\SimpleCredentials(null, null);
$this->session = $repository->login($credentials);
$this->active = true;
}
catch (Exception $e) {
$this->lastError = $e->getMessage();
return $this;
}
return $this;
}
public function isActive() {
return $this->active;
}
public function getLastError() {
return $this->lastError;
}
public function getSession() {
return $this->session;
}
}
?>




...to use it do something like this...
 index.php
<?php
require __DIR__ . '/vendor/autoload.php';
require_once 'SessionWrapper.php'
$sessionW = (new SessionWrapper())->initSession(array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'user' => 'mysql_user',
'password' => 'mysql_password',
'dbname' => 'mysql_database'
));
$session = $sessionW->getSession();
// your code here
?>


The code for the SessionWrapper class is quite boiler plate Jackalope code. The repository initialisation happens on lines 33-39. There is some error handling thrown in and that's about it. No need to mess around with manual repository initialisation!

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.