Use composer ton install depedencies while creating custom WP plugin - wordpress

I'm really new to composer and don't understand it well (yet).
Here's the thing :
I'm building a Wordpress plugin that needs external libraries.
Thoses libraries are FluentDOM and Selectors-Symfony for FluentDOM.
Both have installation instructions for Composer only :
FluentDOM :
FluentDOM is available on Packagist.org, just add the dependency to your composer.json.
{
"require" : {
"fluentdom/fluentdom": "5.x"
}
}
Selectors-Symfony :
composer require symfony/css-selector
My plugin path is /wordpress/wp-content/my-custom-plugin.
Should I write a composer.json file at the root of this directory, and what should be its content ?
Eventually, I would like install those depedencies in /wordpress/wp-content/my-custom-plugin/_inc/lib
Could anyone explain me how to do this ?
Thanks !

Well, I'd say that unless Wordpress starts supporting Composer (they don't officially at this time although Wordpress can be installed with Composer if you know what you're doing, first and foremost know the package name of it), you shouldn't think too much about using it for delivery of your plugin, meaning: If you use other software in your plugin, I think you have to bundle it inside your plugin, or it won't work.
It still will create issues like "Is the version of the library you are using compatible with the same library other plugins use?" and "How do you do autoloading?" correctly.
Internally, you could use Composer to manage these libraries just like you would do with any other project that uses Composer, with the minor difference that the released package of your plugin must include all these libraries and autoloaders you added - with Composer or something else.
Be warned that I basically don't know anything about how Wordpress people usually organize their stuff. Reading the discussion I linked to in the comment to your question, I get the impression that they have still a very long way to go, and that there is nobody actively behind it and promotes using Composer for dependency management because it also works the usual way, or it might break things.

Internally, you could use Composer to manage these libraries just like
you would do with any other project that uses Composer, with the minor
difference that the released package of your plugin must include all
these libraries and autoloaders you added - with Composer or something
else.
Thanks Sven, that is what I wanted to know.
I finally managed to do it.
Here is my step-by-step guide to install a dependency (here, fluentDOM) into /wordpress/wp-content/plugins/my-custom-plugin/_inc/php with the terminal and without having any composer.json at the start.
First, of course, you need to install Composer. As I will use it for php dependencies of my plugin, I will install it in my-custom-plugin/_inc/php.
(You could also install it at the root of your plugin and adjust following commands)
1/ Open Terminal and go to that directory :
cd /Applications/MAMP/htdocs/my-project/wordpress/wp-content/plugins/my-custom-plugin/_inc/php
2/ Install Composer :
curl -sS https://getcomposer.org/installer | php
Now I'm ready to use Composer in my-custom-plugin/_inc/php.
On the fluentDOM website, I see that the package I need to install is called fluentdom/fluentdom.
3/ So, let's install the package :
composer require fluentdom/fluentdom
If you need more informations about this package, the website packagist could be useful. It shows informations (version, dependencies), ... for composer packages. See fluentdom/fluentdom.
This installs fluentdom in the default composer directory /vendor and generates a composer.json file; which is nice to update dependencies later.
Here's the generated content:
{
"require": {
"fluentdom/fluentdom": "~5.2"
}
}
But we wanted our dependency to be installed into my-custom-plugin/_inc/php, not into my-custom-plugin/_inc/php/vendor !
4/ Let's edit composer.json, and set the default directory parameter vendor-dir to empty.
{
"require": {
"fluentdom/fluentdom": "~5.2"
},
"config": {
"vendor-dir": ""
}
}
5/ Delete the my-custom-plugin/_inc/php/vendor directory as we don't need it anymore.
6/ Now that we have a composer.json file, we just have to run
composer.phar install
Or
composer.phar update
... and the magic happens ! Done !!! We have the dependencies installed, and an autoload.php file generated.
7/ The last thing to do is to include the autoload.php in your plugin :
require_once( plugin_dir_path( __FILE__ ) . '_inc/php/autoload.php' );
This was the way to achieve it without having a composer.json file.
If you have your composer.json file ready, skip steps 3 to 5.
I also suggest to read this blog post : 5 features to know about Composer PHP.

Related

How to instal modules with Drupal 8 and Composer?

I installed Drupal 8 via composer with:
composer create-project drupal-composer/drupal-project:8.x-dev my_site --stability dev --no-interaction
This downloaded all the files and run composer install. According to this tutorial - https://www.drupal.org/node/2718229 - doing so this way will also configure composer.json to allow installation of modules, themes etc too via composer. Nice
However, I'm trying to install a new module:
$ composer require drupal/codesnippet
Using version ^1.6 for drupal/codesnippet
./composer.json has been updated
> DrupalProject\composer\ScriptHandler::checkComposerVersion
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing drupal/codesnippet (1.6.0)
Downloading: 100%
Writing lock file
Generating autoload files
> DrupalProject\composer\ScriptHandler::createRequiredFiles
However, when I go to Admin Bar > Extend > Install new module, I can search for the module and it says it's not installed yet. If I try to enable/install it from there it tells me I need to download and copy to the /libraries directory:
Before you can use the CKEditor CodeSnippet module, you need to download the codesnippet plugin from ckeditor.com and place it in /libraries/codesnippet. Check the README.txt for more information. Get the plugin here. (Currently using CodeSnippet version Plugin not detected)
Are these two completely different methods? How can I complete the installation with composer of this module?
Composer is a dependency manager, and whether or not third-party dependencies are included depends on how the module author managed their dependencies in the first place.
You aren't going to be able to complete the install via Composer alone, if a specific dependency isn't present on the repository that Composer downloads its packages from.
You're going to have to download the CKEditor CodeSnippet module from ckeditor.com. Composer can't manage that dependency for you, because that CKEditor plugin isn't a Composer package.
You can download it here: http://ckeditor.com/addon/codesnippet
Martyn, I guess you are confusing two different things into the same one: the drupal module and the external library required by the module.
The Drupal module codesnippet (https://www.drupal.org/project/codesnippet) is just a drupal integration module for the CKeditor addon with the same name, which you can download it (http://download.ckeditor.com/codesnippet/releases/codesnippet_4.6.2.zip) and place it in the drupal webroot /libraries folder manually (in your case my_site/web/libraries/ to be more specific - you have to create it if does not exist already).
Then you should be able to enable the drupal module.
PS: You could also add the library requirement in the composer.json library manually, which might be just a bit more complicated for beginners, because you also have to manually specify other things like a repository type, url and installer-paths for the extra external library that you need , but might be easier in the long run to deploy new Drupal8 installations with the same requirements just with a proper main composer.json file, without the need to go and manually download external libraries. There is a similar comment of mine(user zet) that you could read on this drupal dropzonejs module issue https://www.drupal.org/node/2853274

How to install vendor bundle WITHOUT composer (corporate network)

First of all, I can't use composer because Im under a corporate network. I tried everything to get composer working :
HTTP_PROXY, HTTPS_PROXY, HTTPS_FULLURI ... Nothing is working. Composer diag gives an OK status for http packagist but FAILS for https connectivity. The error it gives me is:
SSL : Handshake timed out.
But that's not my question, I spent to much time trying to get composer working (But if you got a solution, you'll make my day )
My real question is the following : How to install bundles manually
I want to install this bundle : http://knpbundles.com/pierredup/MenuBundle.
What I did to try installing the bundle :
Registering the bundle in appKernel.php :
new \CS\MenuBundle\CSMenuBundle()
Tried to add it in the autoload.php :
$loader->add('CS', __ DIR __.'/../vendor/CS/CSMenuBundle.php');
(Dont know how to add php code properly ... )
But it doesn't work, got the following error :
Attempted to load class "CSMenuBundle" from namespace "CS\MenuBundle".
Did you forget a "use" statement for another namespace?
And then, even if it is not a good practise, I tried to add it to autoload_namespaces.php and did a dump-autoload after that :
'CS\MenuBundle' => array($vendorDir. '/CS/')
I still have an error, but not exactly the same one :
Attempted to load class "CSMenuBundle" from namespace "CS\MenuBundle".
Did you forget a "use" statement for "CS\MenuBundle\CSMenuBundle"?
Now I'm a bit frustrating, I saw many posts (not on Stack) where people scream because we have to use composer to manage dependencies. I totally agree with that, but I can't, so I'm trying to find another way, and as I can't find any clear tutorial which explains how to install vendors without composer, here I am.
Note that I commented on the problems I see with your approach on your question directly.
However, I looked at the package you want to use to see if there would be ANY chance installing it somehow (preferring Composer). I don't think it is possible or feasible.
composer require customscripts/menubundle:dev-master - this would be the easy command for Composer to do everything. However there are problems:
The package you want to use is not registered on packagist.org, so there is no way to simply use Composer on a machine properly connected to the internet, grab the packages, zip them and transfer them to the place you need it.
To work around this, you'd manually add the repository to the composer.json file - this might actually work (however it takes way too much time on my VM). You'll end up with code that was last edited in the year 2012!
The dependencies of that code will likely not work anymore. The composer.json of that package lists "require": {"knplabs/knp-menu-bundle": "dev-master", "symfony/framework-bundle": ">=2.0,<2.3-dev", "jms/di-extra-bundle": "1.1.*"} - even the first "knplabs/knp-menu-bundle" will never work. Remember that the code of this package is from 2012 - now we are in 2016, so "knp-menu-bundle" has seen four years of development on the master branch. There simply is NO WAY of knowing which commit had been used of this package. You'd have to reverse-engineer this lost information.
Additionally, you see why Composer is awesome and doing it manually is bad: In addition to your wished package, you have to download the three additional packages mentioned here.
But detecting packages that have to be added is a recursive task: knp-menu-bundle has a dependency on knp-menu (with no further dependencies) and symfony/framework-bundle (already added). symfony/framework-bundle itself has a dependency on 9 more Symfony packages and doctrine/common... and so on. You have to detect every single package of this and download the correct version manually if you cannot use Composer.
Skipping your original package because that installation wasn't finishing while I was typing my answer, I tried to install knp-menu-bundle to see how many packages would be installed. Composer installed 20 packages, some of them using Symfony in 2.8 (it SHOULD be compatible with Symfony 2.2 stuff, shouldn't it) - and I simply ran composer require knplabs/knp-menu-bundle:1.1.1 to grab a similarly old version of knp-menu-bundle from 2012.
The strength of using Composer is that it supports rapid development by allowing updating quickly, downgrading reliably (if you commit your composer.lock file and always use tagged versions), and simply allowing to add new packages instantly. Not being able to use Composer is a very severe limitation for you as a PHP developer. Talk to your boss or team lead about your company's situation with the HTTPS proxy, and find a solution to use Composer. The alternative is to develop everything from scratch on your own or waste plenty of hours trying to fiddle with manual downloads that don't fit together easily.

How do you register bundles in Symfony2.1 designed for older versions of Symfony?

I am attempting to use they EntityAudit extension for Doctrine2 in my Symfony2.1 app.
I'm very new to this, and I've just started realizing how many "correct" methods there have been for installing new bundles for Symfony over the years. Some sort of "Deps" file used to exist but does no longer? When installing Symfony, "using Composer" was an option -- but purely an option, it seemed. Now I'm starting to think that's not true.
In EntityAudit's instructions it refers to "Autoload", and based on other things, I'm apparently supposed to modify the registerNamespaces array in my Autoload.php. Except I don't have that. So I found this link where the guy indicates Symfony2.1 doesn't do that anymore in favor of using Composer.
I don't really know how to use Composer in this case though. I don't really know how to use it at all, actually, but I seem to have bumbled through doing 1 or 2 basic things in it -- "updating" itself and "installing" .. vendors? Anyway, I can find no instructions general enough to be adapted for this need. Thanks in advance for any help!
The deps file is used in 2.0 to manage dependencies. The 2.1 version uses the much better Composer dependency management tool.
Install with composer
First you'll need some basix about composer. For instance, read this article: http://net.tutsplus.com/tutorials/php/easy-package-management-with-composer/
Before you can use composer to install a bundle you should look for a Packagist package of that bundle. For the SimpleThings\EntityAuditBundle you should look for a simplethings/entity-audit-bundle package and it does exists: https://packagist.org/packages/simplethings/entity-audit-bundle
SIDENOTE
Packagist is the main archive for Composer.
If you are searching for a bundle, the best thing you can do is check out
KnpBundles, it is the unofficial achive of Symfony Bundles.
If a bundle contains a README file, it is displayed there and if it has a Packagist
package it shows a link to the package. It's a really usefull site to begin searching
for bundles.
Now you have the package name, you should determine the version you want to use. As this is a not-finished bundle we can use the latest version by using the dev-master version. But it could be possible that a dev-master version is for Symfony2.2 and we should use another version if we use Symfony2.1, this should be in the README file (in the Package, which you can view on Github or KnpBundles). If it isn't in the README, you can use the version you want. An example of the note about version can be found in the StofDoctrineExtensionsBundle.
Now we can add the bundle to our composer.json file and update the dependencies. You can do this manually:
Add it to the composer.json file:
{
...,
"require": {
...,
"simplethings/entity-audit-bundle": "dev-master"
}
}
Update the dependency
$ php composer.phar update simplethings/entity-audit-bundle
or update all dependencies
$ php composer.phar update
Or you can do this is one command:
Run this command (which includes the package in the composer.json and updates the package)
$ php composer.phar require simplethings/entity-audit-bundle:dev-master
Now the bundle is installed into our Symfony project (in vendor/simpletings/) and the autoloader recognises this bundle. The only thing we need to do now is registering the bundle in the AppKernel:
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
// ...
public function registerBundles()
{
$bundles = array(
...,
new SimpleThings\EntityAudit\SimpleThingsEntityAuditBundle(),
);
// ...
}
}

Symfony2 Composer Not Downloading Packages

I was recently trying to re-install the fos:userbundle and noticed the docs have changed. They are no longer using the deps file are now referencing the new package manager composer.
I found some info about integrating composer with sf2.0.* here:
http://knplabs.com/blog/symfony2-with-composer
After downloading the src: https://github.com/KnpLabs/symfony-with-composer
I tried adding the following to my composer.json: "friendsofsymfony/user-bundle": "*"
as per the instructions: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md
When I run bin/vendors update I get the following:
Updating dependencies
Nothing to install or update
Writing lock file
Generating autoload files
It does not appear to be installing the fos package. Am I missing something?
Thanks
Composer is a PHAR archive that takes care of installing dependencies, update them, create projects, etc. It has nothing to do with the old bin/vendors.
What you need to do is to download Composer:
curl -s http://getcomposer.org/installer | php
And install your dependencies:
php composer.phar install
By the way, the symfony-with-composer thing you downloaded is an old version of Symfony2 Standard Distribution that isn't maintained anymore, as mentioned on the repository itself.

Symfony 2.0 bundle installation

So I am a new to doctrine, but I am not able to install a bundle at all. I am following the guide, but the "error" which I am getting is very unusual.
Anyhow, I add this lines into deps file:
[FOSRestBundle]
git=http://github.com/FriendsOfSymfony/FOSRestBundle.git
target=bundles/FOS/RestBundle
Then I do:
./bin/vendors install
And I get:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20090626+lfs/sqlite.so' - /usr/lib/php5/20090626+lfs/sqlite.so: cannot open shared object file: No such file or directory in Unknown on line 0
Your project seems to be based on a Standard Edition that includes vendors.
Try to run ./bin/vendors install --reinstall
So on this standard way I am not able to install it at all. Can somebody explain me what is the problem, because to me it looks like, the symfony vendors script doesnt recognize changes in deps file at all.
This happens when you've downloaded the Symfony2 Standard Edition from the website. The vendor install script checks to see if the vendor directories are git repositories, and if not, will throw this error. You can fix the situation in one of two ways:
you can either run the command that it suggests: php bin/vendors install --reinstall
or, you can remove the vendors directory, then run php bin/vendors install, which amounts to about the same thing
No need to install that. Just follow the steps in the url : http://mmoreramerino.github.com/GearmanBundle/installation.html

Resources