Skip to content
This repository has been archived by the owner on Dec 27, 2018. It is now read-only.

Commit

Permalink
Trying to reduce complexity.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Aug 9, 2016
1 parent 87cf1f6 commit 302c96b
Showing 1 changed file with 92 additions and 68 deletions.
160 changes: 92 additions & 68 deletions src/Generator/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class Taxonomy implements GeneratorInterface
protected $options;
/* @var \PHPoole\Taxonomy\Collection $taxonomies */
protected $taxonomies;
/* @var PageCollection */
protected $pageCollection;
/* @var array */
protected $siteTaxonomies;
/* @var PageCollection */
protected $generatedPages;

/**
* Taxonomy constructor.
Expand All @@ -38,95 +44,113 @@ public function __construct(Data $options)
*/
public function generate(PageCollection $pageCollection, \Closure $messageCallback)
{
$generatedPages = new PageCollection();
$this->pageCollection = $pageCollection;
$this->generatedPages = new PageCollection();

if (array_key_exists('taxonomies', $this->options->get('site'))) {
$siteTaxonomies = $this->options->get('site.taxonomies');
$this->siteTaxonomies = $this->options->get('site.taxonomies');

$disabled = array_key_exists('disabled', $siteTaxonomies) && $siteTaxonomies['disabled'];
$disabled = array_key_exists('disabled', $this->siteTaxonomies) && $this->siteTaxonomies['disabled'];
if ($disabled) {
return $generatedPages;
return $this->generatedPages;
}

// prepares taxonomies collection
$this->taxonomies = new \PHPoole\Taxonomy\Collection();
$this->taxonomies = new \PHPoole\Taxonomy\Collection('taxonomies');
// adds each vocabulary collection to the taxonomies collection
foreach ($siteTaxonomies as $plural => $singular) {
foreach ($this->siteTaxonomies as $plural => $singular) {
if ($plural != 'disable') {
$this->taxonomies->add(new \PHPoole\Taxonomy\Vocabulary($plural));
}
}

// collects taxonomies from pages
/* @var $page Page */
foreach ($pageCollection as $page) {
foreach ($siteTaxonomies as $plural => $singular) {
if (isset($page[$plural])) {
// converts a list to an array if necessary
if (!is_array($page[$plural])) {
$page->setVariable($plural, [$page[$plural]]);
}
foreach ($page[$plural] as $term) {
// adds each terms to the vocabulary collection
$this->taxonomies->get($plural)
->add(new \PHPoole\Taxonomy\Term($term));
// adds each pages to the term collection
$this->taxonomies
->get($plural)
->get($term)
->add($page);
}
$this->collectTaxonomiesFromPages();

// creates node pages
$this->createNodePages();
}

return $this->generatedPages;
}

/**
* Collects taxonomies from pages
*/
protected function collectTaxonomiesFromPages()
{
/* @var $page Page */
foreach ($this->pageCollection as $page) {
foreach ($this->siteTaxonomies as $plural => $singular) {
if (isset($page[$plural])) {
// converts a list to an array if necessary
if (!is_array($page[$plural])) {
$page->setVariable($plural, [$page[$plural]]);
}
foreach ($page[$plural] as $term) {
// adds each terms to the vocabulary collection
$this->taxonomies->get($plural)
->add(new \PHPoole\Taxonomy\Term($term));
// adds each pages to the term collection
$this->taxonomies
->get($plural)
->get($term)
->add($page);
}
}
}
// adds node pages
/* @var $terms \PHPoole\Taxonomy\Vocabulary */
foreach ($this->taxonomies as $plural => $terms) {
if (count($terms) > 0) {
/*
* Creates $plural/$term pages (list of pages)
* ex: /tags/tag-1/
*/
/* @var $pages PageCollection */
foreach ($terms as $term => $pages) {
if (!$pageCollection->has($term)) {
$pages = $pages->sortByDate()->toArray();
$page = (new Page())
->setId(Page::urlize(sprintf('%s/%s/index', $plural, $term)))
->setPathname(Page::urlize(sprintf('%s/%s', $plural, $term)))
->setTitle(ucfirst($term))
->setNodeType(NodeTypeEnum::TAXONOMY)
->setVariable('pages', $pages)
->setVariable('singular', $siteTaxonomies[$plural])
->setVariable('pagination', ['pages' => $pages]);
$generatedPages->add($page);
}
}
/*
* Creates $plural pages (list of terms)
* ex: /tags/
*/
$page = (new Page())
->setId(strtolower($plural))
->setPathname(strtolower($plural))
->setTitle($plural)
->setNodeType(NodeTypeEnum::TERMS)
->setVariable('plural', $plural)
->setVariable('singular', $siteTaxonomies[$plural])
->setVariable('terms', $terms);
}
}

// add page only if a template exist
try {
$generatedPages->add($page);
} catch (\Exception $e) {
echo $e->getMessage()."\n";
// do not add page
unset($page);
/**
* Creates node pages
*/
protected function createNodePages()
{
/* @var $terms \PHPoole\Taxonomy\Vocabulary */
foreach ($this->taxonomies as $plural => $terms) {
if (count($terms) > 0) {
/*
* Creates $plural/$term pages (list of pages)
* ex: /tags/tag-1/
*/
/* @var $pages PageCollection */
foreach ($terms as $term => $pages) {
if (!$this->pageCollection->has($term)) {
$pages = $pages->sortByDate()->toArray();
$page = (new Page())
->setId(Page::urlize(sprintf('%s/%s/index', $plural, $term)))
->setPathname(Page::urlize(sprintf('%s/%s', $plural, $term)))
->setTitle(ucfirst($term))
->setNodeType(NodeTypeEnum::TAXONOMY)
->setVariable('pages', $pages)
->setVariable('singular', $this->siteTaxonomies[$plural])
->setVariable('pagination', ['pages' => $pages]);
$this->generatedPages->add($page);
}
}
/*
* Creates $plural pages (list of terms)
* ex: /tags/
*/
$page = (new Page())
->setId(strtolower($plural))
->setPathname(strtolower($plural))
->setTitle($plural)
->setNodeType(NodeTypeEnum::TERMS)
->setVariable('plural', $plural)
->setVariable('singular', $this->siteTaxonomies[$plural])
->setVariable('terms', $terms);

// add page only if a template exist
try {
$this->generatedPages->add($page);
} catch (\Exception $e) {
echo $e->getMessage()."\n";
// do not add page
unset($page);
}
}
}

return $generatedPages;
}
}

0 comments on commit 302c96b

Please sign in to comment.