-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Core] Add LorisInstance class #6118
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace LORIS; | ||
|
||
/** | ||
* A LorisInstance represents an installed instance of a LORIS | ||
* project. | ||
* | ||
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
*/ | ||
class LorisInstance | ||
{ | ||
protected $modulesDirs; | ||
private $DB; | ||
|
||
/** | ||
* Construct a LORISInstance for the install connected to $db | ||
* which uses modules from $moduleDirs. | ||
* | ||
* @param \Database $db A database connection to this | ||
* instance. | ||
* @param string[] $moduleDirs A list of directories that may | ||
* contain modules for this instance. | ||
*/ | ||
public function __construct(\Database $db, array $modulesDirs) | ||
{ | ||
$this->DB = $db; | ||
$this->modulesDirs = $modulesDirs; | ||
} | ||
|
||
/** | ||
* Return an active database connection to this instance. | ||
* | ||
* @return \Database | ||
*/ | ||
public function getDatabaseConnection() : \Database | ||
{ | ||
return $this->DB; | ||
} | ||
|
||
/** | ||
* Return a list of directories on the filesystem which | ||
* may contain modules. | ||
* | ||
* @return string[] | ||
*/ | ||
private function getModulesDirs() : array | ||
{ | ||
return $this->modulesDirs; | ||
} | ||
|
||
/** | ||
* Retrieve all active module descriptors from the given database. | ||
* | ||
* @param \Database $db an open connection to a database containing a 'modules' | ||
* table | ||
* | ||
* @return \Module[] | ||
*/ | ||
public function getActiveModules(): array | ||
{ | ||
$mnames = $this->getDatabaseConnection()->pselectCol( | ||
"SELECT Name FROM modules WHERE Active='Y'", | ||
[] | ||
); | ||
|
||
$modules = []; | ||
foreach ($mnames as $name) { | ||
try { | ||
$modules[] = \Module::factory($name); | ||
} catch (\LorisModuleMissingException $e) { | ||
error_log($e->getMessage() . " " . $e->getTraceAsString()); | ||
} | ||
} | ||
return $modules; | ||
} | ||
|
||
/** | ||
* Return true if the LORISInstance has a module named | ||
* $name. To be installed it must be both available on | ||
* the filesystem, and active in the modules table. | ||
driusan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @return bool | ||
*/ | ||
public function hasModule(string $name) : bool | ||
{ | ||
$dirs = $this->getModulesDirs(); | ||
$found = false; | ||
foreach ($dirs as $subdir) { | ||
if (is_dir($subdir . "/" . $name)) { | ||
$found = true; | ||
break; | ||
} | ||
} | ||
|
||
if ($found === false) { | ||
return false; | ||
} | ||
|
||
foreach ($this->getActiveModules() as $module) { | ||
if ($module->getName() == $name) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be nice to use SPLFileInfo instead especially if the main purpose of this property is to do validation based on the filepaths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is not the main purpose. The main purpose is to have a list of directories that modules may be found in.