-
-
Notifications
You must be signed in to change notification settings - Fork 634
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
[POC] resolving hooks in workpress code #3067
Open
jaapio
wants to merge
1
commit into
master
Choose a base branch
from
POC/wordpress_parsing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,23 @@ | ||
{% extends 'base.html.twig' %} | ||
|
||
{% block content %} | ||
{% include('components/breadcrumbs.html.twig') %} | ||
|
||
<article class="phpdocumentor-element -file"> | ||
{{ include('components/file-title.html.twig') }} | ||
{{ include('components/element-header.html.twig') }} | ||
|
||
{{ include('components/constants.html.twig') }} | ||
{{ include('components/functions.html.twig') }} | ||
|
||
<h2 class="phpdocumentor-content__title">Hooks</h2> | ||
<ul> | ||
{% for action in node.metadata.wp_hooks.actions %} | ||
<li> | ||
<h3>{{ action.name }}</h3> | ||
<section class="phpdocumentor-description">{{ action.docblock.description|markdown }}</section> | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
</article> | ||
{% endblock %} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,15 @@ | ||
################################################################################### | ||
## Wordpress - EXPERIMENTAL ########################################################## | ||
################################################################################### | ||
|
||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
public: false | ||
_instanceof: | ||
phpDocumentor\Reflection\Php\ProjectFactoryStrategy: | ||
tags: ['phpdoc.reflection.strategy'] | ||
|
||
phpDocumentor\Wordpress\: | ||
resource: '%kernel.project_dir%/vendor/phpdocumentor/wordpress/src' |
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 @@ | ||
vendor/* |
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,4 @@ | ||
**EXPERIMENTAL**: Anything regarding Guide generation, in this folder or elsewhere, is to be considered experimental and | ||
prone to be changed or removed without notice or consideration for BC. | ||
|
||
This is a follow-up to an earlier POC and can, at best, seen as an incubator project. |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2010 Mike van Riel | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,22 @@ | ||
{ | ||
"name": "phpdocumentor/wordpress", | ||
"type": "library", | ||
"license": "MIT", | ||
"homepage": "https://www.phpdoc.org", | ||
"autoload": { | ||
"psr-4": { | ||
"phpDocumentor\\Wordpress\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"phpDocumentor\\Wordpress\\": [ | ||
"tests/unit/" | ||
] | ||
} | ||
}, | ||
"minimum-stability": "stable", | ||
"require": { | ||
"php": ">=7.2.5||^8.0" | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Wordpress\Reflection\Php; | ||
|
||
use phpDocumentor\Reflection\DocBlock; | ||
|
||
final class Action | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @var DocBlock | ||
*/ | ||
private $docBlock; | ||
|
||
public function __construct(string $name, DocBlock $docBlock) | ||
{ | ||
$this->name = $name; | ||
$this->docBlock = $docBlock; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getDocBlock(): DocBlock | ||
{ | ||
return $this->docBlock; | ||
} | ||
} |
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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Wordpress\Reflection\Php\Factory; | ||
|
||
use phpDocumentor\Reflection\DocBlock; | ||
use phpDocumentor\Reflection\DocBlockFactoryInterface; | ||
use phpDocumentor\Reflection\Metadata\MetaDataContainer; | ||
use phpDocumentor\Reflection\Php\Factory\AbstractFactory; | ||
use phpDocumentor\Reflection\Php\Factory\ContextStack; | ||
use phpDocumentor\Reflection\Php\StrategyContainer; | ||
use phpDocumentor\Wordpress\Reflection\Php\Action as ActionElement; | ||
use phpDocumentor\Wordpress\Reflection\Php\Hooks; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\PrettyPrinter\Standard as PrettyPrinter; | ||
|
||
final class Action extends AbstractFactory | ||
{ | ||
private const ACTION_NAMES = [ | ||
'do_action', | ||
'do_action_ref_array', | ||
'do_action_deprecated', | ||
]; | ||
/** | ||
* @var PrettyPrinter | ||
*/ | ||
private $valuePrinter; | ||
|
||
public function __construct(DocBlockFactoryInterface $docBlockFactory, PrettyPrinter $valuePrinter) | ||
{ | ||
parent::__construct($docBlockFactory); | ||
$this->valuePrinter = $valuePrinter; | ||
} | ||
|
||
|
||
public function matches(ContextStack $context, object $object): bool | ||
{ | ||
return $object instanceof FuncCall && $context->peek() instanceof MetaDataContainer && in_array($object->name->toString(), self::ACTION_NAMES); | ||
} | ||
|
||
public function doCreate(ContextStack $context, object $object, StrategyContainer $strategies): void | ||
{ | ||
/** @var MetaDataContainer $container */ | ||
$container = $context->peek(); | ||
|
||
$hooks = new Hooks(); | ||
$existing = $container->getMetadata()[$hooks->key()] ?? $hooks; | ||
if ($existing === $hooks) { | ||
$container->addMetadata($hooks); | ||
} | ||
|
||
$existing->addAction( | ||
new ActionElement( | ||
$this->valuePrinter->prettyPrintExpr($object->args[0]->value), | ||
$this->createDocBlock($object->getDocComment(), $context->getTypeContext()) ?? new DocBlock() | ||
) | ||
); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
incubator/wordpress/src/Reflection/Php/Factory/Expression.php
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Wordpress\Reflection\Php\Factory; | ||
|
||
use phpDocumentor\Reflection\Php\Factory; | ||
use phpDocumentor\Reflection\Php\Factory\ContextStack; | ||
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy; | ||
use phpDocumentor\Reflection\Php\StrategyContainer; | ||
|
||
final class Expression implements ProjectFactoryStrategy | ||
{ | ||
public function matches(ContextStack $context, object $object): bool | ||
{ | ||
return $object instanceof \PhpParser\Node\Stmt\Expression; | ||
} | ||
|
||
public function create(ContextStack $context, object $object, StrategyContainer $strategies): void | ||
{ | ||
$strategies->findMatching($context, $object->expr)->create($context, $object->expr, $strategies); | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Wordpress\Reflection\Php; | ||
|
||
use phpDocumentor\Reflection\Metadata\Metadata; | ||
|
||
final class Hooks implements Metadata | ||
{ | ||
private $actions = []; | ||
|
||
public function key(): string | ||
{ | ||
return 'wp_hooks'; | ||
} | ||
|
||
public function addAction(Action $action): void | ||
{ | ||
$this->actions[] = $action; | ||
} | ||
|
||
/** @return Action[] */ | ||
public function getActions(): array | ||
{ | ||
return $this->actions; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually actions are filters which do nothing return in WordPress. So it's maybe better to call this either
Filter
or introduce a second class which is calledFilter
. In WordPress there are following functions which are using internally the Hook API ofWP_Hook
: