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

Please leave your comments or feedback below!
comments powered by Disqus
Other posts you may like...
Hi! You can search my blog here ⤵
Or browse the recent top tags...

Recent Blog Posts

WebLogic Maven Plugin - How to fix the MojoExecutionException: The artifact location was not specified

jPhotoFrame version 0.4 released with a whole new layout engine

Upcycling a couple of old broken lamps to create something amazing

A custom exception mapper and writer for a RESTful JAX-RS Jersey service

How to fix Plex error - Sorry there was a problem playing this item

Jersey JAX-RS filters and interceptors execution order for a POST request

Fix your Mac - users not showing on the macOS login screen when FileVault is enabled

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Writing a custom MessageBodyReader to process POST body data with Jersey

How to make Skype for Business responsive again on macOS

Recent Galleries

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Tiny Arcade revision 6 kit assembly and decal application

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 1 - introduction and case disassembly

Building a custom Atari Lynx game box storage shelf unit in a day

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity