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