Set up EntityManager and specify Object schema
This commit is contained in:
parent
05035c230f
commit
d9c9d1d17b
@ -1,9 +1,55 @@
|
||||
<?php
|
||||
require __DIR__ . '../vendor/autoload.php';
|
||||
require_once __DIR__ . '../vendor/autoload.php';
|
||||
|
||||
use Doctrine\ORM\Tools\Setup;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use ActivityPub\Database\PrefixNamingStrategy;
|
||||
|
||||
namespace ActivityPub;
|
||||
|
||||
class ActivityPub {
|
||||
class ActivityPub
|
||||
{
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* Constructs a new ActivityPub instance
|
||||
*
|
||||
* @param array $opts Array of options. Valid keys are
|
||||
* 'dbuser', 'dbpass', 'dbname', 'dbprefix', and 'isDevMode'.
|
||||
*/
|
||||
public function __construct( $opts )
|
||||
{
|
||||
$this->validateOptions( $opts );
|
||||
$defaults = array(
|
||||
'isDevMode' => false,
|
||||
'dbprefix' => '',
|
||||
);
|
||||
$options = array_merge( $defaults, $opts );
|
||||
$dbConfig = Setup::createAnnotationMetadataConfiguration(
|
||||
array( __DIR__ . '/src/Entities' ), $options['isDevMode']
|
||||
);
|
||||
$namingStrategy = new PrefixNamingStrategy( $options['dbprefix'] );
|
||||
$dbConfig->setNamingStrategy( $namingStrategy );
|
||||
$dbParams = array(
|
||||
'driver' => 'pdo_mysql',
|
||||
'user' => $options['dbuser'],
|
||||
'password' => $options['dbpass'],
|
||||
'dbname' => $options['dbname'],
|
||||
);
|
||||
$this->entityManager = EntityManager::create( $dbParams, $dbConfig );
|
||||
// TODO create tables
|
||||
}
|
||||
|
||||
private function validateOptions( $opts )
|
||||
{
|
||||
$required = array( 'dbuser', 'dbpass', 'dbname' );
|
||||
$actual = array_keys( $opts );
|
||||
$missing = array_diff( $required, $actual );
|
||||
if ( count( $missing ) > 0 ) {
|
||||
throw new InvalidArgumentException(
|
||||
'Missing required options: ' . print_r( $missing, t )
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
47
src/Database/PrefixNamingStrategy.php
Normal file
47
src/Database/PrefixNamingStrategy.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace ActivityPub\Database;
|
||||
|
||||
use Doctrine\ORM\Mapping\NamingStrategy;
|
||||
|
||||
class PrefixNamingStrategy implements NamingStrategy
|
||||
{
|
||||
protected $prefix;
|
||||
|
||||
public function __construct( $prefix )
|
||||
{
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
public function classToTableName($className)
|
||||
{
|
||||
return $this->prefix . substr($className, strrpos($className, '\\') + 1);
|
||||
}
|
||||
|
||||
public function propertyToColumnName($propertyName)
|
||||
{
|
||||
return $propertyName;
|
||||
}
|
||||
|
||||
public function referenceColumnName()
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
|
||||
public function joinColumnName($propertyName, $className = null)
|
||||
{
|
||||
return $propertyName . '_' . $this->referenceColumnName();
|
||||
}
|
||||
|
||||
public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
|
||||
{
|
||||
return strtolower($this->classToTableName($sourceEntity) . '_' .
|
||||
$this->classToTableName($targetEntity));
|
||||
}
|
||||
|
||||
public function joinKeyColumnName($entityName, $referencedColumnName = null)
|
||||
{
|
||||
return strtolower($this->classToTableName($entityName) . '_' .
|
||||
($referencedColumnName ?: $this->referenceColumnName()));
|
||||
}
|
||||
}
|
||||
?>
|
39
src/Entities/Object.php
Normal file
39
src/Entities/Object.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
namespace ActivityPub\Entities;
|
||||
|
||||
/**
|
||||
* Represents an ActivityPub JSON-LD object
|
||||
* @Entity @Table(name="objects")
|
||||
*/
|
||||
class ObjectEntity
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
* @Id
|
||||
* @Column(type="integer")
|
||||
* @GeneratedValue
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @Column(type="string")
|
||||
*/
|
||||
protected $json;
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getJson()
|
||||
{
|
||||
return $this->json;
|
||||
}
|
||||
|
||||
public function setJson($json)
|
||||
{
|
||||
$this->json = $json;
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Reference in New Issue
Block a user