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

CraftCMS support #10

Closed
marcusgaius opened this issue Mar 23, 2024 · 6 comments · Fixed by #11
Closed

CraftCMS support #10

marcusgaius opened this issue Mar 23, 2024 · 6 comments · Fixed by #11
Assignees
Labels
feature New feature or request

Comments

@marcusgaius
Copy link

marcusgaius commented Mar 23, 2024

Currently, the extension only supports the first half of the twig debug command, which is to target the binary, however, the 2nd part of the command is hardcoded, and thus only useable in symfony (possibly laravel) applications.

It would be a great quality of life to allow users of other frameworks, namely CraftCMS, to submit their local twig environments. Craft doesn't come with such a command out of the box, but it's possible to collect all the information which symfony command retrieves, and send it through a console command.

I have tried fiddling with your source code locally, but I am not familiar with the VSCE API and I am too busy at the moment to learn it and to be able to contribute with a PR.

I have found that the easiest way to go about it would be to make this line return the entire command and remove the hard-coded part, and then no additional work would need to be implemented. That way everyone would have to provide the whole console command, but it would work the same way for everyone.

Thank you for your good work,

Best regards,
Marko

@moetelo
Copy link
Owner

moetelo commented Apr 4, 2024

make this line return the entire command and remove the hard-coded part

Making that line return the entire command will make other developers reimplement the same logic of collecting all of the information. That is not desired.

Better: introduce the new collection logic for CraftCMS inside of the Twiggy language server.

We could detect Symfony and CraftCMS projects separately, and if it is the Craft project, then we will run some other logic instead.

The next question here is how to collect paths, globals, functions and filters in CraftCMS.

As far as I understand, Craft is built on top of Yii. Does Yii have such kind of command out of the box?
If no, how could we collect such data manually?

The farthest I could get is this: https://github.com/craftcms/cms/blob/856f45f2766b1a1df5db437b5cd3fe5cce896b5b/src/web/View.php#L364-L406
I think we could provide some PHP code to resolve environments in Craft/Yii.

@moetelo moetelo added the help wanted Extra attention is needed label Apr 4, 2024
@marcusgaius
Copy link
Author

To my knowledge, Yii2 doesn't implement Twig out of the box in any way, it's purely Craft.

The next question here is how to collect paths, globals, functions and filters in CraftCMS.
The farthest I could get is this: craftcms/cms@856f45f/src/web/View.php#L364-L406

In addition to the setter you linked here, there is also the getter just above it. It retrieves the instance of the Twig\Environment that is centrally used in Craft to provide all globals, tests, functions and filters.
Paths are provided directly from the Craft's View module.

I have managed to get all the relevant information for the project by using the snippet below, although, it should be parsed/"JSONified" for metadata, similarly (or, more likely, exactly) how it's done in Symfony, especially the globals, as they mix twig globals and craft global sets, which can branch out infinitely, and will always exhaust the memory.

/** @var \craft\web\View $view */
$view = Craft::$app->getView();
$twig = Craft::$app->getView()->getTwig();

$tests = $twig->getTests();
$functions = $twig->getFunctions();
$filters = $twig->getFilters();
$globals = $twig->getGlobals();
$paths = $view->getSiteTemplateRoots();

The caveat is that there is no OOB way to use this in Craft, and any implementation is (for now) strictly per-project, but having this feature in the extension might incentivize the creators of Craft to provide such a command of their own.

@moetelo moetelo changed the title Support for other frameworks using Twig CraftCMS support Apr 6, 2024
@moetelo moetelo self-assigned this Apr 6, 2024
@moetelo moetelo linked a pull request Apr 6, 2024 that will close this issue
@moetelo moetelo removed the help wanted Extra attention is needed label Apr 6, 2024
@moetelo moetelo mentioned this issue Apr 6, 2024
@moetelo
Copy link
Owner

moetelo commented Apr 6, 2024

#11 is a draft, although is open for suggestions and testing. Kinda works on my machine, let's see if it does on yours.

Tested on DDEV starter with php-legacy arch package.

image

@marcusgaius

This comment was marked as off-topic.

@moetelo

This comment was marked as off-topic.

@moetelo
Copy link
Owner

moetelo commented Apr 8, 2024

Let's call it a Basic CraftCMS support, and for extended support create new issues.

#15

@moetelo moetelo added the feature New feature or request label Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants