From d9c9d1d17b9d94082a1b29a0bd65ac08b1f77444 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Fri, 16 Nov 2018 06:52:17 -0500 Subject: [PATCH] Set up EntityManager and specify Object schema --- src/ActivityPub.php | 50 +++++++++++++++++++++++++-- src/Database/PrefixNamingStrategy.php | 47 +++++++++++++++++++++++++ src/Entities/Object.php | 39 +++++++++++++++++++++ 3 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 src/Database/PrefixNamingStrategy.php create mode 100644 src/Entities/Object.php diff --git a/src/ActivityPub.php b/src/ActivityPub.php index 283bfd9..01bb222 100644 --- a/src/ActivityPub.php +++ b/src/ActivityPub.php @@ -1,9 +1,55 @@ 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 ) + ); + } + } } ?> diff --git a/src/Database/PrefixNamingStrategy.php b/src/Database/PrefixNamingStrategy.php new file mode 100644 index 0000000..114c847 --- /dev/null +++ b/src/Database/PrefixNamingStrategy.php @@ -0,0 +1,47 @@ +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())); + } +} +?> diff --git a/src/Entities/Object.php b/src/Entities/Object.php new file mode 100644 index 0000000..e79c855 --- /dev/null +++ b/src/Entities/Object.php @@ -0,0 +1,39 @@ +id; + } + + public function getJson() + { + return $this->json; + } + + public function setJson($json) + { + $this->json = $json; + } +} +?>