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
StrFunc() Online
300 George St Blogumentary
Guru JSON-RPC Tester
Extrudifier Object Designer

Recent Blog Posts

Where to find the last item (Robbie's lost lyrics) in GravityFalls Lake level

Detecting print requests to print contents of a DIV with JavaScript

Using Google DFP with AdSense on responsive pages

How to change the email subject line in Gmail

Force PHP GD library to load JPEG images that have invalid data

How to uninstall Samsung Smart Switch from your Mac

Facebook adds weather forecasts but forgets to adjust them to your time zone

No sound or microphone when answering calls on an iPhone

Replacing the standard Odroid XU4 fan and heatsink

PHP PDO doesn't work binding multiple IN statement values

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