Skip to content

Commit

Permalink
Lazy loaded FileSystemFile.
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Mar 24, 2016
1 parent ac57575 commit 14ca256
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "qtism/qtism",
"description": "OAT QTI Software Module Library",
"type": "library",
"version": "0.9.21",
"version": "0.9.22",
"authors": [
{
"name": "Open Assessment Technologies S.A.",
Expand Down
17 changes: 13 additions & 4 deletions qtism/common/datatypes/files/FileSystemFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ class FileSystemFile implements File {
* @throws RuntimeException If the file cannot be retrieved correctly.
*/
public function __construct($path) {
$this->setPath($path);
}

private function readInfo() {
// Retrieve filename and mime type.
$fp = @fopen($path, 'r');
$fp = @fopen($this->getPath(), 'r');

if ($fp === false) {
$msg = "Unable to retrieve QTI file at '${path}.";
$msg = "Unable to retrieve QTI file at '".$this->getPath().".";
throw new RuntimeException($msg);
}

Expand All @@ -95,7 +99,6 @@ public function __construct($path) {

$this->setFilename($filename);
$this->setMimeType($mimeType);
$this->setPath($path);
}

/**
Expand All @@ -121,10 +124,16 @@ protected function setMimeType($mimeType) {
}

public function getMimeType() {
if (is_null($this->mimeType)) {
$this->readInfo();
}
return $this->mimeType;
}

public function getFilename() {
if (empty($this->filename)) {
$this->readInfo();
}
return $this->filename;
}

Expand Down Expand Up @@ -332,4 +341,4 @@ public function getIdentifier() {
public function __toString() {
return $this->getFilename();
}
}
}
3 changes: 2 additions & 1 deletion test/qtism/common/datatypes/files/FileSystemFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function testGetStream($path, $expectedData) {
public function testInstantiationWrongPath() {
$this->setExpectedException('\\RuntimeException');
$pFile = new FileSystemFile('/qtism/test');
$pFile->getFilename();
}

public function retrieveProvider() {
Expand All @@ -96,4 +97,4 @@ public function createFromExistingFileProvider() {
array(self::samplesDir() . 'datatypes/file/raw/text.txt', 'text/plain', 'new-name.txt'),
);
}
}
}

0 comments on commit 14ca256

Please sign in to comment.