Skip to content
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

Remove file and index getters and setters from project #3482

Merged
merged 4 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 0 additions & 33 deletions src/phpDocumentor/Descriptor/ProjectDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace phpDocumentor\Descriptor;

use phpDocumentor\Descriptor\Interfaces\ElementInterface;
use phpDocumentor\Descriptor\Interfaces\FileInterface;
use phpDocumentor\Descriptor\ProjectDescriptor\Settings;
use phpDocumentor\Descriptor\Traits\HasDescription;
Expand Down Expand Up @@ -66,18 +65,6 @@ public function __construct(string $name)
$this->versions = Collection::fromClassString(VersionDescriptor::class);
}

/**
* Sets all files on this project.
*
* @deprecated Please use {@see DocumentationSetDescriptor::getFiles()}
*
* @param Collection<FileInterface> $files
*/
public function setFiles(Collection $files): void
{
$this->getApiDocumentationSet()->setFiles($files);
}

/**
* Returns all files with their sub-elements.
*
Expand All @@ -90,18 +77,6 @@ public function getFiles(): Collection
return $this->getApiDocumentationSet()->getFiles();
}

/**
* Sets all indexes for this project.
*
* @deprecated Please use {@see DocumentationSetDescriptor::setIndexes()}
*
* @param Collection<Collection<ElementInterface>> $indexes
*/
public function setIndexes(Collection $indexes): void
{
$this->getApiDocumentationSet()->setIndexes($indexes);
}

/**
* Returns all indexes in this project.
*
Expand Down Expand Up @@ -153,14 +128,6 @@ public function getPartials(): Collection
return $this->partials;
}

/**
* @deprecated Please use {@see ApiSetDescriptor::findElement()}
*/
public function findElement(Fqsen $fqsen): ?ElementInterface
{
return $this->getApiDocumentationSet()->findElement($fqsen);
}

/**
* @return Collection<VersionDescriptor>
*/
Expand Down
41 changes: 24 additions & 17 deletions src/phpDocumentor/Pipeline/Stage/Transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Exception;
use League\Flysystem\FilesystemInterface;
use phpDocumentor\Descriptor\ApiSetDescriptor;
use phpDocumentor\Dsn;
use phpDocumentor\Event\Dispatcher;
use phpDocumentor\Parser\FlySystemFactory;
Expand Down Expand Up @@ -87,26 +88,32 @@ public function __invoke(Payload $payload): Payload
$project = $payload->getBuilder()->getProjectDescriptor();
$transformations = $templates->getTransformations();

/** @var PreTransformEvent $preTransformEvent */
$preTransformEvent = PreTransformEvent::createInstance($this);
$preTransformEvent->setProject($project);
$preTransformEvent->setTransformations($transformations);
Dispatcher::getInstance()->dispatch(
$preTransformEvent,
Transformer::EVENT_PRE_TRANSFORM
);
foreach ($project->getVersions() as $version) {
$apiSets = $version->getDocumentationSets()->filter(ApiSetDescriptor::class);
foreach ($apiSets as $apiSet) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, using multiple documentation sets would still not work because it is not fully supported when it comes to linking and even writing to the correct folder; but this will ensure the documentation set is propagated throughout the chain, which allowed me to remove findElement from the Project

/** @var PreTransformEvent $preTransformEvent */
$preTransformEvent = PreTransformEvent::createInstance($this);
$preTransformEvent->setProject($project);
$preTransformEvent->setTransformations($transformations);
Dispatcher::getInstance()->dispatch(
$preTransformEvent,
Transformer::EVENT_PRE_TRANSFORM
);

$this->transformer->execute(
$project,
$transformations
);
$this->transformer->execute(
$project,
$apiSet,
$transformations
);

/** @var PostTransformEvent $postTransformEvent */
$postTransformEvent = PostTransformEvent::createInstance($this);
$postTransformEvent->setProject($project);
$postTransformEvent->setTransformations($transformations);
/** @var PostTransformEvent $postTransformEvent */
$postTransformEvent = PostTransformEvent::createInstance($this);
$postTransformEvent->setProject($project);
$postTransformEvent->setTransformations($transformations);

Dispatcher::getInstance()->dispatch($postTransformEvent, Transformer::EVENT_POST_TRANSFORM);
Dispatcher::getInstance()->dispatch($postTransformEvent, Transformer::EVENT_POST_TRANSFORM);
}
}

return $payload;
}
Expand Down
29 changes: 20 additions & 9 deletions src/phpDocumentor/Transformer/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace phpDocumentor\Transformer;

use League\Flysystem\FilesystemInterface;
use phpDocumentor\Descriptor\DocumentationSetDescriptor;
use phpDocumentor\Descriptor\ProjectDescriptor;
use phpDocumentor\Dsn;
use phpDocumentor\Event\Dispatcher;
Expand Down Expand Up @@ -127,9 +128,12 @@ public function destination(): FilesystemInterface
*
* @param Transformation[] $transformations
*/
public function execute(ProjectDescriptor $project, array $transformations): void
{
$this->initializeWriters($project, $transformations);
public function execute(
ProjectDescriptor $project,
DocumentationSetDescriptor $documentationSet,
array $transformations
): void {
$this->initializeWriters($project, $documentationSet, $transformations);
$this->transformProject($project, $transformations);

$this->logger->log(LogLevel::NOTICE, 'Finished transformation process');
Expand All @@ -140,8 +144,11 @@ public function execute(ProjectDescriptor $project, array $transformations): voi
*
* @param Transformation[] $transformations
*/
private function initializeWriters(ProjectDescriptor $project, array $transformations): void
{
private function initializeWriters(
ProjectDescriptor $project,
DocumentationSetDescriptor $documentationSet,
array $transformations
): void {
$isInitialized = [];
foreach ($transformations as $transformation) {
$writerName = $transformation->getWriter();
Expand All @@ -152,7 +159,7 @@ private function initializeWriters(ProjectDescriptor $project, array $transforma

$isInitialized[] = $writerName;
$writer = $this->writers->get($writerName);
$this->initializeWriter($writer, $project, $transformation->template());
$this->initializeWriter($writer, $project, $documentationSet, $transformation->template());
}
}

Expand All @@ -172,15 +179,19 @@ private function initializeWriters(ProjectDescriptor $project, array $transforma
*
* @uses Dispatcher to emit the events surrounding an initialization.
*/
private function initializeWriter(WriterAbstract $writer, ProjectDescriptor $project, Template $template): void
{
private function initializeWriter(
WriterAbstract $writer,
ProjectDescriptor $project,
DocumentationSetDescriptor $documentationSet,
Template $template
): void {
/** @var WriterInitializationEvent $instance */
$instance = WriterInitializationEvent::createInstance($this);
$event = $instance->setWriter($writer);
$this->eventDispatcher->dispatch($event, self::EVENT_PRE_INITIALIZATION);

if ($writer instanceof Initializable) {
$writer->initialize($project, $template);
$writer->initialize($project, $documentationSet, $template);
}

$this->eventDispatcher->dispatch($event, self::EVENT_POST_INITIALIZATION);
Expand Down
7 changes: 6 additions & 1 deletion src/phpDocumentor/Transformer/Writer/Initializable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@

namespace phpDocumentor\Transformer\Writer;

use phpDocumentor\Descriptor\DocumentationSetDescriptor;
use phpDocumentor\Descriptor\ProjectDescriptor;
use phpDocumentor\Transformer\Template;

interface Initializable
{
public function initialize(ProjectDescriptor $project, Template $template): void;
public function initialize(
ProjectDescriptor $project,
DocumentationSetDescriptor $documentationSet,
Template $template
): void;
}
8 changes: 5 additions & 3 deletions src/phpDocumentor/Transformer/Writer/RenderGuide.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ public function transform(ProjectDescriptor $project, Transformation $transforma
//TODO Extract this, as this code is duplicated
$this->environmentBuilder->setEnvironmentFactory(
function () use ($transformation, $project, $documentationSet) {
$twig = $this->environmentFactory->create($project, $transformation->template());
$twig->addGlobal('project', $project);
$twig = $this->environmentFactory->create(
$project,
$documentationSet,
$transformation->template()
);
$twig->addGlobal('usesNamespaces', count($project->getNamespace()->getChildren()) > 0);
$twig->addGlobal('usesPackages', count($project->getPackage()->getChildren()) > 0);
$twig->addGlobal('documentationSet', $documentationSet);
$twig->addGlobal('destinationPath', null);

return $twig;
Expand Down
12 changes: 7 additions & 5 deletions src/phpDocumentor/Transformer/Writer/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use phpDocumentor\Descriptor\Collection as DescriptorCollection;
use phpDocumentor\Descriptor\Descriptor;
use phpDocumentor\Descriptor\DocumentationSetDescriptor;
use phpDocumentor\Descriptor\ProjectDescriptor;
use phpDocumentor\Descriptor\Query\Engine;
use phpDocumentor\Transformer\Template;
Expand Down Expand Up @@ -114,9 +115,12 @@ public function getName(): string
return 'twig';
}

public function initialize(ProjectDescriptor $project, Template $template): void
{
$this->environment = $this->environmentFactory->create($project, $template);
public function initialize(
ProjectDescriptor $project,
DocumentationSetDescriptor $documentationSet,
Template $template
): void {
$this->environment = $this->environmentFactory->create($project, $documentationSet, $template);
}

/**
Expand Down Expand Up @@ -200,10 +204,8 @@ private function transformNode(

$parameters = array_merge($transformation->getParameters(), $extraParameters);

$this->environment->addGlobal('project', $project);
$this->environment->addGlobal('usesNamespaces', count($project->getNamespace()->getChildren()) > 0);
$this->environment->addGlobal('usesPackages', count($project->getPackage()->getChildren()) > 0);
$this->environment->addGlobal('documentationSet', $project);
$this->environment->addGlobal('node', $node);
$this->environment->addGlobal('destinationPath', $path);
$this->environment->addGlobal('parameter', $parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace phpDocumentor\Transformer\Writer\Twig;

use League\CommonMark\ConverterInterface;
use phpDocumentor\Descriptor\DocumentationSetDescriptor;
use phpDocumentor\Descriptor\ProjectDescriptor;
use phpDocumentor\Guides\Graphs\Twig\UmlExtension;
use phpDocumentor\Guides\Twig\AssetsExtension;
Expand Down Expand Up @@ -57,6 +58,7 @@ public function withTemplateOverridesAt(Path $path): void

public function create(
ProjectDescriptor $project,
DocumentationSetDescriptor $documentationSet,
Template $template
): Environment {
$mountManager = $template->files();
Expand All @@ -72,7 +74,7 @@ public function create(

$env = new Environment(new ChainLoader($loaders));

$this->addPhpDocumentorExtension($project, $env);
$this->addPhpDocumentorExtension($project, $documentationSet, $env);
$this->enableDebug($env);

return $env;
Expand All @@ -83,10 +85,12 @@ public function create(
*/
private function addPhpDocumentorExtension(
ProjectDescriptor $project,
DocumentationSetDescriptor $documentationSet,
Environment $twigEnvironment
): void {
$extension = new Extension(
$project,
$documentationSet,
$this->markDownConverter,
$this->renderer,
$this->relativePathToRootConverter,
Expand Down
26 changes: 21 additions & 5 deletions src/phpDocumentor/Transformer/Writer/Twig/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use phpDocumentor\Descriptor\Descriptor;
use phpDocumentor\Descriptor\DescriptorAbstract;
use phpDocumentor\Descriptor\DocBlock\DescriptionDescriptor;
use phpDocumentor\Descriptor\DocumentationSetDescriptor;
use phpDocumentor\Descriptor\EnumDescriptor;
use phpDocumentor\Descriptor\Interfaces\ArgumentInterface;
use phpDocumentor\Descriptor\Interfaces\ClassInterface;
Expand Down Expand Up @@ -92,6 +93,8 @@ final class Extension extends AbstractExtension implements ExtensionInterface, G
private ConverterInterface $markdownConverter;
private RelativePathToRootConverter $relativePathToRootConverter;
private PathBuilder $pathBuilder;
private DocumentationSetDescriptor $documentationSet;
private ProjectDescriptor $project;

/**
* Registers the structure and transformation with this extension.
Expand All @@ -100,28 +103,40 @@ final class Extension extends AbstractExtension implements ExtensionInterface, G
*/
public function __construct(
ProjectDescriptor $project,
DocumentationSetDescriptor $documentationSet,
ConverterInterface $markdownConverter,
LinkRenderer $routeRenderer,
RelativePathToRootConverter $relativePathToRootConverter,
PathBuilder $pathBuilder
) {
$this->project = $project;
$this->documentationSet = $documentationSet;
$this->markdownConverter = $markdownConverter;
$this->routeRenderer = $routeRenderer;
$this->routeRenderer = $this->routeRenderer->withProject($project);
$this->routeRenderer = $this->routeRenderer->withProject($project)->forDocumentationSet($documentationSet);
$this->relativePathToRootConverter = $relativePathToRootConverter;
$this->pathBuilder = $pathBuilder;
}

/**
* Initialize series of globals used by the writers to set the context
*
* @return array<string, (true|string[]|null)>
* @return array{
* project: ProjectDescriptor,
* documentationSet: DocumentationSetDescriptor,
* node: ?Descriptor,
* usesNamespaces: bool,
* usesPackages: bool,
* destinationPath: ?string,
* parameter: array<string, mixed>,
* env: mixed
* }
*/
public function getGlobals(): array
{
return [
'project' => null,
'documentationSet' => null,
'project' => $this->project,
'documentationSet' => $this->documentationSet,
'node' => null,
'usesNamespaces' => true,
'usesPackages' => true,
Expand Down Expand Up @@ -447,7 +462,8 @@ private function contextRouteRenderer(array $context): LinkRenderer
{
return $this->routeRenderer
->withDestination(ltrim($context['destinationPath'] ?? $context['env']->getCurrentFileDestination(), '/\\'))
->withProject($context['project']);
->withProject($context['project'])
->forDocumentationSet($context['documentationSet']);
}

/**
Expand Down