0
votes

So I have a CakePHP 3 project and want to load FluentDOM, a PHP plugin not specifically written for CakePHP. According to both software documentations, Composer is the way to go. In my understanding, all I would have to do is the following:

  1. run composer require fluentdom/fluentdom in powershell
  2. run composer require fluentdom/selectors-phpcss in powershell

OR

  1. add the following to composer.json in the project's root directory:

"require": { "fluentdom/fluentdom": "^7.0", "fluentdom/selectors-phpcss": "^1.1" }

  1. run composer update in powershell

Both ways will install the desired plugins to vendor/fluentdom/{pluginname}/ as expected, but /vendor/cakephp-plugins.php won't include them, as implied by CakePHP's plugin installation manual.

The following attempt to load either plugin in a controller by writing

use Cake\Core\Plugin;
Plugin::load('fluentdom/fluentdom');
Plugin::load('fluentdom/selectors-phpcss');

would cause an exception that the desired plugins were not found in plugins/ :

Make sure your plugin fluentdom/fluentdom is in the {absolute project path}\plugins\ directory and was loaded

-- Which is already odd, because Composer wouldn't install anything there to begin with.

I found that I might get around this issue by manually extending vendor/cakephp-plugins.php to include the correct paths:

'fluentdom/fluentdom' => $baseDir . '/vendor/fluentdom/fluentdom/',
'fluentdom/selectors-phpcss' => $baseDir . '/vendor/fluentdom/selectors-phpcss/'

(However, that doesn't seem the way to go, because this file is auto-generated and overwritten by Composer after every update.)

And even then, the final issue still persists: although the plugins seem to be loaded successfully (confirmed by running Plugin::loaded()), I'd finally get the following exception when trying to access FluentDOM's classes as described in their wiki:

$document = new FluentDOM\DOM\Document();

Class 'App\Controller\FluentDOM\DOM\Document' not found

Does the plugin miss out on having its' autoload executed?

Even extending the line in my controller to Plugin::load('fluentdom/fluentdom', ['autoload' => true]);, but doesn't seem to help either; according to CakePHP's doc, that shouldn't be necessary anyway.

So what am I missing?

1
When the CakePHP docs refer to plugins, they are only talking about CakePHP plugins. What you have is what they would call vendor files. - Greg Schmidt

1 Answers

0
votes

Found it! First of all, I had the false presumption that Plugins and Vendor Packages are more or less the same: they are not; thanks to Greg Schmidt for pointing this out in the question's comments.

The issue was in the line of how I tried to access FluentDOM's class. While

$document = new FluentDOM\DOM\Document();

worked in a standalone php file, it didn't within the Cake project; I was missing a backslash:

$document = new \FluentDOM\DOM\Document();

So, the entire path of actions to load a Vendor Package is merely:

  1. run composer require fluentdom/fluentdom in powershell
  2. run composer require fluentdom/selectors-phpcss in powershell
  3. Use the new classes right away with $document = new \FluentDOM\DOM\Document();

No further steps required. Side note: Composer seems to refresh autoload config after installing a vendor file with composer require {vendor}/{package}, but in case it doesn't, or autoload config is messed up from earlier experiments, composer dumpautoload should fix it.