I've been working on setting up a CodeIgniter project with composer. I'm wanting to include php classes stored in files outside the vendor folder - in a shared folder.
My directory structure:
/
--application/
--shared/
-application/
-class1.php
-class2.php
-class3.php
-base/
-classb1.php
--vendor/
--composer.json
--composer.lock
Looking at the composer documentation, I see there is an autoload property in the root package that I'm trying to use to load the classes in the shared directory. These classes aren't namespaced.
My composer.json file is as follows:
{
"description" : "The CodeIgniter Application with Composer",
"require": {
"php": ">=5.3.2",
"codeigniter/framework": "3.1.*"
},
"require-dev": {
"mikey179/vfsStream": "1.1.*"
},
"autoload":{
"psr-0":{
"":"shared/application/",
"":"shared/base/",
"":"shared/data/"
}
}
}
My search led me to this question, but the classes are still not being loaded. I've ran composer update on the terminal.
Well after looking further, there's a property called classmap (documentation) in the root package.
"autoload":{
"classmap":["shared/application/","shared/base/", "shared/data/"]
}
This loads all the required files in the folders.
Related
I have problem that occurs on a rare occasions. On some PCs, and WordPress instances class loading using composer works, but on a rare occasions I can't load any classes, and I'm getting error Class not found. I noticed that when it works, it's usually Mac or a Windows machine, run from either Docker container, Local WP, or XAMPP.
composer.json looks like this:
{
"name": "vendor/my-plugin",
"license": "proprietary",
"description": "Integration of the ...",
"autoload": {
"psr-4": {
"Vendor\\MyPlugin\\": "src"
}
},
"require": {
"jumbojett/openid-connect-php": "^0.9.5"
}
}
Folder structure is similar to this one:
myplugin.php
composer.json
composer.lock
vendor
- ...
src
- controller
- Settings.php
- enumerators
- Message.php
- Uri.php
- helper
- Template.php
- view
...
Running composer install goes without any errors.
Part of myplugin.php file where I'm registering namespace and loading class looks like this:
<?php
namespace Vendor\MyPlugin;
require_once __DIR__ . '/vendor/autoload.php';
use Vendor\MyPlugin\Helper\Template;
use Vendor\MyPlugin\Controller\Settings;
use Vendor\MyPlugin\Enumerators\Uri as UriEnumerator;
I use these classes later on in the code, but as I said, it doesn't recognize classes on some instances of the Wordpress, but it works on ~70% of computers / Wordpress instances.
I am thankful for any suggestion!
After some research, I discovered solution. Adding:
"config": {
"optimize-autoloader": true
}
to composer.json solved the issue.
I have created a custom comoposer package and I want to use it on my project with this composer.json:
{
"name": "papillon/test",
"type": "library",
"version": "dev-master",
"require": {
"php": "^7.1.11"
},
"autoload": {
"psr-4": {
"Papillon\\Fountaine\\Eau\\": "src/Papillon/Fountaine/Eau/"
}
}
}
I compress it in zip. In the main project, I add a folder called repo, where I add de composer package zip. Then, I modify the composer.json of the main project like this:
{
"repositories": [
{
"type": "artifact",
"url": "var/main/repo"
}
],
"require": {
"papillon/test": "dev-master"
}
}
I execute composer update and the pakage is added to vendor folder; all seems to be going well... but if I want to test the package from the main project with this script:
<?php
require (__DIR__ . '/vendor/autoload.php');
use Papillon\Fountaine\Eau\FlowerClass;
echo FlowerClass::bloom();
It returns: PHP Fatal error: Uncaught Error: Class 'Papillon\Fountaine\Eau\FlowerClass' not found in .../test_package.php:6
Stack trace:
#0 {main}
thrown in .../test_package.php on line 6
I think that the package may not be recognized by the main project; maybe the package was improperly installed in the main project?
Debugging autoload can be very useful to catch errors. Take care with the route paths, the autoload tryed to find the classes files in a path with a lowercase folder when in the package composer.json the route was definded with that folder uppercase.
EDIT | I basically would like to tell composer to populate
autoload_classmap.php with a mapping of all files/classes under the
directory "web/". This houses all the application specific classes -
some which follow PSR-0 others do not.
I have a composer.json:
{
"name": "company/project",
"description": "Internal management system",
"require": {
"swiftmailer/swiftmailer": "^5.4",
"slim/slim": "2.4.2"
},
"autoload": {
"psr-0": {
"Application_Ancillary_": "web/private/module/rpi/ancillary/"
}
}
}
This "autoload" will kind of work - but there are cases where the classnames do no map according to PSR-0 the classname might be something like
Application_TestSomething => web/private/module/test/ApplicationTestSomething
When I tried to use 1:1 classname => file mapping it didn't work when I ran:
composer dumpautoload -o
Also the "Slim" classes are being included in the autoload_classmap.php
Ideally all the composer included packages (ie: Slim, etc) would not be part of the classmap file as I have hundreds of legacy files which I would rather include.
I assume that up until now someone manually edited the autoload_classmap.php
Any suggestions?
In case you don't follow any standard (PSR-0/PSR-4), use classmap:
"autoload": {
"classmap": [ "web" ]
}
I downloaded the Text-Statistics ZIP master from Github and extracted and renamed the folder to stats on /Library/WebServer/Documents/stats. Then I used cd /Library/WebServer/Documents/stats and composer update and all dependencies built on /Library/WebServer/Documents/stats as well. When I go to http://localhost/stats/ in my browser, it shows as follows:
This is my first time to install a project on my localhost directory which uses composer. Shall I create an index.php to see how the codes work? Below you can also see the composer.json file:
{
"name": "textstatistics",
"description": "PHP package to measure the readability of text according to various algorithms.",
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "4.2.*"
},
"autoload": {
"psr-0" : {
"DaveChild\\TextStatistics" : "src"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"suggest": {
"ext-bcmath": "More accurate floating point calculations.",
"ext-mbstring": "Handle multi-byte text properly."
}
}
The package you 'installed' is a toolbox and meant to be used inside your project. It has no index.php since that would be useless, what would it have to show?
You should add it to an existing project by running composer require davechild/textstatistics from your project's root directory.
Composer exists so you do not have to download the package, and it's dependencies, yourself (like you did with the zip). It will also generate your autoload file so you do not have to bother about requiring or loading those classes. And it is especially useful when you want to update your dependencies (just run composer update)
This is completely unrelated to github or composer. It looks like this project is not a web application, because I don't see a bootstrap file (something like index.php). This means there is nothing which makes sense to call from a webserver context.
I took a short look at the projects github page. PHP Text Statistics is a library that will be used in other projects, but it's not a standalone tool, or application.
My profesionnal network block internet access. Some month ago I download the Silex framework from an archive (which contains composer.json file) and the composer.phar one's, then I transfer them on my desktop throught HDD.
My composer.json that I customized:
{
"name": "user/silex",
"require": {
"silex/silex": "1.2"
, "twig/twig": ">=1.8,<2.0-dev"
, "doctrine/dbal": "2.2.*"
, "symfony/security": "~2.3"
, "symfony/security": "~2.3"
},
"autoload": {
"psr-4": {
"Portal\\": "src/"
}
}
}
It works fine, my autoload customization too.
Today I want to add the monolog/monolog package, so I manually import it from an other computer.
I place it into my vendor folder, I add the following line to my composer.json file:
, "monolog/monolog": ">=1.0.0"
I run on the console:
php composer.phar dumpautoload
It outputs:
Generating autoload files
Then it stop without error, but the monolog namespace doesn't appear into my /vendor/composer/autoload_*.php files.
What did I miss?
Thanks to edmondscommerce's comment I found the solution:
I update my main composer.json file with an artifact respository (and I disable the packagist one):
{
"name": "user/silex",
"repositories": [
{
"type": "artifact",
"url": "artifact/"
}, {
"packagist": false
}
], "require": {
"silex/silex": "1.2"
, "twig/twig": ">=1.8,<2.0-dev"
, "monolog/monolog": "1.*"
, "doctrine/dbal": "2.2.*"
, "symfony/security": "~2.3"
},
"autoload": {
"psr-4": {
"Portal\\": "src/"
}
}
}
Then I put a folder called artifact according to the url put in the composer.json file.
I create into this folder a zip called monolog-monolog-1.8.zip with the library I want to add.
Then just launch a composer update command!
Be carefull, zip's root must contain a composer.json file, and this composer.json file must contain a version!
If you do not want to create a custom repository, you can also run composer install (or composer update) on a copy that is on a network-connected computer. Then you can copy over the newly added and extracted component into the vendor folder on the machine without internet access. Note that you also need to copy vendor/composer/installed.json to let composer know that the new package has been installed. Once you have copied all these files, you can run composer install on the machine without internet access and it will not try to install anything and dump autoload files.