This repository has been archived by the owner on Dec 27, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move options to a dedicated class. (#114)
* Move options to a dedicated class. * Applied fixes from StyleCI (#115) * Code enhancement. * CS.
- Loading branch information
1 parent
9b24eba
commit c49cd54
Showing
4 changed files
with
342 additions
and
195 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,264 @@ | ||
<?php | ||
/* | ||
* Copyright (c) Arnaud Ligny <arnaud@ligny.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace PHPoole; | ||
|
||
use Dflydev\DotAccessData\Data; | ||
|
||
/** | ||
* Class Options. | ||
*/ | ||
class Options | ||
{ | ||
/** | ||
* Options. | ||
* | ||
* @var Data | ||
*/ | ||
protected $options; | ||
/** | ||
* Source directory. | ||
* | ||
* @var string | ||
*/ | ||
protected $sourceDir; | ||
/** | ||
* Destination directory. | ||
* | ||
* @var string | ||
*/ | ||
protected $destinationDir; | ||
/** | ||
* Default options. | ||
* | ||
* @var array | ||
*/ | ||
protected static $defaultOptions = [ | ||
'site' => [ | ||
'title' => 'PHPoole', | ||
'baseline' => 'A PHPoole website', | ||
'baseurl' => 'http://localhost:8000/', | ||
'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', | ||
'taxonomies' => [ | ||
'tags' => 'tag', | ||
'categories' => 'category', | ||
], | ||
'paginate' => [ | ||
'max' => 5, | ||
'path' => 'page', | ||
], | ||
'date' => [ | ||
'format' => 'j F Y', | ||
'timezone' => 'Europe/Paris', | ||
], | ||
], | ||
'content' => [ | ||
'dir' => 'content', | ||
'ext' => 'md', | ||
], | ||
'frontmatter' => [ | ||
'format' => 'yaml', | ||
], | ||
'body' => [ | ||
'format' => 'md', | ||
], | ||
'static' => [ | ||
'dir' => 'static', | ||
], | ||
'layouts' => [ | ||
'dir' => 'layouts', | ||
], | ||
'output' => [ | ||
'dir' => '_site', | ||
'filename' => 'index.html', | ||
], | ||
'themes' => [ | ||
'dir' => 'themes', | ||
], | ||
]; | ||
|
||
/** | ||
* Options constructor. | ||
* | ||
* @param Options|array|null $options | ||
*/ | ||
public function __construct($options) | ||
{ | ||
$data = new Data(self::$defaultOptions); | ||
if ($options instanceof self) { | ||
$data->importData($options->getAll()); | ||
} elseif (is_array($options)) { | ||
$data->import($options); | ||
} | ||
$this->setFromData($data); | ||
} | ||
|
||
/** | ||
* Set options. | ||
* | ||
* @param Data $data | ||
* | ||
* @return $this | ||
*/ | ||
public function setFromData(Data $data) | ||
{ | ||
if ($this->options !== $data) { | ||
$this->options = $data; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get options. | ||
* | ||
* @return Data | ||
*/ | ||
public function getAll() | ||
{ | ||
if (is_null($this->options)) { | ||
$this->setFromData(new Data()); | ||
} | ||
|
||
return $this->options; | ||
} | ||
|
||
/** | ||
* Get options as array. | ||
* | ||
* @return array | ||
*/ | ||
public function getAllAsArray() | ||
{ | ||
return $this->options->export(); | ||
} | ||
|
||
/** | ||
* return an option value. | ||
* | ||
* @param string $key | ||
* @param string $default | ||
* | ||
* @return array|mixed|null | ||
*/ | ||
public function get($key, $default = '') | ||
{ | ||
return $this->options->get($key, $default); | ||
} | ||
|
||
/** | ||
* @param null $sourceDir | ||
* | ||
* @throws \Exception | ||
* | ||
* @return $this | ||
*/ | ||
public function setSourceDir($sourceDir = null) | ||
{ | ||
if ($sourceDir === null) { | ||
$sourceDir = getcwd(); | ||
} | ||
if (!is_dir($sourceDir)) { | ||
throw new \InvalidArgumentException(sprintf("'%s' is not a valid source directory.", $sourceDir)); | ||
} | ||
$this->sourceDir = $sourceDir; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getSourceDir() | ||
{ | ||
return $this->sourceDir; | ||
} | ||
|
||
/** | ||
* @param null $destinationDir | ||
* | ||
* @throws \Exception | ||
* | ||
* @return $this | ||
*/ | ||
public function setDestinationDir($destinationDir = null) | ||
{ | ||
if ($destinationDir === null) { | ||
$destinationDir = $this->sourceDir; | ||
} | ||
if (!is_dir($destinationDir)) { | ||
throw new \InvalidArgumentException(sprintf("'%s' is not a valid destination directory.", $destinationDir)); | ||
} | ||
$this->destinationDir = $destinationDir; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getDestinationDir() | ||
{ | ||
return $this->destinationDir; | ||
} | ||
|
||
/** | ||
* Path helpers. | ||
*/ | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getContentPath() | ||
{ | ||
return $this->getSourceDir().'/'.$this->get('content.dir'); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getLayoutsPath() | ||
{ | ||
return $this->getSourceDir().'/'.$this->get('layouts.dir'); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getThemesPath() | ||
{ | ||
return $this->getSourceDir().'/'.$this->get('themes.dir'); | ||
} | ||
|
||
/** | ||
* @param string $theme | ||
* @param string $dir | ||
* | ||
* @return string | ||
*/ | ||
public function getThemePath($theme, $dir = 'layouts') | ||
{ | ||
return $this->getSourceDir().'/'.$this->get('themes.dir')."/$theme/$dir"; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getOutputPath() | ||
{ | ||
return $this->getSourceDir().'/'.$this->get('output.dir'); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getStaticPath() | ||
{ | ||
return $this->getSourceDir().'/'.$this->get('static.dir'); | ||
} | ||
} |
Oops, something went wrong.