2010-06-24 16:22:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2011-07-01 08:49:31 +02:00
|
|
|
* A {@link DocumentationEntity} is created when registering a module path
|
|
|
|
* with {@link DocumentationService::register()}. A {@link DocumentationEntity}
|
|
|
|
* represents a module or folder with documentation rather than a specific
|
|
|
|
* page. Individual pages are handled by {@link DocumentationPage}
|
|
|
|
*
|
2010-08-01 06:46:32 +02:00
|
|
|
* Each folder must have at least one language subfolder, which is automatically
|
|
|
|
* determined through {@link addVersion()} and should not be included in the $path argument.
|
2010-08-01 09:03:48 +02:00
|
|
|
*
|
|
|
|
* Versions are assumed to be in numeric format (e.g. '2.4'),
|
2011-07-01 08:49:31 +02:00
|
|
|
*
|
2011-07-01 03:19:35 +02:00
|
|
|
* They're also parsed through version_compare() in {@link getLatestVersion()} which assumes a certain format.
|
2010-06-24 16:22:41 +02:00
|
|
|
*
|
|
|
|
* @package sapphiredocs
|
2011-07-01 08:49:31 +02:00
|
|
|
* @subpackage models
|
2010-06-24 16:22:41 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
class DocumentationEntity extends ViewableData {
|
|
|
|
|
|
|
|
static $casting = array(
|
|
|
|
'Name' => 'Text'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2011-07-01 08:49:31 +02:00
|
|
|
* @var string $folder folder name
|
2010-06-24 16:22:41 +02:00
|
|
|
*/
|
2011-07-01 08:49:31 +02:00
|
|
|
private $folder;
|
2010-06-24 16:22:41 +02:00
|
|
|
|
|
|
|
/**
|
2010-12-21 10:42:44 +01:00
|
|
|
* @var string $title nice title
|
2010-06-24 16:22:41 +02:00
|
|
|
*/
|
|
|
|
private $title;
|
|
|
|
|
|
|
|
/**
|
2010-12-21 10:42:44 +01:00
|
|
|
* @var array $version version numbers and the paths to each
|
2010-06-24 16:22:41 +02:00
|
|
|
*/
|
|
|
|
private $versions = array();
|
|
|
|
|
2010-08-01 09:03:48 +02:00
|
|
|
/**
|
2010-12-21 10:42:44 +01:00
|
|
|
* @var array
|
2010-08-01 09:03:48 +02:00
|
|
|
*/
|
2011-07-01 03:19:35 +02:00
|
|
|
private $latestVersion;
|
2010-08-01 09:03:48 +02:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
/**
|
|
|
|
* @var Array $langs a list of available langauges
|
|
|
|
*/
|
|
|
|
private $langs = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor. You do not need to pass the langs to this as
|
|
|
|
* it will work out the languages from the filesystem
|
|
|
|
*
|
2011-07-01 08:49:31 +02:00
|
|
|
* @param string $folder folder name
|
2010-12-21 10:42:44 +01:00
|
|
|
* @param string $version version of this module
|
|
|
|
* @param string $path Absolute path to this module (excluding language folders)
|
|
|
|
* @param string $title
|
2010-06-24 16:22:41 +02:00
|
|
|
*/
|
2011-07-01 08:49:31 +02:00
|
|
|
function __construct($folder, $version, $path, $title = false) {
|
2010-06-24 16:22:41 +02:00
|
|
|
$this->addVersion($version, $path);
|
2011-07-01 08:49:31 +02:00
|
|
|
$this->title = (!$title) ? $folder : $title;
|
|
|
|
$this->folder = $folder;
|
2010-06-24 16:22:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the languages which are available
|
|
|
|
*
|
2011-07-01 08:49:31 +02:00
|
|
|
* @return array
|
2010-06-24 16:22:41 +02:00
|
|
|
*/
|
|
|
|
public function getLanguages() {
|
|
|
|
return $this->langs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether this entity has a given language
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasLanguage($lang) {
|
|
|
|
return (in_array($lang, $this->langs));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a langauge or languages to the entity
|
|
|
|
*
|
|
|
|
* @param Array|String languages
|
|
|
|
*/
|
|
|
|
public function addLanguage($language) {
|
|
|
|
if(is_array($language)) {
|
|
|
|
$this->langs = array_unique(array_merge($this->langs, $language));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->langs[] = $language;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the folder name of this module
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
2011-07-01 08:49:31 +02:00
|
|
|
public function getFolder() {
|
|
|
|
return $this->folder;
|
2010-06-24 16:22:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the title of this module
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public function getTitle() {
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-01 08:49:31 +02:00
|
|
|
* Return the versions which have been registered for this entity.
|
2010-06-24 16:22:41 +02:00
|
|
|
*
|
2011-07-01 08:49:31 +02:00
|
|
|
* @return array
|
2010-06-24 16:22:41 +02:00
|
|
|
*/
|
|
|
|
public function getVersions() {
|
|
|
|
return array_keys($this->versions);
|
|
|
|
}
|
|
|
|
|
2010-08-01 09:03:48 +02:00
|
|
|
/**
|
|
|
|
* @return String|Boolean
|
|
|
|
*/
|
2011-07-01 03:19:35 +02:00
|
|
|
public function getLatestVersion() {
|
2010-08-01 09:03:48 +02:00
|
|
|
if(!$this->hasVersions()) return false;
|
|
|
|
|
2011-07-01 03:19:35 +02:00
|
|
|
if($this->latestVersion) {
|
|
|
|
return $this->latestVersion;
|
2010-08-01 09:03:48 +02:00
|
|
|
} else {
|
|
|
|
$sortedVersions = $this->getVersions();
|
|
|
|
usort($sortedVersions, create_function('$a,$b', 'return version_compare($a,$b);'));
|
2011-07-01 03:19:35 +02:00
|
|
|
|
2010-08-01 09:03:48 +02:00
|
|
|
return array_pop($sortedVersions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $version
|
|
|
|
*/
|
2011-07-01 03:19:35 +02:00
|
|
|
public function setLatestVersion($version) {
|
2010-08-01 09:03:48 +02:00
|
|
|
if(!$this->hasVersion($version)) throw new InvalidArgumentException(sprintf('Version "%s" does not exist', $version));
|
2011-07-01 03:19:35 +02:00
|
|
|
$this->latestVersion = $version;
|
2010-08-01 09:03:48 +02:00
|
|
|
}
|
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
/**
|
|
|
|
* Return whether we have a given version of this entity
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasVersion($version) {
|
|
|
|
return (isset($this->versions[$version]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether we have any versions at all0
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasVersions() {
|
|
|
|
return (sizeof($this->versions) > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add another version to this entity
|
|
|
|
*
|
|
|
|
* @param Float $version Version number
|
|
|
|
* @param String $path path to folder
|
|
|
|
*/
|
2011-07-01 03:19:35 +02:00
|
|
|
public function addVersion($version = '', $path) {
|
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
$langs = scandir($path);
|
|
|
|
$available = array();
|
|
|
|
|
|
|
|
if($langs) {
|
|
|
|
foreach($langs as $key => $lang) {
|
|
|
|
if(!is_dir($path . $lang) || strlen($lang) > 2 || in_array($lang, DocumentationService::get_ignored_files(), true))
|
|
|
|
$lang = 'en';
|
|
|
|
|
|
|
|
if(!in_array($lang, $available))
|
|
|
|
$available[] = $lang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addLanguage($available);
|
|
|
|
$this->versions[$version] = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a version from this entity
|
|
|
|
*
|
|
|
|
* @param Float $version
|
|
|
|
*/
|
|
|
|
public function removeVersion($version = '') {
|
|
|
|
if(isset($this->versions[$version])) {
|
|
|
|
unset($this->versions[$version]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-22 09:21:49 +01:00
|
|
|
* Return the absolute path to this documentation entity on the
|
|
|
|
* filesystem
|
2010-06-24 16:22:41 +02:00
|
|
|
*
|
2010-12-22 09:21:49 +01:00
|
|
|
* @return string
|
2010-06-24 16:22:41 +02:00
|
|
|
*/
|
|
|
|
public function getPath($version = false, $lang = false) {
|
2011-07-01 03:19:35 +02:00
|
|
|
if(!$version) $version = $this->getLatestVersion();
|
2010-06-24 16:22:41 +02:00
|
|
|
if(!$lang) $lang = 'en';
|
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
if($this->hasVersion($version)) {
|
|
|
|
$path = $this->versions[$version];
|
2010-12-22 09:21:49 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-08-01 06:46:32 +02:00
|
|
|
$versions = $this->getVersions();
|
2010-12-22 09:21:49 +01:00
|
|
|
$path = $this->versions[$versions[0]];
|
|
|
|
}
|
|
|
|
|
2011-07-01 08:49:31 +02:00
|
|
|
return Controller::join_links($path, $lang);
|
2010-08-01 09:03:50 +02:00
|
|
|
}
|
|
|
|
|
2010-12-22 09:21:49 +01:00
|
|
|
/**
|
|
|
|
* Returns the web accessible link to this Entity
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function Link($version = false, $lang = false) {
|
2011-01-16 21:17:56 +01:00
|
|
|
return Controller::join_links(
|
|
|
|
Director::absoluteBaseURL(),
|
|
|
|
$this->getRelativeLink($version, $lang)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRelativeLink($version = false, $lang = false) {
|
2011-07-01 03:19:35 +02:00
|
|
|
if(!$version) $version = $this->getLatestVersion();
|
2010-12-22 09:21:49 +01:00
|
|
|
if(!$lang) $lang = 'en';
|
|
|
|
|
|
|
|
return Controller::join_links(
|
|
|
|
DocumentationViewer::get_link_base(),
|
2011-07-01 08:49:31 +02:00
|
|
|
$this->getFolder(),
|
2010-12-22 09:21:49 +01:00
|
|
|
$lang,
|
2011-01-14 02:03:46 +01:00
|
|
|
$version
|
2010-12-22 09:21:49 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-07-01 08:49:31 +02:00
|
|
|
/**
|
|
|
|
* Return the summary / index text for this entity. Either pulled
|
|
|
|
* from an index file or some other summary field
|
|
|
|
*
|
|
|
|
* @return DocumentationPage
|
|
|
|
*/
|
|
|
|
function getIndexPage($version, $lang = 'en') {
|
|
|
|
$path = $this->getPath($version, $lang);
|
|
|
|
$absFilepath = Controller::join_links($path, 'index.md');
|
|
|
|
|
|
|
|
if(file_exists($absFilepath)) {
|
|
|
|
$relativeFilePath = str_replace($path, '', $absFilepath);
|
|
|
|
|
|
|
|
$page = new DocumentationPage();
|
|
|
|
$page->setRelativePath($relativeFilePath);
|
|
|
|
$page->setEntity($this);
|
|
|
|
$page->setLang($lang);
|
|
|
|
$page->setVersion($version);
|
|
|
|
|
|
|
|
return $page;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-08-01 09:03:50 +02:00
|
|
|
function __toString() {
|
|
|
|
return sprintf('DocumentationEntity: %s)', $this->getPath());
|
2010-06-24 16:22:41 +02:00
|
|
|
}
|
|
|
|
}
|