There are some useful programs written with Composer in mind, meaning the author/s have used some parts of some other program and using Composer it will download these dependencies and make the appropriate configuration, namespacing etc.
I would like to use some of these programs in WordPress in my plugin with using require_once() and including the program(app) so I can use its functions from my WordPress plugin.
How to do that?
I would like to use some of these programs in WordPress in my plugin with using require_once() and including the program(app) so I can use its functions from my WordPress plugin.
Thats possible, but a really bad practice.
You have a plugin folder with a working wordpress plugin.
You add composer.json and require a third-party package.
You trigger composer install and the third-party package gets fetched into the folder vendor inside your plugin folder.
Inside your plugin you include vendor\autoload.php, which is the Composer Autoloader. That's the autoloader for all third-party plugins.
Then you might start to work with third-party classes (without require).
Again, regarding the loading: your plugin is loaded by Wordpress. But your plugin loads its external dependencies itself by including the Composer Autoloader.
When everybody includes Composer packages on the plugin level, then different Wordpress plugins might ship the same Composer packages, right? Ok, lets add 50 different plugins with 10 times the same dependency: crap-ton inside crap-ton.
In other words: including Composer packages directly in plugins works, but is a really bad practice for the Wordpress ecosystem.
The correct way to solve this would be to use Composer on the application level (Wordpress) and not on the plugin level. That means that Wordpress has the vendor folder and all plugins might include code from there - instead of multiple plugins possibly having a vendor folder each.
There are a lot of different approaches out there to handle things a bit more centralized. I really don't know what the current state of Composer integration on the Wordpress core is. That answer is better given by a member of the Wordpress team. In other words: ask for community guidance over at the official Wordpress support.
http://wpackagist.org/
https://github.com/coenjacobs/wordpress-composer-installer
Composer/Installers Wordpress
Lets go through the last one: "Composer Installer for Wordpress"
This installer handles plugin, theme and muplugin package types. See https://github.com/composer/installers/blob/master/src/Composer/Installers/WordPressInstaller.php
How does that work? You need to add a composer.json to your plugin and add "wordpress-plugin" as "type" and require the "composer installer" as a dependency of the package.
{
"name": "my/plugin",
"type": "wordpress-plugin",
"require": {
"composer/installers": "v1.0.6"
}
}
When you run composer install, Composer will fetch the composer/installer and recognize the type "wordpress-plugin". It will then install the plugin into wp-content/plugins/{$name}/.
You can also add more packages to the "require" section.
Related
I'm trying to install the Coinbase PHP API but it requires Composer:
https://github.com/coinbase/coinbase-php
I'm looking for a universal PHP solution (perhaps a function) to let me install composer packages directly onto my server, without having to use Composer.
I think the developers of Composer believe they are helping people, but actually there are thousands of beginner developers that are being locked out of learning web development by the 'Composer barrier'.
It would really help if there was a flexible solution or some approach where we could install without Composer? How can I do this?
Please don't respond with some sarcastic comment. There are people that don't want to use Composer and I don't see why we should be herded into a specific third-party software in order to do web development.
You can try https://php-download.com/ which can help you download all dependency most of the time along with vendor folder. It promises composer not required.
Tried it myself. It finds and creates all required folders and zips it for download. Works perfectly !!
The composer.json file lists the dependencies. In your example:
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "^6.0",
"psr/http-message": "^1.0",
"psr/log": "^1.0"
},
You must then find the corresponding packages in the packagist site. Repeat the same process for each dependency: find additional dependencies in their corresponding composer.json files and search again.
When you finally have a complete list of the required packages, you only need to install them all one by one. For the most part, it's just a matter of dropping the files somewhere in your project directory. But you must also ensure that PHP can find the needed classes. Since you aren't using Composer's auto-loader, you need to add them to your own custom autoloader. You can figure out the information from the respective composer.json files, e.g.:
"autoload": {
"psr-4": { "Coinbase\\Wallet\\": "src/" }
},
If you don't use a class auto-loader you'll need to figure out the individual require_once statements. You'll probably need a lot of trial and error because most library authors won't care documenting that.
Also, and just in case there's confusion about this:
Composer has an official GUI installer for Windows and a copy and paste command-line installation procedure for all platforms.
Composer can be run locally and its output just uploaded elsewhere. You don't need SSH in your shared hosting.
The command needed to install a library can be copied and pasted from the package web site—even if the package maintainer didn't care to document it, packagist.org generates it by default.
Composer is not perfect and it doesn't suit all use cases but, when it comes to installing a library that relies on it, it's undoubtedly the best alternative and it's a fairly decent one.
I've checked other answers that came after mine. They mostly fall in two categories:
Install a library and write a custom download script with it
Use an online web based interface for Composer
Unless I'm missing something, none of them address the complaints expressed by the OP:
Learning curve
Use of third-party software
Possibility to develop right on the server (using SSH, I presume)
Potentially deep dependency tree
I'm using shared hosting for a website and can't execute commands there. Aside from running composer via php script request that I request via browser, I usually use this workflow:
Make sure you have php installed locally.
Make directory on desktop.
download composer.phar from https://getcomposer.org/download/ (under header *Manual Download) and place it in the directory.
make a file composer.json paste in it the following contents
{
"require": {
"coinbase/coinbase": "~2.0"
}
}
Browse to the directory with the shell of your choice(bash, git-bash, cmd, windows bash)
type php composer.phar update
Upload the vendor directory to your webserver via ftp or whatever mechanic you use.
include in your php project where you load your libraries(modify path to where you uploaded the vendor dir so it will include that autoload file)
require_once('vendor/autoload.php');
This way you get the benefit of dependency management and you don't have to include manually all the gazillion of files and download all the dependencies manually, and updating them is just as easy as typing php composer.phar update and then replacing the vendor dir on your server with the new one.
An alternative solution that worked for me (since php-download was down) can be done by making your own little local composer downloader.
Download and install XAMPP locally: https://www.apachefriends.org/index.html
Download and install composer locally: https://getcomposer.org/download/
Open commandprompt, navigate to say c:\temp and and simply type the composer dependancy, for example: composer require league/oauth2-client
Copy the files from your c:\temp folder to your web host using an FTP program
Add this to the top of your php: require("vendor/autoload.php");
This is not the ultimate solution but for me it was a big help for most of the cases:
https://github.com/Wilkins/composer-file-loader
Allow you to load composer.json file just as composer would do it.
This allow you to load composer.json file without composer (so
theorically PHP 5.2 is enough)
I know the question is old but I hope it will help someone.
I had to do this for an FTP server I didn't have SSH access to. The site listed in here worked, then I realized you can just do a composer install on your own server (using your target's PHP version), then copy all the files over.
Analizing the problem
The problem in installing the dependencies without Composer is the autoloading system.
Composer use a homemade autoloader based on an array map, this is a de-facto standard.
But this autoloading system, "fortunally" in this case, is not PSR-4 compliant.
PSR-4 is the de-iure standard for autoload a class in PHP, so you can't escape from autoloading. You must use one of them.
Solution Proposal
In this case, this brilliant PSR-4 autoloader is capable to be manually configured to autoload a VendorClass in a VendorNamespace anywhere in your code, as long as you require the custom autoload.php file early in your source code.
Real life example
Let's look at this example:
I have a legacy project who can't and won't use Composer never and never even if God allow this with a miracle. This project can be speed up in development with this fantastic package for command line scripts.
This is my project directory structure:
- src
- tests
- vendor (not the Composer's one)
This package has this directory structure:
- examples
- src
- Commando
- tests
The only thing I need is the src folder. Placing this folder in my vendor folder would be fine. So my custom autoloader would be like this:
// Constants
$base_path = "path\to\my\project";
$autoloader_class = '\vendor\MarcoConsiglio-Wichee\PSR-4-Autoloading\Psr4AutoloaderClass.php';
define("BASE_PATH", str_replace("\\", DIRECTORY_SEPARATOR, $base_path));
// Autoloader
require_once BASE_PATH.'\vendor\MarcoConsiglio-Wichee\PSR-4-Autoloading\Psr4AutoloaderClass.php';
// Init the autoloader.
$package = [
"nategood\commando" => [
"namespace" => "Commando",
"path" => str_replace("\\", DIRECTORY_SEPARATOR, '\vendor\nategood\commando\src\Commando')
],
"kevinlebrun\colors.php" => [
"namespace" => "Colors",
"path" => str_replace("\\", DIRECTORY_SEPARATOR, '\vendor\kevinlebrun\colors.php\src\Colors')
]
];
// Register namespaces.
$loader = new \PSR4\Psr4AutoloaderClass;
$loader->register();
// Namespace // Path to source
$loader->addNamespace($package["nategood\commando"]["namespace"], BASE_PATH.$package["nategood\commando"]["path"]);
$loader->addNamespace($package["nategood\commando"]["namespace"], BASE_PATH.$package["nategood\commando"]["path"]."\Util");
$loader->addNamespace($package["kevinlebrun\colors.php"]["namespace"], BASE_PATH.$package["kevinlebrun\colors.php"]["path"]);
Now I can use the command package anywhere in my project!
Pros & Cons
This solution allow you to:
Easely and manually build your own custom autoloader (you only need to specify the VendorNamespace and the folder(s) where search for VendorClasses in the VendorNamespace.
Freely organize your composer dependency anywhere in your project folder (and why not, outside it)
Import a composer package as is in your project (either downloading locally with Composer or cloning the package repository) or a relevant part of it (i.e removing composer.json file or files that require the composer autoloader).
Cons:
Manually build your custom autoloader means to work on every required dependency of your project (i hope not a lot).
Mistakes in package source paths can be tedious and frustrating.
Works only with PSR-4 compliant file names (i.e. can't use a A.class.php file name)
In a Shopware system, there's a main composer.json file. This manages the main project dependencies. In addition to this, Shopware plugins require you to add a composer.json file. If these plugins are added to the system via the main composer.json file, then the dependencies in a plugin's composer.json file will end up in the root level vendor/ folder. This I understand.
However, it appears that plugins can also be installed locally outside of the vendor folder, in either
./custom/plugins or
./custom/static-plugins
When a plugin is installed locally, how should its dependencies be managed?
Is the intent that, when installing a plugin locally you'll also add its dependencies to the main composer.json file? Or is there a way to tell shopware
Hey, install this plugin's dependencies
Also -- how do the ./custom/plugins/*/packages folders enter into this? It's my vague understanding that these are for private plugin dependencies, but I'm not sure what that means or how that code should be managed
Or am I misunderstanding the intent behind these local folders, and the expectation in Shopware 6 is that all plugins should be installed via your main composer.json file and the custom/ folders are just legacy?
Or some other thing?
If your plugin lives within the custom/static-plugins folder, you can just composer require my/plugin from the root. So that's the way to use composer plugins within your project. Shopware is then searching for the composer plugin within the custom/static-plugins/* repository.
Take a look at the Require project plugins section within the Docs on how to deal with requiring composer plugins.
Within the custom/plugins folder you have all your store plugins. That's also the reason why the content of the custom/plugins folder is not committed to your git repo. Because plugins within custom/plugins are coming from the Shopware store. However: You can still also just place your plugins within the custom/plugins folder if you want to.
Regarding custom/plugins/*/packages: You're right. Take a look at the adding private composer dependencies section within the docs.
Is it possible to install B2B Suite in Shopware 6 with Composer?
My approach so far has been
composer require shopware/b2b
php bin/console plugin:install SwagB2bPlatform
But this leads to an error when I try to install the plugin
The class "SwagB2bPlatform\SwagB2bPlatform" is not found. Probably an class loader error. Check your plugin composer.json
Does anyone have any experience with this? Do I have to run any scripts to make sure the plugin has all the necessary resources?
Shopware has no way to simple way to require proprietary plugins. You should not use SwagB2bPlatform as composer repository via vcs as all needed assets get packed before the release.
You can setup packages.friendsofshopware.com for your projects. This is a community driven project which provides a repository of all plugins in the community story. You are limited to plugins that you bought in the community store. Using this repository needs an extra entry in the repositories in your composer.json. After that you can just require any plugin that is assigned to your project. In the search you can find the plugin you look out for: https://packages.friendsofshopware.com/?producers=shopware%20AG&term=b2b
I'm trying to install the Coinbase PHP API but it requires Composer:
https://github.com/coinbase/coinbase-php
I'm looking for a universal PHP solution (perhaps a function) to let me install composer packages directly onto my server, without having to use Composer.
I think the developers of Composer believe they are helping people, but actually there are thousands of beginner developers that are being locked out of learning web development by the 'Composer barrier'.
It would really help if there was a flexible solution or some approach where we could install without Composer? How can I do this?
Please don't respond with some sarcastic comment. There are people that don't want to use Composer and I don't see why we should be herded into a specific third-party software in order to do web development.
You can try https://php-download.com/ which can help you download all dependency most of the time along with vendor folder. It promises composer not required.
Tried it myself. It finds and creates all required folders and zips it for download. Works perfectly !!
The composer.json file lists the dependencies. In your example:
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "^6.0",
"psr/http-message": "^1.0",
"psr/log": "^1.0"
},
You must then find the corresponding packages in the packagist site. Repeat the same process for each dependency: find additional dependencies in their corresponding composer.json files and search again.
When you finally have a complete list of the required packages, you only need to install them all one by one. For the most part, it's just a matter of dropping the files somewhere in your project directory. But you must also ensure that PHP can find the needed classes. Since you aren't using Composer's auto-loader, you need to add them to your own custom autoloader. You can figure out the information from the respective composer.json files, e.g.:
"autoload": {
"psr-4": { "Coinbase\\Wallet\\": "src/" }
},
If you don't use a class auto-loader you'll need to figure out the individual require_once statements. You'll probably need a lot of trial and error because most library authors won't care documenting that.
Also, and just in case there's confusion about this:
Composer has an official GUI installer for Windows and a copy and paste command-line installation procedure for all platforms.
Composer can be run locally and its output just uploaded elsewhere. You don't need SSH in your shared hosting.
The command needed to install a library can be copied and pasted from the package web site—even if the package maintainer didn't care to document it, packagist.org generates it by default.
Composer is not perfect and it doesn't suit all use cases but, when it comes to installing a library that relies on it, it's undoubtedly the best alternative and it's a fairly decent one.
I've checked other answers that came after mine. They mostly fall in two categories:
Install a library and write a custom download script with it
Use an online web based interface for Composer
Unless I'm missing something, none of them address the complaints expressed by the OP:
Learning curve
Use of third-party software
Possibility to develop right on the server (using SSH, I presume)
Potentially deep dependency tree
I'm using shared hosting for a website and can't execute commands there. Aside from running composer via php script request that I request via browser, I usually use this workflow:
Make sure you have php installed locally.
Make directory on desktop.
download composer.phar from https://getcomposer.org/download/ (under header *Manual Download) and place it in the directory.
make a file composer.json paste in it the following contents
{
"require": {
"coinbase/coinbase": "~2.0"
}
}
Browse to the directory with the shell of your choice(bash, git-bash, cmd, windows bash)
type php composer.phar update
Upload the vendor directory to your webserver via ftp or whatever mechanic you use.
include in your php project where you load your libraries(modify path to where you uploaded the vendor dir so it will include that autoload file)
require_once('vendor/autoload.php');
This way you get the benefit of dependency management and you don't have to include manually all the gazillion of files and download all the dependencies manually, and updating them is just as easy as typing php composer.phar update and then replacing the vendor dir on your server with the new one.
An alternative solution that worked for me (since php-download was down) can be done by making your own little local composer downloader.
Download and install XAMPP locally: https://www.apachefriends.org/index.html
Download and install composer locally: https://getcomposer.org/download/
Open commandprompt, navigate to say c:\temp and and simply type the composer dependancy, for example: composer require league/oauth2-client
Copy the files from your c:\temp folder to your web host using an FTP program
Add this to the top of your php: require("vendor/autoload.php");
This is not the ultimate solution but for me it was a big help for most of the cases:
https://github.com/Wilkins/composer-file-loader
Allow you to load composer.json file just as composer would do it.
This allow you to load composer.json file without composer (so
theorically PHP 5.2 is enough)
I know the question is old but I hope it will help someone.
I had to do this for an FTP server I didn't have SSH access to. The site listed in here worked, then I realized you can just do a composer install on your own server (using your target's PHP version), then copy all the files over.
Analizing the problem
The problem in installing the dependencies without Composer is the autoloading system.
Composer use a homemade autoloader based on an array map, this is a de-facto standard.
But this autoloading system, "fortunally" in this case, is not PSR-4 compliant.
PSR-4 is the de-iure standard for autoload a class in PHP, so you can't escape from autoloading. You must use one of them.
Solution Proposal
In this case, this brilliant PSR-4 autoloader is capable to be manually configured to autoload a VendorClass in a VendorNamespace anywhere in your code, as long as you require the custom autoload.php file early in your source code.
Real life example
Let's look at this example:
I have a legacy project who can't and won't use Composer never and never even if God allow this with a miracle. This project can be speed up in development with this fantastic package for command line scripts.
This is my project directory structure:
- src
- tests
- vendor (not the Composer's one)
This package has this directory structure:
- examples
- src
- Commando
- tests
The only thing I need is the src folder. Placing this folder in my vendor folder would be fine. So my custom autoloader would be like this:
// Constants
$base_path = "path\to\my\project";
$autoloader_class = '\vendor\MarcoConsiglio-Wichee\PSR-4-Autoloading\Psr4AutoloaderClass.php';
define("BASE_PATH", str_replace("\\", DIRECTORY_SEPARATOR, $base_path));
// Autoloader
require_once BASE_PATH.'\vendor\MarcoConsiglio-Wichee\PSR-4-Autoloading\Psr4AutoloaderClass.php';
// Init the autoloader.
$package = [
"nategood\commando" => [
"namespace" => "Commando",
"path" => str_replace("\\", DIRECTORY_SEPARATOR, '\vendor\nategood\commando\src\Commando')
],
"kevinlebrun\colors.php" => [
"namespace" => "Colors",
"path" => str_replace("\\", DIRECTORY_SEPARATOR, '\vendor\kevinlebrun\colors.php\src\Colors')
]
];
// Register namespaces.
$loader = new \PSR4\Psr4AutoloaderClass;
$loader->register();
// Namespace // Path to source
$loader->addNamespace($package["nategood\commando"]["namespace"], BASE_PATH.$package["nategood\commando"]["path"]);
$loader->addNamespace($package["nategood\commando"]["namespace"], BASE_PATH.$package["nategood\commando"]["path"]."\Util");
$loader->addNamespace($package["kevinlebrun\colors.php"]["namespace"], BASE_PATH.$package["kevinlebrun\colors.php"]["path"]);
Now I can use the command package anywhere in my project!
Pros & Cons
This solution allow you to:
Easely and manually build your own custom autoloader (you only need to specify the VendorNamespace and the folder(s) where search for VendorClasses in the VendorNamespace.
Freely organize your composer dependency anywhere in your project folder (and why not, outside it)
Import a composer package as is in your project (either downloading locally with Composer or cloning the package repository) or a relevant part of it (i.e removing composer.json file or files that require the composer autoloader).
Cons:
Manually build your custom autoloader means to work on every required dependency of your project (i hope not a lot).
Mistakes in package source paths can be tedious and frustrating.
Works only with PSR-4 compliant file names (i.e. can't use a A.class.php file name)
I am having some weird issues with git submodule update for a Plugin dependency with Jenkins # CloudBees.
So I am switching some, if not, all my dependencies from git submodule to Composer.
I came across this repo called composer installers. https://github.com/composer/installers
I was wondering how to use this for both Plugin and Vendor dependencies.
I am not familiar with Composer and even after reading the docs, I am uncertain how to say, place this dependency specifically to Plugin/xxx
I know how to do this with git submodule add.
So anyone knows how I should use Composer or better yet, Composer installers, please advise me.
To make things easier, I want to use 2 actual examples.
https://github.com/milesj/Utility is to be placed inside my app/Plugin/Utility
https://github.com/simkimsia/php-gd-simpleimage is to be placed inside my app/Vendor/SimpleImage
There is a Composer plugin for CakePHP that has a Backery article about it. The code is available on Github:
https://github.com/uzyn/cakephp-composer
It's actively being developed (last commit was yesterday), but in my early use of it (today), it seems to be working as expected.
Packagist has loads of Compose-ready libraries. Some of them are CakePHP related. Some are not.
The two examples you listed aren't in Packagist (yet?). Thankfully, Composer makes it possible to work directly with Git (and other VCS) repos. For the milesj/Utility plugin (which has a composer.json file), you'll need to follow the Repositories guide in the Composer docs to set things up properly.
For the php-gd-simpleimage repo, you'll need to write a composer.json file, then follow the Repositories steps.
One of the most confusing things about Composer is that composer.json is the same system/file-format for both libraries and "projects." Really, they're all the same to Composer. In your "project" repo, though, you're only outlining requirements (usually), not making your application installable via Composer. Regardless of their locations, both composer.json files are for the same thing: tracking and installing dependencies. You can imagine it as a tree with your project (and it's composer.json) at the top, and then a branching dependency tree all the way down.
Happy Composing!