Use Composer with Wordpress - wordpress

Im trying to setup a pleasant way of working with wordpress and its plugins using composer. My question will be quite broad. How would you do it?
What i want is basically so it installs wordpress (which is now doing), but the plugins i specify get installed in a folder named "vendor" and not in the "plugins" folder. Why is that?
Here is my composer.json.
{
"name": "name",
"description": "name Wordpress",
"repositories":[
{
"type":"composer",
"url":"https://wpackagist.org"
}
],
"require": {
"timber/timber": "^1.3",
"johnpbloch/wordpress-core-installer": "^0.2.1",
"johnpbloch/wordpress": "^4.4"
},
"extra": {
"installer-paths": {
"wp-content/plugins/{$name}/": ["type:wordpress-plugin"],
"wp-content/themes/{$name}/" : ["type:wordpress-theme"]
},
"wordpress-install-dir": "wp"
}
}

I'm pretty new to this idea of using composer for package management inside WP. But I found this interesting, so I looked into it.
The installation path is specific to the plugin. Also look for wpackagist-plugin vendor name. Others will probably not put code inside the wp folder.
If you require "wpackagist-plugin/advanced-custom-fields": "^4.4" for example it is installed inside the plugins folder, as desired. The vendor prefix ('wpackagist-plugin') is important, I believe, as the packages in their search have no prefixes.
Quick solution
Try using "wpackagist-plugin/timber-library": "^1.3"
It is placed into the plugins folder nicely and comes with all it's dependencies inside the plugin folder.
Some more explanation
timber/timber is actually pulled from packagist.org (https://packagist.org/packages/timber/timber) instead of wpackagist.org.
On wpackagist.org you can find timber-library which is the packaged equivalent (includes composer autoloader and other deps).
This recipe for paths control (for plugin developers) says that:
To make use of it your extension's composer.json should contain:
"type" : "wordpress-plugin",
After you installed your packages using composer install you'll see, that the package inside of vendor/timber/timber doesn't have that type.
In fact there's an older WordPress plugin called timber, that's stuck on version 0.8. It's succeeded by timber-library.
Using "timber/timber": "^1.3" as from packagist.org
If you want to use timber as pulled from packagist.org you can do so, if you place the following line at the top of your wp-config.php:
require __DIR__ . '/wp-content/vendor/autoload.php';
Then you'll have to deploy both, the vendor and the wp folder.
There's also a discussion on GitHub about how to use vendor libraries in WP projects.
Hope you get along with that info.

Related

Wordpress: How to translate custom Composer package with wp i18n?

I'm creating a WP theme using Sage 10. I figured to create some Composer packages, hosted om GitHub, since I use multiple parts of code in multiple projects. To translate the theme I run yarn translate in the CLI, which runs wp i18n.
Now, this works fine for the theme, but how would I translate a package. So I don't have to re-translate it for each theme? How to store it in the package and for Wordpress to grab and use the translation?
-- Edit, as per request --
In my theme I use Composer as it is used by Roots Sage 10. In my composer.json I require my package with
"require": {
"my-name/mypackage": "^1.0.0",
...
},
My package and code are loaded by the autoloader of the theme. Now, in my theme I can translate the theme using wp i18n. The thing is, that I can translate the theme but not my vendor packages. So I would need to do it from the git of the package.
I guess this could be done, although I don't know how, yet. I suppose my composer package would become something like:
mypackage
- src
- languages
- mypackage.pot
- my-code.php
- composer.json
- init.php
Now, I would guess the should be a way to tell Wordpress there is a language translation inside the /src/languages. I would guess this would be done from the composer.json or the init.php.
But there is where my knowledge ends :)

phpunit composer install dependicies via symlink

I am currently installing phpunit on a per project basis.
This works extremely well, however, the install takes time because I install the directories from cache each time. Is there a way to symlink the directories to one install, or some kind of clever trick I can use to make it do that.
If I do a path repository like this, with phpunit cloned and composer installed on my disk:
"require-dev": {
"phpunit/phpunit": "dev-master"
},
"repositories": [
{
"type": "path",
"url": "../phpunit",
"options": {
"symlink": true
}
}
],
This installs only links the phpunit/phpunit directory, and not the rest of the dependencies like:
"phpunit/php-code-coverage": "^5.2",
"phpunit/php-file-iterator": "^1.4",
"phpunit/php-text-template": "^1.2",etc
Composer can install packages globally and then you can refer to the files via it's path, or put the appropriate directory into your PATH variable (in ~/.composer/vendor/bin). Another, often more popular method is to download the phpunit.phar file, and use that to run PHPunit. This also has the advantage of being a single file that can also be committed to a project - though you will want to occasionally update it to the latest version (at least within the major version, 5.7+ or 6.1+). The .Phar file can also be installed globally on the server, in the same way that composer is suggested to.

Magento 2.0 Unknown module(s): error when enabling custom module from command line

I've read Magento's DevDocs and Googled this problem but the usual missing registration.php answer doesn't apply here.
I'm on the released CE 2.0.0 version and I'm simply trying to enable my first minimum test module in magento/vendor/ but
bin/magento module:enable -c Tchsl_Test
results in:
Unknown module(s): 'Tchsl_Test'
I am basing this on the naming conventions and file positions of modules in vendor/magento/
In vendor/tchsl/module-test/etc/module.xml I have
<config xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Tchsl_Test" />
</config>
In vendor/tchsl/module-test/registration.php I have
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Tchsl_Test',
__DIR__
);
What am I missing?
I had this same problem and after some tinkering, discovered that our Modules actually belong under app/code/Tchsl/Test/. Move your module files there, and running the shell command module:status should show your disabled module.
You don't need to put your module under app/code/. In app/code/ Magento2 will search and find your module's registration.php. In Composer's vendor/ dir it doesn't do that, so we need to trick Composer into loading your module's registration.php.
If you'd check any Magento2 module's composer.json in vendor/magento/module-*, you'll see an "autoload" section which references the registration.php file. So Composer will autoload your module's registration.php which will "tell" Magento2 where your module is located.
This is a fragment from the Magento Checkout module's composer.json:
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\Checkout\\": ""
}
}
So if you have your module in a separate repository and loaded via composer, then that is the way to go. If you do not have it in a separate repository, then your module does not belong on vendor/ but in app/code/.
See also this Magento Stack Exchange post.

Symfony 2 - How to determine the namespace and Bundle name for autoload.php & Appkernel.php

I'm very new to symfony2 and I cannot find this info:
To register a bundle located in my vendor dir, how to determine the namespace and Bundle name for autoload.php & Appkernel.php?
For example, I have downloaded Luiggio's PHPExcel Bundle. I have place it in vendorDir/ExcelBundle/
Where content is:
namespace Liuggio\ExcelBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class LiuggioExcelBundle extends Bundle
{
}
What lines should I put in Appkernel and namespace.php?
This and this does not work:
new Liuggio\ExcelBundle\LiuggioExcelBundle()
//'Liuggio\\ExcelBundle' => array($vendorDir. '/PHPExcel'),
I cannot use composer or github repo at all, too many proxies and restrictions where I am.
You shouldn't place bundles in your vendor directory manually. Let Composer do this for you. Not only does Composer know where vendor libraries / bundles should be located, it also adds them to your autoload files and performs some other automated tasks.
To tell Composer which libraries are required, you should add them to your composer.json:
"require" : {
(...)
"liuggio/ExcelBundle": "~2.0"
},
Next, using the command line, run the composer update command:
$ php composer.phar update
(if you don't have a file composer.phar in your project directory, but you have Composer installed globally instead, use the following:)
$ composer update
This will tell Composer to download the required dependencies, update the autoload script, etc, all automatically. When it's finished, you're ready to go.
If you can't use Composer on your server, then run it locally before you upload your files (although I strongly recommend moving to a server that does allow you to use Composer).
The line you're trying to add to AppKernel.php is correct, however it only works after running Composer (or you'd indeed have to download the files and update the autoloader manually, but I'd strongly recommend against that).
Edit
If you really can't use Composer, do the following:
Place the files of ExcelBundle in the following directory:
vendor/liuggio/ExcelBundle/Liuggio/ExcelBundle
Your line in AppKernel.php was already correct.
Add this line to autoload_namespaces.php:
'Liuggio\\ExcelBundle' => array($vendorDir . '/liuggio/ExcelBundle'),
Last but not least, complain to your system administrator that he's making your work impossible with his stupid security measures.
new Liuggio\ExcelBundle\LiuggioExcelBundle(),
in AppKernel should be working fine.
This is the namespace of the LuiggioExcelBundle class + the class name. Look how your bundles are loaded, its the same.
What's your error?
You say vendorDir/ExcelBundle/ but its vendor/ExcelBundle right?
And what do you mean by namespace.php? :o
https://github.com/liuggio/ExcelBundle ==> there readme is rly easy to understand, it should help you.
For your "proxies and restriction", composer is a powerful tool, I can help you to use it. Download this soft, the free version is enough http://www.frozenway.com/ (if you cant read french, the 1st input in the header is to translate the website, English is Anglais) With this, you wont have any ports restriction.

Symfony 2.1 own vendor library

I want to put some code that I shall use in different projects outside the app's bundles. I put it in a new folder in the vendor folder but I can't included in the project. Which are the steps?
Should I follow a special structure of folder and namespaces
How do I included in my project.
On Google I found a lot of ambiguous answers. I tried also this but it did not work.
You need to get your library loaded. You do this by telling composer where to find your library.
The best thing would be to set up a git repository for this source code and then use composer to load it, keep it up to date and include it into the autoloader. You may need to get used to composer to do this, but as composer is getting the default dependency management tool for php, the time is not wasted. You can find the docs here: http://getcomposer.org/doc/
If you don't want to do this, you can add a new src code folder to composer. Let's say your folder structure is like this:
symfony2/
vendor/
yourlibrary/
Bla/
Blub/
MyClass.php
First, the MyClass.php should have the namespace Bla/Blub defined. Else Then you can add your library like this to the composer.json file:
"autoload": {
"psr-0": {
"": "src/",
"Bla": "vendor/yourlibrary/"
}
}
You already have autoload defined, so you need to overwrite it!
If your library doesn't have namespaces, you can define composer to load it nonetheless. I am not using this, so I can just link the docs where you can read it up: http://getcomposer.org/doc/04-schema.md#autoload

Resources