Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F12944199
FilesCollection.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
7 KB
Referenced Files
None
Subscribers
None
FilesCollection.php
View Options
<?php
/**
* _, __, _, _ __, _ _, _, _
* / \ |_) (_ | | \ | /_\ |\ |
* \ / |_) , ) | |_/ | | | | \|
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* Files Collection class
*
* @package ObsidianWorkspaces
* @subpackage Collection
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*/
/**
* 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.
*
* @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'
]))
{
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
;
//Ensure directory exists. If not, creates it or throws an exception
if
(!
file_exists
(
$path
)
&&
!
mkdir
(
$path
,
0700
,
true
))
{
throw
new
Exception
(
"Directory doesn't exist and couldn't be created: $path"
);
}
return
$path
;
}
/**
* 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'
;
}
/**
* Gets the path to the folder where the current collection is stored.
*
* @return string The path to the specified collection folder
*/
public
function
getCurrentCollectionPath
()
{
return
static
::
getCollectionPath
(
$this
->
id
);
}
///
/// Constructor
///
/**
* Initializes a new instance of MongoCollection
*
* @param string the database identifiant
*/
public
function
__construct
(
$id
)
{
$this
->
id
=
$id
;
}
///
/// CRUD features
///
/**
* 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
)
{
if
(
$document
->
id
===
''
)
{
$document
->
id
=
uniqid
();
}
$filename
=
$this
->
getDocumentPath
(
$document
->
id
);
if
(
file_exists
(
$filename
))
{
throw
new
Exception
(
"A document with the same identifiant is already in the collection, stored at $filename"
);
}
$data
=
json_encode
(
$document
);
file_put_contents
(
$filename
,
$data
);
}
/**
* 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
)
{
$filename
=
$this
->
getDocumentPath
(
$documentId
);
unlink
(
$filename
);
}
/**
* 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
);
}
/**
* 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
);
}
$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
);
}
$data
=
json_encode
(
$document
);
file_put_contents
(
$filename
,
$data
);
}
/**
* Gets a document from the collection
*
* @param string $documentId The identifiant of the document to get
* @param CollectionDocument $document The document
*/
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 register a SPL autoloader or updated includes/autoload.php?"
);
}
$filename
=
$this
->
getDocumentPath
(
$documentId
);
$data
=
json_decode
(
file_get_contents
(
$filename
));
return
$type
::
loadFromObject
(
$data
);
}
/**
* 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
)
{
if
(
$document
->
id
===
''
)
{
$document
->
id
=
uniqid
();
}
$filename
=
$this
->
getDocumentPath
(
$document
->
id
);
$data
=
json_encode
(
$document
);
file_put_contents
(
$filename
,
$data
);
}
/**
* Gets a count of the documents in the collection
*
* @return int The number of documents
*/
public
function
count
()
{
$dir
=
$this
->
getCurrentCollectionPath
();
$count
=
0
;
$files
=
scandir
(
$dir
);
foreach
(
$files
as
$file
)
{
if
(
get_extension
(
$file
)
==
'json'
)
{
$count
++;
}
}
return
$count
;
}
/**
* Gets all the documents from the collection
*
* @return Generator An iterator to the documents, each item an instance of CollectionDocument
*/
public
function
getAll
()
{
$dir
=
$this
->
getCurrentCollectionPath
();
$files
=
scandir
(
$dir
);
foreach
(
$files
as
$file
)
{
if
(
get_extension
(
$file
)
==
'json'
)
{
$documentId
=
get_filename
(
$file
);
yield
$this
->
get
(
$documentId
);
}
}
}
/**
* Gets documents list
*
* @return array The documents list
*/
public
function
getDocumentsList
()
{
$dir
=
$this
->
getFilePath
(
''
);
$files
=
scandir
(
$dir
);
$documents
=
[];
foreach
(
$files
as
$file
)
{
if
(
get_extension
(
$file
)
==
'json'
)
{
$documents
[]
=
get_filename
(
$file
);
}
}
return
$documents
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Tue, Nov 18, 17:05 (22 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3149794
Default Alt Text
FilesCollection.php (7 KB)
Attached To
Mode
rOBSIDIAN Obsidian Workspaces
Attached
Detach File
Event Timeline
Log In to Comment