Igor's Blog

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

, , ,
About — I'm an enthusiastic software engineer and consultant interested in many fields including J2EE, programming, electronics, 3D printing, video games, wood working and gardening.
See my Resume for more information.
The views expressed in this blog are my own and not those of my employer.
comments powered by Disqus
My other posts you may like...
Programming, DIY, Games, Hacks, Tech and more.
Follow me on...
Current and Past Projects
See my Resume

Subscribe


RSS Feed

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer

Recent Blog Posts

Using Jersey 2.x as a shared library on WebLogic 12.1.2

Google DFP with AdSense fallback is causing infinite scroll pages to go haywire

Workaround for Mobile Safari scrollTop() not updating during scroll

Skipping execution of Maven plugins that do not have a native skip option

Unmarshalling an XML fragment representing a JAXB object without XmlRootElement

Injecting a ContainerRequestContext into a Jersey entity provider class

Obfuscating PHP source code with Maven and YAK Pro PO

Making use of corner shelves with easy to make slide out draws

Octotree breaks GitLab Issue Boards

Compiling the FHIR schema with XJC

Recent Galleries

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

Good Friday Electronics fun Easter Bunny LED PCB Kit IBEABU-01.0

Top Categories

Blogs I follow

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth

Friends

RAWS Parts Online
Alpha Dimensions Hosting
Kristensen Photography
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Follow me on... 
     
...or subscribe for updates!

Don't show this again