How to register a local directory as a composer package - wordpress

I can't figure out how to register a local directory as a composer package. I found lots of information about registering a local folder that is either a git repository or a composer package or even a zip hosted somewhere.
However I want to register a folder on my local machine, residing in the same repository as my wordpress installation (using wordplate) that is neither a git repository nor a composer package as it doesn't contain any composer.json.
What I try at the moment is to register it like this under repositories:
{
"type": "package",
"package": {
"name": "local/contact-form-7-mailchimp-extension",
"type": "wordpress-plugin",
"version": "dev-master",
"source": {
"type": "path",
"url": "resources/plugins/contact-form-7-mailchimp-extension",
"reference": "master"
}
}
}
Add "local/contact-form-7-mailchimp-extension": "*" under "require",
add "local/contact-form-7-mailchimp-extension" in "public/plugins/{$name}" under "extras" > "installer-paths".
Running composer install however doesn't install it and gives no information at all. Don't even know if it found the url or not..
Is it even possible to install a local directory like that?

You can use the path repository to install packages from a different path into your project.
However I want to register a folder on my local machine, residing in the same repository as my wordpress installation (using wordplate) that is neither a git repository nor a composer package as it doesn't contain any composer.json.
For this to work, your plugin inside the folder will need a composer.json, though. This is necessary for composer to recognize this folder as a package in the first place. Fortunately, it can be as simple as this:
{
"name": "local/contact-form-7-mailchimp-extension",
"description": "My Contact Form-extension using mailchimp",
"type": "wordpress-plugin",
"version": "dev-master"
}
Assuming the plugin is in the directory resources/plugins/contact-form-7-mailchimp-extension, you can now register this path as a repository path, meaning composer will look inside this folder for a composer.json and when your wordpress-project requires the provided package it will (by default) be symlinked into vendor.
The wordpress project's composer.json then needs these entries (the options for the repository are the defaults and can be left out, this is just to show how to disable symlinking, should you need that):
{
...,
"repositories": [
{
"type": "path",
"url":"resources/plugins/contact-form-7-mailchimp-extension",
"options": {
"symlink": true
}
}
],
"require": {
"local/contact-form-7-mailchimp-extension": "dev-master",
...
}
}
When composer installs the package, it should tell you where it installed it from, i.e. you should find the path from the repository in there.

Related

Use Composer with 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.

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.

.Net Core 1.0 Relative Project Dependencies Not Found

I have the project structure:
/src
- common
- common-x
+ project.json
- module-a
- project-a
+ project.json
- project-a-tests
+ project.json
+ global.json
I'm trying to include the common-x project using relative file paths in the global.json file.
The global.json file in the module-a directory is as follows:
{
"projects": [
"project-a",
"project-a-tests",
"../common/common-x"
]
}
and the project.json file in project-a is
{
// Default config options are also in this...
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"common-x": {
"target": "project"
}
}
}
project-a-tests has no problem referencing project-a since they are in the same directory, but project-a can't find the common-x project. I receive a "Unable to resolve ..." error from dotnet restore.
I have tried using absolute paths but that doesn't work either. The closest solution so far is to use symlinks but that is not preferable.
Note: This is all being run on Ubuntu 16.04.
You should only need to specify top level folders in your global.json file, since sub-folders will be scanned automatically. Global.json reference.
So your global.json should look like this.
{
"projects": [ "src" ]
}
If you are still getting any dependencies issues that might related to compatibility problems between projects/modules, however I would need to see the exact output you are getting to troubleshoot that.
UPDATE
A few tips that might be useful:
Delete old project.json.lock files
Add a .sln solution file if you don't have one created.
UPDATE 2
As per your comment, the working solution was to move global.json into src folder, and list your top-level folders in the projects array.

PHPUnit composer installation on WAMP not working

I am following a Lynda.com Tutorial and can't get PHPUnit to work.
Here is what I did:
Project folder: C:\wamp\www\stats
In this folder I have a composer.json file:
{
"require": {
},
"require-dev": {
"phpunit/phpunit": "*"
},
"autoload": {
"psr-0": {
"stats": ""
}
}
}
I ran the composer command:
>composer update
It created a new folder C:\wamp\www\stats\vendor containing many subfolders, even /Symfony...??
Then I opened cmd.exe and wanted to run phpunit:
>cd C:\wamp\www\stats\
>phpunit
But it doesn't work. Error message:
Command "phpunit" not found
Can anyone help?
As described in documentation:
For a system-wide installation via Composer, you can run:
composer global require "phpunit/phpunit=5.0.*"
I recommend to not use version as of last stable is 5.2
Note that you should use version ^4.8 for php < 5.6
Also you need to add composer folder (C:\Users\<username>\AppData\Roaming\Composer\vendor\bin) to your path enviroment variable

update to silverstripe 3.1 using composer

Is it possible to update my project to the beta release of Silverstripe 3.1 using composer?
I have composer installed as well as a composer.json file in the root of my Silverstripe project. Here is my composer.json file:
{
"name": "silverstripe/installer",
"description": "The SilverStripe Framework Installer",
"require": {
"php": ">=5.3.2",
"silverstripe/cms": "3.0.*#stable",
"silverstripe/framework": "3.0.*#stable",
"silverstripe-themes/simple": "*"
},
"require-dev": {
"silverstripe/compass": "*",
"silverstripe/docsviewer": "*"
},
"minimum-stability": "dev"
}
When I run
composer update /Path/To/My/Site
Composer tells me that it cannot find the composer.json file even though it is there.
Am I running the wrong command?
Thanks.
You'll need to remove the "composer.lock" file (if existing), replace "3.0.*#stable" with "3.1.x-dev", and call "composer update". That'll get you the latest 3.1 branch.
Its not possible to upgrade to 3.1.0-beta1 this way, because I've mucked up the composer.json dependencies in the tag. You'll need to create a new project for that, based on the composer.lock file committed to this tag:
composer create-project silverstripe/installer test 3.1.0-beta1

Resources