Skip to content

Commit

Permalink
Merge pull request #2009 from humanmade/late-render-partials
Browse files Browse the repository at this point in the history
Store partials internally as full objects
  • Loading branch information
jaapio committed Aug 11, 2018
2 parents 4212ab6 + cf05f18 commit cf2ef3c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 57 deletions.
48 changes: 0 additions & 48 deletions src/phpDocumentor/Partials/Collection.php

This file was deleted.

34 changes: 34 additions & 0 deletions src/phpDocumentor/Partials/Partial.php
Expand Up @@ -37,6 +37,9 @@ class Partial
*/
protected $link;

/** @var \Parsedown $parser */
protected $parser = null;

/**
* @return mixed
*/
Expand Down Expand Up @@ -84,4 +87,35 @@ public function setName(string $name): void
{
$this->name = $name;
}

/**
* Constructs a new collection object.
*
* @param \Parsedown $parser
*/
public function setParser($parser)
{
$this->parser = $parser;
}

/**
* Renders the partial to a string.
*
* @return string
*/
public function __toString()
{
$content = '';
if ($this->getContent()) {
$content = $partial->getContent();
} elseif ($this->getLink()) {
if (! is_readable($this->getLink())) {
// Handled in ServiceProvider.
continue;
}

$content = file_get_contents($this->getLink());
}
return $this->parser->text($content);
}
}
14 changes: 5 additions & 9 deletions src/phpDocumentor/Partials/ServiceProvider.php
Expand Up @@ -16,7 +16,7 @@
namespace phpDocumentor\Partials;

use Parsedown;
use phpDocumentor\Partials\Collection as PartialsCollection;
use phpDocumentor\Descriptor\Collection;
use phpDocumentor\Partials\Exception\MissingNameForPartialException;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
Expand Down Expand Up @@ -44,7 +44,7 @@ public function register(Container $app): void
return Parsedown::instance();
};

$partialsCollection = new PartialsCollection($app['markdown']);
$partialsCollection = new Collection();
$app['partials'] = $partialsCollection;

/** @var Partial[] $partials */
Expand All @@ -55,21 +55,17 @@ public function register(Container $app): void
throw new MissingNameForPartialException('The name of the partial to load is missing');
}

$content = '';
if ($partial->getContent()) {
$content = $partial->getContent();
} elseif ($partial->getLink()) {
$partial->setParser($app['markdown']);
if (!$partial->getContent() && $partial->getLink()) {
if (! is_readable($partial->getLink())) {
$app['monolog']->error(
sprintf($translator->translate('PPCPP:EXC-NOPARTIAL'), $partial->getLink())
);
continue;
}

$content = file_get_contents($partial->getLink());
}

$partialsCollection->set($partial->getName(), $content);
$partialsCollection->set($partial->getName(), $partial);
}
}
}
Expand Down

0 comments on commit cf2ef3c

Please sign in to comment.