Page MenuHomeDevCentral

D3843.diff
No OneTemporary

D3843.diff

diff --git a/workspaces/src/Engines/Apps/Application.php b/workspaces/src/Engines/Apps/Application.php
--- a/workspaces/src/Engines/Apps/Application.php
+++ b/workspaces/src/Engines/Apps/Application.php
@@ -17,12 +17,11 @@
namespace Waystone\Workspaces\Engines\Apps;
+use Waystone\Workspaces\Engines\Collection\Collection;
use Waystone\Workspaces\Engines\Controller\Controller;
use Waystone\Workspaces\Engines\Errors\ErrorHandling;
use Waystone\Workspaces\Engines\Workspaces\WorkspaceConfiguration;
-use Collection;
-
use Exception;
/**
diff --git a/workspaces/src/includes/collection/Collection.php b/workspaces/src/Engines/Collection/Collection.php
rename from workspaces/src/includes/collection/Collection.php
rename to workspaces/src/Engines/Collection/Collection.php
--- a/workspaces/src/includes/collection/Collection.php
+++ b/workspaces/src/Engines/Collection/Collection.php
@@ -15,12 +15,18 @@
* @filesource
*/
+namespace Waystone\Workspaces\Engines\Collection;
+
+use Exception;
+use Traversable;
+
/**
* Collection class
*
* This abstract class represents a collection of documents
*/
abstract class Collection {
+
///
/// Common properties
///
@@ -43,8 +49,11 @@
* Loads a new instance of the collection
*
* @param string $id The collection identifiant
- * @param string $documentType The type, inheriting from CollectionDocumt to use for ollection's documents
- * @return Collection The collection, of the type specified in the storage configuration
+ * @param string $documentType The type, inheriting from CollectionDocumt
+ * to use for ollection's documents
+ *
+ * @return Collection The collection, of the type specified in the storage
+ * configuration
*/
public static function load ($id, $documentType = null) {
global $Config;
@@ -64,6 +73,7 @@
if ($documentType !== null) {
$instance->documentType = $documentType;
}
+
return $instance;
}
@@ -76,6 +86,7 @@
* Adds a document to the collection
*
* @param CollectionDocument $document The document to add
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public abstract function add (CollectionDocument &$document);
@@ -84,6 +95,7 @@
* Deletes a document from the collection
*
* @param string $documentId The identifiant of the document to delete
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public abstract function delete ($documentId);
@@ -92,6 +104,7 @@
* Determines if a document exists
*
* @param CollectionDocument $document The document to check
+ *
* @return boolean true if the document exists; otherwise, false.
*/
public abstract function exists (CollectionDocument $document);
@@ -100,6 +113,7 @@
* Updates a document in the collection
*
* @param CollectionDocument $document The document to update
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public abstract function update (CollectionDocument &$document);
@@ -122,7 +136,8 @@
/**
* Gets all the documents from the collection
*
- * @return Traversable An iterator to the documents, each item an instance of CollectionDocument
+ * @return Traversable An iterator to the documents, each item an instance
+ * of CollectionDocument
*/
public abstract function getAll ();
@@ -130,6 +145,7 @@
* Adds or updates a document in the collection
*
* @param CollectionDocument $document The document to set
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public function set (CollectionDocument &$document) {
diff --git a/workspaces/src/includes/collection/CollectionDocument.php b/workspaces/src/Engines/Collection/CollectionDocument.php
rename from workspaces/src/includes/collection/CollectionDocument.php
rename to workspaces/src/Engines/Collection/CollectionDocument.php
--- a/workspaces/src/includes/collection/CollectionDocument.php
+++ b/workspaces/src/Engines/Collection/CollectionDocument.php
@@ -15,14 +15,19 @@
* @filesource
*/
+namespace Waystone\Workspaces\Engines\Collection;
+
use Waystone\Workspaces\Engines\Serialization\ObjectDeserializable;
+use JsonSerializable;
+
/**
* Collection document class
*
* This class represents a document, inside a collection.
*/
class CollectionDocument implements JsonSerializable, ObjectDeserializable {
+
/**
* @var string The document identifier
*/
@@ -30,10 +35,8 @@
/**
* Specifies data which should be serialized to JSON
- *
- * @return moxed data which can be serialized by json_encode()
*/
- public function jsonSerialize () {
+ public function jsonSerialize () : static {
return $this;
}
@@ -41,9 +44,10 @@
* Loads a CollectionDocument instance from a generic object.
*
* @param object $data The object to deserialize
- * @return object The deserialized instance
+ *
+ * @return self The deserialized instance
*/
- public static function loadFromObject ($data) {
+ public static function loadFromObject (object $data) : static {
$instance = new static;
foreach ($data as $key => $value) {
if ($key == '_id') {
@@ -54,6 +58,7 @@
}
$instance->$key = $value;
}
+
return $instance;
}
}
diff --git a/workspaces/src/includes/collection/FilesCollection.php b/workspaces/src/Engines/Collection/FilesCollection.php
rename from workspaces/src/includes/collection/FilesCollection.php
rename to workspaces/src/Engines/Collection/FilesCollection.php
--- a/workspaces/src/includes/collection/FilesCollection.php
+++ b/workspaces/src/Engines/Collection/FilesCollection.php
@@ -15,28 +15,38 @@
* @filesource
*/
+namespace Waystone\Workspaces\Engines\Collection;
+
+use Exception;
+use Generator;
+
/**
* Files Collection class
*
* This class represents a collection of documents, stored on the filesystem.
*/
class FilesCollection extends Collection {
+
///
/// Helper methods
///
/**
- * Gets the path to the folder where the specified collection documents are stored.
+ * Gets the path to the folder where the specified collection documents are
+ * stored.
*
* @param string $collectionId The collection identifiant
+ *
* @return string The path to the specified collection folder
*/
public static function getCollectionPath ($collectionId) {
global $Config;
- if (!array_key_exists('DocumentStorage', $Config) || !array_key_exists('Path', $Config['DocumentStorage'])) {
+ if (!array_key_exists('DocumentStorage', $Config)
+ || !array_key_exists('Path', $Config['DocumentStorage'])) {
throw new Exception("Configuration parameter missing: \$Config['DocumentStorage']['Path']. Expected value for this parameter is the path to the collections folders.");
}
- $path = $Config['DocumentStorage']['Path'] . DIRECTORY_SEPARATOR . $collectionId;
+ $path = $Config['DocumentStorage']['Path'] . DIRECTORY_SEPARATOR
+ . $collectionId;
//Ensure directory exists. If not, creates it or throws an exception
if (!file_exists($path) && !mkdir($path, 0700, true)) {
@@ -50,12 +60,14 @@
* Gets the path to the file where the specified document is stored.
*
* @param string $documentId The document identifiant
+ *
* @return string The path to the specified document file
*/
public function getDocumentPath ($documentId) {
- return static::getCollectionPath($this->id) . DIRECTORY_SEPARATOR . $documentId . '.json';
+ return static::getCollectionPath($this->id) . DIRECTORY_SEPARATOR
+ . $documentId . '.json';
}
-
+
/**
* Gets the path to the folder where the current collection is stored.
*
@@ -86,6 +98,7 @@
* Adds a document to the collection
*
* @param CollectionDocument $document The document to add
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public function add (CollectionDocument &$document) {
@@ -107,6 +120,7 @@
* Deletes a document from the collection
*
* @param string $documentId The identifiant of the document to delete
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public function delete ($documentId) {
@@ -118,10 +132,12 @@
* Determines if a document exists
*
* @param CollectionDocument $document The document to check
+ *
* @return boolean true if the document exists; otherwise, false.
*/
public function exists (CollectionDocument $document) {
$filename = $this->getDocumentPath($document->id);
+
return file_exists($filename);
}
@@ -129,17 +145,20 @@
* Updates a document in the collection
*
* @param CollectionDocument $document The document to update
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public function update (CollectionDocument &$document) {
if ($document->id === '') {
$document->id = uniqid();
- user_error("An ID were expected for an update operation, but not provided. We'll use $document->id.", E_USER_WARNING);
+ user_error("An ID were expected for an update operation, but not provided. We'll use $document->id.",
+ E_USER_WARNING);
}
$filename = $this->getDocumentPath($document->id);
if (!file_exists($filename)) {
- user_error("File $filename doesn't exist. The update operation has became an insert one.", E_USER_WARNING);
+ user_error("File $filename doesn't exist. The update operation has became an insert one.",
+ E_USER_WARNING);
}
$data = json_encode($document);
@@ -161,6 +180,7 @@
$filename = $this->getDocumentPath($documentId);
$data = json_decode(file_get_contents($filename));
+
return $type::loadFromObject($data);
}
@@ -168,6 +188,7 @@
* Adds or updates a document in the collection
*
* @param CollectionDocument $document The document to set
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public function set (CollectionDocument &$document) {
@@ -193,13 +214,15 @@
$count++;
}
}
+
return $count;
}
/**
* Gets all the documents from the collection
*
- * @return Generator An iterator to the documents, each item an instance of CollectionDocument
+ * @return Generator An iterator to the documents, each item an instance of
+ * CollectionDocument
*/
public function getAll () {
$dir = $this->getCurrentCollectionPath();
@@ -226,6 +249,7 @@
$documents[] = get_filename($file);
}
}
+
return $documents;
}
}
diff --git a/workspaces/src/includes/collection/MongoDBCollection.php b/workspaces/src/Engines/Collection/MongoDBCollection.php
rename from workspaces/src/includes/collection/MongoDBCollection.php
rename to workspaces/src/Engines/Collection/MongoDBCollection.php
--- a/workspaces/src/includes/collection/MongoDBCollection.php
+++ b/workspaces/src/Engines/Collection/MongoDBCollection.php
@@ -15,8 +15,14 @@
* @filesource
*/
-define('MONGO_DEFAULT_HOST', 'localhost');
-define('MONGO_DEFAULT_PORT', 27017);
+namespace Waystone\Workspaces\Engines\Collection;
+
+use Exception;
+use Iterator;
+use MongoClient;
+
+define('MONGO_DEFAULT_HOST', 'localhost');
+define('MONGO_DEFAULT_PORT', 27017);
define('MONGO_DEFAULT_DATABASE', 'obsidian');
/**
@@ -25,6 +31,7 @@
* This class represents a collection of documents, stored on MongoDB.
*/
class MongoDBCollection extends Collection {
+
/**
* @var MongoCollection the Mongo collection
*/
@@ -35,13 +42,15 @@
///
/**
- * @var MongoClient The mongo client to the database the collection is hosted
+ * @var MongoClient The mongo client to the database the collection is
+ * hosted
*/
public static $mongoClient = null;
/**
- * Gets the existing MongoClient instance, or if not available, initializes one.
+ * Gets the existing MongoClient instance, or if not available, initializes
+ * one.
*
* @return MongoClient The MongoClient instance
*/
@@ -49,6 +58,7 @@
if (self::$mongoClient === null) {
self::$mongoClient = self::initializeMongoClient();
}
+
return self::$mongoClient;
}
@@ -65,10 +75,11 @@
global $Config;
//Protocol
- $connectionString = 'mongodb://';
+ $connectionString = 'mongodb://';
//Host
- if (isset($Config['DocumentStorage']) && array_key_exists('Host', $Config['DocumentStorage'])) {
+ if (isset($Config['DocumentStorage'])
+ && array_key_exists('Host', $Config['DocumentStorage'])) {
$connectionString .= $Config['DocumentStorage']['Host'];
} else {
$connectionString .= MONGO_DEFAULT_HOST;
@@ -76,7 +87,8 @@
//Port
$connectionString .= ':';
- if (isset($Config['DocumentStorage']) && array_key_exists('Port', $Config['DocumentStorage'])) {
+ if (isset($Config['DocumentStorage'])
+ && array_key_exists('Port', $Config['DocumentStorage'])) {
$connectionString .= $Config['DocumentStorage']['Port'];
} else {
$connectionString .= MONGO_DEFAULT_PORT;
@@ -93,16 +105,18 @@
public static function initializeMongoClient () {
global $Config;
- $connectionString = self::getConnectionString();
- if (isset($Config['DocumentStorage']) && array_key_exists('SSL', $Config['DocumentStorage']) && $Config['DocumentStorage']['SSL'] !== null) {
+ $connectionString = self::getConnectionString();
+ if (isset($Config['DocumentStorage'])
+ && array_key_exists('SSL', $Config['DocumentStorage'])
+ && $Config['DocumentStorage']['SSL'] !== null) {
$context = stream_context_create(
- [ 'ssl' => $Config['DocumentStorage']['SSL'] ]
+ ['ssl' => $Config['DocumentStorage']['SSL']],
);
$m = new MongoClient(
$connectionString,
- [ 'ssl' => true ],
- [ 'context' => $context ]
+ ['ssl' => true],
+ ['context' => $context],
);
} else {
$m = new MongoClient($connectionString);
@@ -117,7 +131,8 @@
public static function getDatabase () {
global $Config;
- if (isset($Config['DocumentStorage']) && array_key_exists('Database', $Config['DocumentStorage'])) {
+ if (isset($Config['DocumentStorage'])
+ && array_key_exists('Database', $Config['DocumentStorage'])) {
return $Config['DocumentStorage']['Database'];
}
@@ -144,9 +159,11 @@
///
/**
- * Gets an associative array with document collection properties as key and values.
+ * Gets an associative array with document collection properties as key and
+ * values.
*
* @param CollectionDocument $document The document
+ *
* @return array The array representation of the document
*/
public function getArrayFromDocument (CollectionDocument $document) {
@@ -159,6 +176,7 @@
$array[$key] = $value;
}
}
+
return $array;
}
@@ -170,6 +188,7 @@
* Adds a document to the collection
*
* @param CollectionDocument $document The document to add
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public function add (CollectionDocument &$document) {
@@ -182,11 +201,12 @@
* Deletes a document from the collection
*
* @param string $documentId The identifiant of the document to delete
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public function delete ($documentId) {
$this->mongoCollection->remove(
- [ '_id' => $documentId ]
+ ['_id' => $documentId],
);
}
@@ -194,14 +214,16 @@
* Determines if a document exists
*
* @param CollectionDocument $document The document to check
+ *
* @return boolean true if the document exists; otherwise, false.
*/
public function exists (CollectionDocument $document) {
//According https://blog.serverdensity.com/checking-if-a-document-exists-mongodb-slow-findone-vs-find/
//this is more efficient to use find than findOne() to determine existence.
$cursor = $this->mongoCollection->find(
- [ '_id' => $document->id ]
+ ['_id' => $document->id],
)->limit(1);
+
return $cursor->hasNext();
}
@@ -209,13 +231,14 @@
* Updates a document in the collection
*
* @param CollectionDocument $document The document to update
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public function update (CollectionDocument &$document) {
$object = static::getArrayFromDocument($document);;
$this->mongoCollection->update(
- [ '_id' => $document->id ],
- $object
+ ['_id' => $document->id],
+ $object,
);
}
@@ -227,19 +250,21 @@
*/
public function get ($documentId) {
$data = $this->mongoCollection->findOne(
- [ '_id' => $documentId ]
+ ['_id' => $documentId],
);
+
return $this->getDocumentFromArray($data);
}
/**
* Gets a document of the relevant collection documents type from an array.
*/
- public function getDocumentFromArray($documentArray) {
+ public function getDocumentFromArray ($documentArray) {
$type = $this->documentType;
if (!class_exists($type)) {
throw new Exception("Can't create an instance of $type. If the class exists, did you register a SPL autoloader or updated includes/autoload.php?");
}
+
return $type::loadFromObject($documentArray);
}
@@ -255,7 +280,8 @@
/**
* Gets all the documents from the collection
*
- * @return Iterator An iterator to the documents, each item an instance of CollectionDocument
+ * @return Iterator An iterator to the documents, each item an instance of
+ * CollectionDocument
*/
public function getAll () {
return new MongoDBCollectionIterator($this);
diff --git a/workspaces/src/includes/collection/MongoDBCollectionIterator.php b/workspaces/src/Engines/Collection/MongoDBCollectionIterator.php
rename from workspaces/src/includes/collection/MongoDBCollectionIterator.php
rename to workspaces/src/Engines/Collection/MongoDBCollectionIterator.php
--- a/workspaces/src/includes/collection/MongoDBCollectionIterator.php
+++ b/workspaces/src/Engines/Collection/MongoDBCollectionIterator.php
@@ -15,17 +15,24 @@
* @filesource
*/
+namespace Waystone\Workspaces\Engines\Collection;
+
+use Iterator;
+use MongoCursor;
+
/**
* Iterator for MongoDBCollection::getAll()
*/
class MongoDBCollectionIterator implements Iterator {
+
/**
* @var MongoCursor The MongoDB cursor
*/
private $cursor;
/**
- * @var MongoDBCollection The collection attached to the current iterator instance
+ * @var MongoDBCollection The collection attached to the current iterator
+ * instance
*/
private $collection;
@@ -35,7 +42,10 @@
* @param MongoDBCollection $collection The collection to iterate
* @param ?MongoCursor $cursor The cursor to the results [optional]
*/
- public function __construct (MongoDBCollection $collection, ?MongoCursor $cursor = null) {
+ public function __construct (
+ MongoDBCollection $collection,
+ ?MongoCursor $cursor = null,
+ ) {
$this->collection = $collection;
if ($cursor === null) {
$this->cursor = $collection->mongoCollection->find();
@@ -51,7 +61,7 @@
*/
public function current () {
return $this->collection->getDocumentFromArray(
- $this->cursor->current()
+ $this->cursor->current(),
);
}
@@ -67,7 +77,7 @@
/**
* Moves forward to next element
*/
- public function next () {
+ public function next () {
$this->cursor->next();
}
diff --git a/workspaces/src/includes/collection/MySQLCollection.php b/workspaces/src/Engines/Collection/MySQLCollection.php
rename from workspaces/src/includes/collection/MySQLCollection.php
rename to workspaces/src/Engines/Collection/MySQLCollection.php
--- a/workspaces/src/includes/collection/MySQLCollection.php
+++ b/workspaces/src/Engines/Collection/MySQLCollection.php
@@ -15,28 +15,38 @@
* @filesource
*/
+namespace Waystone\Workspaces\Engines\Collection;
+
use Waystone\Workspaces\Engines\Framework\Context;
+use Exception;
+use InvalidArgumentException;
+use Iterator;
+
/**
* MySQL Collection class
*
* This class represents a collection of documents, stored on MySQL.
*/
class MySQLCollection extends SQLCollection {
+
///
/// Singleton pattern to get the MySQLDatabase instance
///
/**
- * @var MySQLDatabase The mongo client to the database the collection is hosted
+ * @var MySQLDatabase The mongo client to the database the collection is
+ * hosted
*/
public static $client = null;
/**
- * Gets the existing MySQLDatabase instance, or if not available, initializes one.
+ * Gets the existing MySQLDatabase instance, or if not available,
+ * initializes one.
*
* @param Context $context
+ *
* @return MySQLDatabase The MySQLDatabase instance
*/
public static function getCurrentSiteDatabaseClient () {
@@ -48,6 +58,7 @@
throw new InvalidArgumentException("The MySQLDatabase driver is intended to be used when your main database product is MySQL. We recommend whether you pick the same engine for collections and other db use, whether you use a key/store storage solution for collections, like MongoDB.");
}
}
+
return self::$client;
}
@@ -60,7 +71,11 @@
*
* @param string $id the collection identifiant
*/
- public function __construct ($id, ?MySQLDatabase $client = null, $table = '') {
+ public function __construct (
+ $id,
+ ?MySQLDatabase $client = null,
+ $table = '',
+ ) {
global $Config;
if ($client === null) {
@@ -70,7 +85,8 @@
}
if ($table == '') {
- if (!array_key_exists('DocumentStorage', $Config) || !array_key_exists('Table', $Config['DocumentStorage'])) {
+ if (!array_key_exists('DocumentStorage', $Config)
+ || !array_key_exists('Table', $Config['DocumentStorage'])) {
throw new Exception("Configuration parameter missing: \$Config['DocumentStorage']['Table']. Expected value for this parameter is the table to store the collections documents.");
}
$this->table = $Config['DocumentStorage']['Table'];
@@ -90,7 +106,8 @@
* Initializes collections table
*/
protected function initializeCollectionsTable () {
- if (defined('COLLECTIONS_MYSQL_DATABASE_READY') && COLLECTIONS_MYSQL_DATABASE_READY) {
+ if (defined('COLLECTIONS_MYSQL_DATABASE_READY')
+ && COLLECTIONS_MYSQL_DATABASE_READY) {
return;
}
@@ -100,7 +117,7 @@
document_id VARCHAR(255),
document_value BLOB,
PRIMARY KEY (collection_id, document_id)
- );"
+ );",
);
define('COLLECTIONS_MYSQL_DATABASE_READY', true);
@@ -114,7 +131,10 @@
* Executes a SQL query
*
* @param string $sql The SQL query
- * @return mixed If the query doesn't return any result, null. If the query return a row with one field, the scalar value. Otherwise, an associative array, the fields as keys, the row as values.
+ *
+ * @return mixed If the query doesn't return any result, null. If the query
+ * return a row with one field, the scalar value. Otherwise, an
+ * associative array, the fields as keys, the row as values.
*/
public function query ($sql) {
if ($sql == "") {
@@ -141,6 +161,7 @@
* Escapes the SQL string
*
* @param string $value The value to escape
+ *
* @return string The escaped value
*/
public function escape ($value) {
@@ -150,13 +171,15 @@
/**
* Gets all the documents from the collection
*
- * @return Iterator An iterator to the documents, each item an instance of CollectionDocument
+ * @return Iterator An iterator to the documents, each item an instance of
+ * CollectionDocument
*/
public function getAll () {
$db = self::$client;
$collectionId = $this->escape($this->id);
- $sql = "SELECT * FROM $this->table WHERE collection_id = '$collectionId'";
+ $sql =
+ "SELECT * FROM $this->table WHERE collection_id = '$collectionId'";
if (!$result = $db->query($sql, MYSQL_ASSOC)) {
throw new Exception("Can't get each collection documents.");
}
diff --git a/workspaces/src/includes/collection/SQLCollection.php b/workspaces/src/Engines/Collection/SQLCollection.php
rename from workspaces/src/includes/collection/SQLCollection.php
rename to workspaces/src/Engines/Collection/SQLCollection.php
--- a/workspaces/src/includes/collection/SQLCollection.php
+++ b/workspaces/src/Engines/Collection/SQLCollection.php
@@ -15,10 +15,16 @@
* @filesource
*/
+namespace Waystone\Workspaces\Engines\Collection;
+
+use Exception;
+
/**
- * Abstract class with SQL standard implementation of CRUD features for collections using a SQL database.
+ * Abstract class with SQL standard implementation of CRUD features for
+ * collections using a SQL database.
*/
abstract class SQLCollection extends Collection {
+
/**
* @var string The SQL collections table
*/
@@ -28,7 +34,10 @@
* Executes a SQL query
*
* @param string $sql The SQL query
- * @return mixed If the query doesn't return any null, nothing. If the query return a row with one field, the scalar value. Otherwise, an associative array, the fields as keys, the row as values.
+ *
+ * @return mixed If the query doesn't return any null, nothing. If the
+ * query return a row with one field, the scalar value. Otherwise, an
+ * associative array, the fields as keys, the row as values.
*/
public abstract function query ($sql);
@@ -36,6 +45,7 @@
* Escapes the SQL string
*
* @param string $value The value to escape
+ *
* @return string The escaped value
*/
public abstract function escape ($value);
@@ -44,6 +54,7 @@
* Adds a document to the collection
*
* @param CollectionDocument $document The document to add
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public function add (CollectionDocument &$document) {
@@ -56,8 +67,8 @@
$value = $this->escape(json_encode($document));
$sql = "INSERT INTO $this->table "
- . "(collection_id, document_id, document_value) VALUES "
- . "('$collectionId', '$documentId', '$value')";
+ . "(collection_id, document_id, document_value) VALUES "
+ . "('$collectionId', '$documentId', '$value')";
$this->query($sql);
}
@@ -65,13 +76,15 @@
* Deletes a document from the collection
*
* @param string $documentId The identifiant of the document to delete
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public function delete ($documentId) {
$collectionId = $this->escape($this->id);
$documentId = $this->escape($documentId);
- $sql = "DELETE FROM $this->table WHERE collection_id = '$collectionId' AND document_id = '$documentId'";
+ $sql =
+ "DELETE FROM $this->table WHERE collection_id = '$collectionId' AND document_id = '$documentId'";
$this->query($sql);
}
@@ -79,13 +92,16 @@
* Determines if a document exists
*
* @param CollectionDocument $document The document to check
+ *
* @return boolean true if the document exists; otherwise, false.
*/
public function exists (CollectionDocument $document) {
$collectionId = $this->escape($this->id);
$documentId = $this->escape($document->id);
- $sql = "SELECT count(*) FROM $this->table WHERE collection_id = '$collectionId' AND document_id = '$documentId'";
+ $sql =
+ "SELECT count(*) FROM $this->table WHERE collection_id = '$collectionId' AND document_id = '$documentId'";
+
return $this->query($sql) == 1;
}
@@ -93,6 +109,7 @@
* Updates a document in the collection
*
* @param CollectionDocument $document The document to update
+ *
* @return boolean true if the operation succeeded; otherwise, false.
*/
public function update (CollectionDocument &$document) {
@@ -100,7 +117,8 @@
$documentId = $this->escape($document->id);
$value = $this->escape(json_encode($document));
- $sql = "UPDATE $this->table SET document_value = '$value' WHERE collection_id = '$collectionId' AND document_id = '$documentId'";
+ $sql =
+ "UPDATE $this->table SET document_value = '$value' WHERE collection_id = '$collectionId' AND document_id = '$documentId'";
$this->query($sql);
}
@@ -110,7 +128,7 @@
* @param string $documentId The identifiant of the document to get
* @param CollectionDocument $document The document
*/
- public function get ($documentId){
+ public function get ($documentId) {
$type = $this->documentType;
if (!class_exists($type)) {
throw new Exception("Can't create an instance of $type. If the class exists, did you registered a SPL autoloader or updated includes/autoload.php?");
@@ -119,7 +137,8 @@
$collectionId = $this->escape($this->id);
$documentId = $this->escape($documentId);
- $sql = "SELECT document_value FROM $this->table WHERE collection_id = '$collectionId' AND document_id = '$documentId'";
+ $sql =
+ "SELECT document_value FROM $this->table WHERE collection_id = '$collectionId' AND document_id = '$documentId'";
$data = $this->query($sql);
$data = json_decode($data);
@@ -133,7 +152,9 @@
*/
public function count () {
$collectionId = $this->escape($this->id);
- $sql = "SELECT count(*) FROM $this->table WHERE collection_id = '$collectionId'";
+ $sql =
+ "SELECT count(*) FROM $this->table WHERE collection_id = '$collectionId'";
+
return $this->query($sql);
}
}
diff --git a/workspaces/src/includes/collection/SQLiteCollection.php b/workspaces/src/Engines/Collection/SQLiteCollection.php
rename from workspaces/src/includes/collection/SQLiteCollection.php
rename to workspaces/src/Engines/Collection/SQLiteCollection.php
--- a/workspaces/src/includes/collection/SQLiteCollection.php
+++ b/workspaces/src/Engines/Collection/SQLiteCollection.php
@@ -15,6 +15,10 @@
* @filesource
*/
+namespace Waystone\Workspaces\Engines\Collection;
+
+use SQLite3;
+
/**
* SQLite Collection class
*
@@ -41,6 +45,7 @@
if (self::$client === null) {
self::$client = self::initializeClient();
}
+
return self::$client;
}
@@ -51,7 +56,8 @@
*/
public static function initializeClient () {
global $Config;
- if (!array_key_exists('DocumentStorage', $Config) || !array_key_exists('File', $Config['DocumentStorage'])) {
+ if (!array_key_exists('DocumentStorage', $Config)
+ || !array_key_exists('File', $Config['DocumentStorage'])) {
throw new Exception("Configuration parameter missing: \$Config['DocumentStorage']['File']. Expected value for this parameter is the path to the SQLite database file.");
}
@@ -81,7 +87,8 @@
* Initializes collections table
*/
protected function initializeCollectionsTable () {
- if (defined('COLLECTIONS_SQLITE_DATABASE_READY') && COLLECTIONS_SQLITE_DATABASE_READY) {
+ if (defined('COLLECTIONS_SQLITE_DATABASE_READY')
+ && COLLECTIONS_SQLITE_DATABASE_READY) {
return;
}
@@ -92,7 +99,7 @@
document_id TEXT,
document_value BLOB,
PRIMARY KEY (collection_id, document_id)
- );"
+ );",
);
define('COLLECTIONS_SQLITE_DATABASE_READY', true);
@@ -106,10 +113,12 @@
* Determines if the SQL query is a statement
*
* @return boolean true is a SELECT one; otherwise, false.
- * @todo To use this outside the Collection scope, adds the other cases where a query is indicated.
+ * @todo To use this outside the Collection scope, adds the other cases
+ * where a query is indicated.
*/
public static function isStatement ($sql) {
$instruction = strtoupper(strstr($sql, ' ', true));
+
return $instruction != "SELECT" && $instruction != "PRAGMA";
}
@@ -117,7 +126,10 @@
* Executes a SQL query
*
* @param string $sql The SQL query
- * @return mixed If the query doesn't return any result, null. If the query return a row with one field, the scalar value. Otherwise, an associative array, the fields as keys, the row as values.
+ *
+ * @return mixed If the query doesn't return any result, null. If the query
+ * return a row with one field, the scalar value. Otherwise, an
+ * associative array, the fields as keys, the row as values.
*/
public function query ($sql) {
$client = static::getClient();
@@ -126,9 +138,10 @@
if (!$client->exec($sql)) {
throw new Exception(
"Can't execute collection query. "
- . $client->lastErrorMsg()
+ . $client->lastErrorMsg(),
);
}
+
return null;
}
@@ -139,7 +152,7 @@
if ($result === false) {
throw new Exception(
"Can't execute collection query. "
- . $client->lastErrorMsg()
+ . $client->lastErrorMsg(),
);
}
@@ -158,6 +171,7 @@
* Escapes the SQL string
*
* @param string $value The value to escape
+ *
* @return string The escaped value
*/
public function escape ($value) {
@@ -167,11 +181,13 @@
/**
* Gets all the documents from the collection
*
- * @return Iterator An iterator to the documents, each item an instance of CollectionDocument
+ * @return Iterator An iterator to the documents, each item an instance of
+ * CollectionDocument
*/
public function getAll () {
$collectionId = $this->escape($this->id);
- $sql = "SELECT document_value FROM $this->table WHERE collection_id = '$collectionId'";
+ $sql =
+ "SELECT document_value FROM $this->table WHERE collection_id = '$collectionId'";
$client = static::getClient();
$type = $this->documentType;
diff --git a/workspaces/tests/Engines/Collection/BookDocument.php b/workspaces/tests/Engines/Collection/BookDocument.php
--- a/workspaces/tests/Engines/Collection/BookDocument.php
+++ b/workspaces/tests/Engines/Collection/BookDocument.php
@@ -2,7 +2,7 @@
namespace Waystone\Workspaces\Tests\Engines\Collection;
-use CollectionDocument;
+use Waystone\Workspaces\Engines\Collection\CollectionDocument;
require_once(__DIR__ . '/../../../src/includes/autoload.php');
diff --git a/workspaces/tests/Engines/Collection/CRUDTestTrait.php b/workspaces/tests/Engines/Collection/CRUDTestTrait.php
--- a/workspaces/tests/Engines/Collection/CRUDTestTrait.php
+++ b/workspaces/tests/Engines/Collection/CRUDTestTrait.php
@@ -17,7 +17,7 @@
namespace Waystone\Workspaces\Tests\Engines\Collection;
-use CollectionDocument;
+use Waystone\Workspaces\Engines\Collection\CollectionDocument;
/**
* The tests for our basic, non storage engine specific CRUD features
diff --git a/workspaces/tests/Engines/Collection/FilesCollectionTest.php b/workspaces/tests/Engines/Collection/FilesCollectionTest.php
--- a/workspaces/tests/Engines/Collection/FilesCollectionTest.php
+++ b/workspaces/tests/Engines/Collection/FilesCollectionTest.php
@@ -17,11 +17,11 @@
namespace Waystone\Workspaces\Tests\Engines\Collection;
+use Waystone\Workspaces\Engines\Collection\FilesCollection;
+
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
-use FilesCollection;
-
use Exception;
require_once(__DIR__ . '/../../../src//includes/GlobalFunctions.php');
diff --git a/workspaces/tests/Engines/Collection/MongoDBCollectionTest.php b/workspaces/tests/Engines/Collection/MongoDBCollectionTest.php
--- a/workspaces/tests/Engines/Collection/MongoDBCollectionTest.php
+++ b/workspaces/tests/Engines/Collection/MongoDBCollectionTest.php
@@ -17,11 +17,11 @@
namespace Waystone\Workspaces\Tests\Engines\Collection;
+use Waystone\Workspaces\Engines\Collection\MongoDBCollection;
+
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
-use MongoDBCollection;
-
/**
* Tests MongoDBCollection class
*/
diff --git a/workspaces/tests/Engines/Collection/MySQCollectionTest.php b/workspaces/tests/Engines/Collection/MySQCollectionTest.php
--- a/workspaces/tests/Engines/Collection/MySQCollectionTest.php
+++ b/workspaces/tests/Engines/Collection/MySQCollectionTest.php
@@ -17,11 +17,11 @@
namespace Waystone\Workspaces\Tests\Engines\Collection;
+use Waystone\Workspaces\Engines\Collection\MySQLCollection;
+
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
-use MySQLCollection;
-
/**
* Tests MySQLCollection class
*/
diff --git a/workspaces/tests/Engines/Collection/SQLTestTrait.php b/workspaces/tests/Engines/Collection/SQLTestTrait.php
--- a/workspaces/tests/Engines/Collection/SQLTestTrait.php
+++ b/workspaces/tests/Engines/Collection/SQLTestTrait.php
@@ -17,7 +17,7 @@
namespace Waystone\Workspaces\Tests\Engines\Collection;
-require_once(__DIR__ . '/../../../src/includes/autoload.php');
+use Exception;
/**
* The tests for our SQL storage engines, to ensure the schema is created correctly.
@@ -25,9 +25,7 @@
* For coverage purposes, it requires a coversDefaultClass annotation in the classes using this trait.
*/
trait SQLTestTrait {
- /**
- * @covers ::query()
- */
+
public function testQuery () {
/*
The query method is a complicated aspect of the code, as it returns a different
diff --git a/workspaces/tests/Engines/Collection/SQLiteCollectionTest.php b/workspaces/tests/Engines/Collection/SQLiteCollectionTest.php
--- a/workspaces/tests/Engines/Collection/SQLiteCollectionTest.php
+++ b/workspaces/tests/Engines/Collection/SQLiteCollectionTest.php
@@ -17,11 +17,11 @@
namespace Waystone\Workspaces\Tests\Engines\Collection;
+use Waystone\Workspaces\Engines\Collection\SQLiteCollection;
+
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
-use SQLiteCollection;
-
/**
* Tests SQLiteCollection class
*/

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 7, 08:02 (8 h, 27 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3142439
Default Alt Text
D3843.diff (39 KB)

Event Timeline