I'm trying to import a composer project into a plugin for wordpress. Is there a better way to do this than requiring every single asset? I do not plan to use composer to manage anything moving forward with this project, but I did use composer to install all requirements into the project folder (the plugin folder). Essentially, I'm trying to figure out how to convert a composer project to a wordpress plugin.
What you could do, load the autoloader from vendor/autoloader.php inside your functions.php. Then you could do something like this:
$loader = require $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
$loader->addPsr4('NAMESPACE\\', __DIR__ . '/app');
You are now autoloading the app folder.
How I use a composer packages into Wordpress plugin
I have a plugin's folder wp-content/plugins/stack-overflow/
Inside the folder I have the composer.json like next:
{
"name": "kumaxim/webhose-io-integration",
"license": "proprietary",
"authors": [
{
"name": "Your_name_here",
"email": "your_email_if#your_want.com"
}
],
"require": {
"php": "^5.4"
*** you can add other packages here ***
}
}
You have a plugin's file like next:
<?php
/**
* Plugin Name: The name of the plugin
*/
defined( 'WPINC' ) || die( 'Access restricted' );
function your_prefix_plugin_run() {
require_once __DIR__ . '/vendor/autoload.php';
}
your_prefix_plugin_run();
Pay attention, I strong recommend to call composer dependencies in plugin's bootstrap function. I have a problems in the past when I required it outside function.
What is next?
Add your dependencies and run composer install or composer update
Related
I'm developing a brand new Wordpress plugin and I would like to use Composer to autoload classes.
Here is the plugin directory heriarchy:
my composer.json content:
{
"autoload": {
"psr-4": {
"G4S_ECommerce\\": "src"
}
}
}
In the directory where composer.json is, on cmd, I execute:
composer install -> this generates the vendor/composer folder and the vendore/autoload.php.
composer composer dumpautoload -o -> outputs "Generated optimized autoload files containing 0 classes"
In the main file G4S_Ecommerce.php I put the following line:
require __DIR__.'/vendor/autoload.php';
In the same file I put
use G4S_Ecommerce\Includes\Ecommerce;
$starter = new Ecommerce();
but it leads me to a Fatal error: Uncaught Error: Class 'G4S_Ecommerce\Includes\Ecommerce' not found
Why the composer dumpautoload -o returns 0 classes? What am I doing wrong?
Thanks
First (it is not obvious from your files structure) you need to set a namespace for your Ecommerce class (i.e., G4S_Ecommerce/Includes)
Second, based on what you've declared in the autoload directive, composer is expecting to find the G4S_Ecommerce folder under the src folder, and in that folder you need to place your php class file with a name identical to the class name (i.e., Ecommerce).
I'm developing an application where I started using Kohana and now Koseven, and I need to use an api that was available in the composer, I followed the steps to download the files I created a folder to sell inside the application, and put in the bootstrap.php code to call the autoload of the composer.
But after doing this when trying to use a class of this api error of "class not found" occurs.
I do not know what else to do, can you help me?
In order to use composer with Kohana or Koseven, you need to call composer's autoloader from within bootstrap.php.
After Kohana::modules($modules); and before Route::set, insert the following code:
/**
* Autoload composer libraries
*
*/
require APPPATH . 'vendor/autoload.php';
This assumes your composer install command is run the from the root of your app, and it uses the default vendor directory.
Probably you must add this to your composer.json. (Check inc composer doc.) I don't know, because in modules directory I have second instance.
"extra": {
"installer-paths": {
"modules/{$name}/": ["type:kohana-module"]
}
},
And enable module composer as first:
Kohana::modules(array(
'composer' => MODPATH.'composer', //
'auth' => MODPATH.'auth', // Basic authentication
'cache' => MODPATH.'cache', // Caching with multiple backends
It works for me ko3
I'm trying to build a composer package for one of my old libraries. I'm also bit new to GIT. Doing this I'm also learning git workflow. I'm following these articles for building composer package.
1 - http://culttt.com/2014/05/07/create-psr-4-php-package/
2 - https://knpuniversity.com/screencast/question-answer-day/create-composer-package
I've uploaded a test code to Github to know everything working fine. My Github link : https://github.com/mi6crazyheart/youtube-extract
But, It seems like when I'm downloading my package through Composer it's autoloader is not working. Getting following error in my console file -
Uncaught Error: Class 'Youtube\\Extract' not found in /var/www/html/suresh/opensource/test/index.php:4\nStack trace:\n#0 {main}\n thrown in /var/www/html/suresh/opensource/test/index.php on line 4
Code for my index.php file where I'm trying to load this library
<?php
require __DIR__ . '/vendor/autoload.php';
$youtube = new Youtube\Extract();
echo $youtube->greeting();
I'm using the following code in my composer.json file to download code from git repository
{
"require": {
"mi6crazyheart/youtube-extract": "dev-master"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/mi6crazyheart/youtube-extract"
}
]
}
Don't know where I'm doing mistake. Need some guidance.
Your namespace is "Youtube\Extract" and your class is "Extract" which means your code to make a new instance of the class Extract needs to look like the following:
<?php
$youtube = new Youtube\Extract\Extract();
I have a question with regards to refactoring a legacy PHP application to be compatible with PHP's PSR-4 standards. I have some classes located at app/classes/ folder which are not yet namespaced properly and I want them to be autoloaded directly when I call composer's vendor/autoload.php. I added a name space according to \<NamespaceName>(\<SubNamespaceNames>)*\<ClassName> and I have tried creating a vendor/myapp/classes/src/ directory under the vendor folder of composer, and executed a dump-autoload command but to no avail. The class doesn't get loaded up and composer can't figure out where to find it. Any pointers on how to do this?
Thanks,
Jan
Thing/s to Note:
-> I don't want to upload the source code to any repository that can be publicly searchable like Packagist as the code is for internal use only.
EDIT:
Here is my composer.json:
...
"autoload":{
"psr-4": {
"Myapp\\" : "Myapp"
}
},
...
But now the structure looks like:
->Myapp
-->Classes
--->Module1
---->submodule.php
--->Module2
---->submodule2.php
--->Module3
---->submodule3.php
--->Config
---->config.db.php
->vendor
-->autoload.php
Here are my issues/questions:
When I try to load submodule.php, which in turn would load submodule2.php and submodule3.php, it would tell me that submodule3.php is not found. The namespace of submodule3.php is Myapp\Classes\Module3 but it says its not found.
I want to include forcibly, config.db.php, on every call of autoload.php
I now figured it out. But the source files will not reside in the /vendor folder, which is okay for my case. If you want to make your files be autoloaded automatically no matter which folder, just add it to the psr-4 block in the composer.json, or create it if it's not yet there. In my composer.json, I have this block on the top level object, which adds the folder I want to get autoloaded and includes also the specific file I want to include, like a config file of some sorts:
...
"autoload":{
"files": [
"somefolder/somefile.php"
],
"psr-4": {
"MyApp\\" : "MyApp"
}
},
...
Which simply means that composer should autoload files residing in the MyApp directory which will also have a namespace of MyApp.
So my folder structure looks like this now:
->MyApp
-->Classes
--->MyClass1.php --> namespace MyApp\Classes classname: MyClass1
-->Components
--->MyClass2.php --> namespace MyApp\Classes classname: MyClass2
->somefolder
-->somefile.php
->vendor
-->autoload.php
->index.php
So if I want to include the file MyClass1.php in index.php, I will just add it like:
include_once( __DIR__ . "/vendor/autoload.php");
use \MyApp\Classes\MyClass1;
$foo = new MyClass1();
$foo->bar();
Hope it helps.
You could approach this by creating your own composer package, adding it to your private git repository, and adding the package to your project's composer.json file:
"require": {
"{your-vendor}/{package-name}": "dev-master"
},
"repositories": [
{
"type": "vcs",
"url": "git#bitbucket.org:{your-vendor}/{package-name}.git"
}
],
Once this is done, run composer update.
I am developing a package and I want to store it in the vendor directory before I publish it.
So, The path to say Basset's service provider once it is installed via composer is
/siteroot/vendor/jasonlewis/basset/src/Basset/BassetServiceProvider.php
which composer maps in it's autoloader fine, now If mine is at
/siteroot/vendor/hailwood/databaseconfigloader/src/DatabaseConfigLoader/DatabaseConfigLoaderServiceProvider.php
What do I need to add to the composer.json file to make it generate the autoload mapping correctly?
I can manually edit the autoload_namespaces.php file to add the mapping
'Hailwood\\DatabaseConfigLoader' => $vendorDir . '/hailwood/databaseconfigloader/src/'
and that works, but obviously gets overwritten when I do a composer update, I tried adding
"psr-0": {
"Hailwood\\DatabaseConfigLoader": "src/"
}
to the composer.json but that outputs 'Hailwood\\DatabaseConfigLoader' => $baseDir . '/src/', which doesn't work.
In state, you don't respect PSR-0. So you have two solutions :
Respect PSR-0
You must put your code in /siteroot/vendor/hailwood/databaseconfigloader/src/Hailwood/DatabaseConfigLoader/DatabaseConfigLoaderServiceProvider.php. After that, simply do :
"psr-0":
{
"Hailwood\\DatabaseConfigLoader": "vendor/hailwood/databaseconfigloader/src/"
}
Use classmap autoloading
Just try :
"classmap": ["vendor/hailwood/databaseconfigloader/src/"]