I'm having trouble understanding with doesn't Composer autoloads the packages I required.
My current composer.json file has the following:
{
"require": {
"atlas/orm": "#dev"
},
"require-dev": {
"atlas/cli": "#dev"
}
}
It was supposed to generate a Namespace in the /vendor/composer/autoload_namespaces.php file. But it doesn't. The file only has the following:
// autoload_namespaces.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Psr\\Log\\' => array($vendorDir . '/psr/log'),
);
Of course, when I try to use the "require DIR . '/vendor/autoload.php';" to autoload the package and then use its classes, it does not work.
Any idea on how can I solve this?
For requiring in all of the installed dependencies, you have to require 'autoload.php'. For autoloading(PSR-4), in the composer.json file, you have to give a name under which everything will be namespaced and the folder name from which files will be autoloaded.
"Namespace_name\\":"folder_name"
Note: The backslash after the namespace_name needs to be escaped, hence the extra backslash.
Then run composer dump-autoload -o
Related
Composer in autoload_static.php use class that I don't need them in every app request.
'd5fa61a7f6cbc1df09dd4df84549a2dc' => __DIR__ . '/..' . '/rospdf/pdf-php/src/Cpdf.php',
'2d15964294879de66053d54f6bde65d7' => __DIR__ . '/..' . '/rospdf/pdf-php/src/Cezpdf.php',
How to remove them from this autoload file? I can delete/comment them manually but every Composer update this file is re-generated.
I try to add in my main composer.json:
"exclude-from-classmap": ["vendor/rospdf/pdf-php/src/"]
& run composer dump-autoload bo those class are still in there.
You can trick the autoloader of composer and let him think those are already loaded:
<?php
// Setting global variable:
$GLOBALS["__composer_autoload_files"] = [
"d5fa61a7f6cbc1df09dd4df84549a2dc" => true,
"2d15964294879de66053d54f6bde65d7" => true,
];
require "vendor/autoload.php";
But this needs to happen before the vendor/autoload.php is included.
After initializing the Composer autoloader, I want to check the existence of a class:
require_once('vendor/autoload.php');
var_dump(class_exists('PagesController'));
This gives me boolean false, as if my class doesn't exist. However, it does, and is mentioned in the classmap autoloading.
When I add a simple change in the vendor/autoload.php, like adding a var_dump("ballon"), the original check for the PagesController changes to boolean true.
My composer.json file looks like this:
{
"require": {
"propel/propel": "~2.0#dev"
},
"autoload": {
"classmap": ["controllers/", "views/", "views/helpers/", "controllers/components/", "models/", "generated-reversed-database/generated-classes/"]
}
}
I've ran php composer.phar install to generate the autoloader.
require_once('vendor/autoload.php') includes the composer-generated file and is the very first line of code to be executed (it's in my index.php). The PagesController class is located in controllers/.
I'm running php 5.5.9 with Apache2 on an Ubuntu server.
It doesn't even matter whether its a var_dump, a comment (//), or simply a whitespace () that I add in vendor/autoload.php, as long as I add something.
My autoload_classmap.php file:
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
(...lots of classes...),
'PagesController' => $baseDir . '/controllers/PagesController.php'
);
So the class is found and listed.
I've added composer to an existing project that uses the PHP autoload function. Now that composer autoload.php is being used I've removed my old autoload function and I'm trying to load my existing source directory via composer autoload but it isn't picking up any of my existing source classes.
Everything installed by composer loads fine and can be accessed via namespaces etc. so it's just the existing sources in the source directory not being picked up. Any suggestions?
I've looked at a few other of the composer questions on stackoverflow but nothing I've read has solved my problem.
File structure:
index.php
root/
sources/
vendor/
composer.json
media/
Composer autoload:
"autoload": {
"psr-0": {
"" : "sources/"
}
}
There were two things causing issues for me, one was the class file names and the second was a composer command that needed to be run.
My class file names were in the format {classname}.class.php when they need to be in the format that PSR-0 expects which is Classname.php (uppercase first letter) and in turn the classname in the class file follows the file name.
class Classname
{
...
The second issue was that I needed to run the below command.
composer dump-autoload
From composer.org:
If you need to update the autoloader because of new classes in a classmap package for example, you can use "dump-autoload" to do that without having to go through an install or update.
If your code structure is too complex to convert to PSR-* structure, you can use your old autoloader and composer autoload together.
spl_autoload_register( function ( $class ) {
$file = "sources/" . $class . ".class.php";
if ( file_exists( $file ) ) {
require $file;
return;
}
} );
require "vendor/autoload.php";
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/"]
So - I have a simple PCR0 auto-loader in my bootstrap.php, that should load any PCR0 compatible library class from vendors directory...
spl_autoload_register( function( $classname ) {
$path = preg_match( '/\\\\/', $classname )
? str_replace( '\\', DIRECTORY_SEPARATOR, $classname )
: str_replace( '_', DIRECTORY_SEPARATOR, $classname );
$file = VENDORS_PATH . DIRECTORY_SEPARATOR . $path . '.php';
if ( file_exists( $file ) ) {
require_once( $file );
}
});
I'm not sure if I understand why composer generates auto-loading files in vendors directory (namely composer directory and autoload.php file) ?
Can I stop Composer from generating those auto-loader files? or am I missing something? I don't think I need them?
There are three autoload related files, each having a different purpose.
vendor/autoload.php initializes the autoloaders of composer. Composer offers a autoloaders to enable composer compatible libraries to be load.
vendor/composer/autoload_classmap.php this file is used by the classmap autoloader, this is for either libraries that are not even PSR-0 compatible, or production environments (classmap is faster than a lookup through the file system).
vendor/composer/autoload_namespaces.php this is the configuration for the PSR-0 autoloading that composer comes with
Now you mentioned that you have your own PSR-0 classloader, which you are not supposed to use for composer dependencies - you are simply supposed to require/include the vendor/autoload.php and have composer take care of the rest.
This is why there is no option to disable the generation of the autoloading files. In the end composer is supposed to enable you to use the library installed, and enables you by providing all loading you need.
Unfortunately, It doesn't sound like Composer is going to support this feature: https://github.com/composer/composer/issues/1663
In my CMS EFFCORE I used the following solution...
For UNIX shell:
File composer.json
"scripts": {
"post-install-cmd": [
"rm vendors/autoload.php",
"rm -rf vendors/composer"
],
"post-update-cmd": [
"rm vendors/autoload.php",
"rm -rf vendors/composer"
]
}
For Win/Nix:
File composer.json
"scripts": {
"post-install-cmd": [
"php vendors/post-install-cmd.php"
],
"post-update-cmd": [
"php vendors/post-install-cmd.php"
]
}
File vendors/post-install-cmd.php
# rm -rf vendors/packages/composer
if (file_exists('vendors/packages/composer/')) {
$composer_items = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator('vendors/packages/composer/', \FilesystemIterator::UNIX_PATHS|\FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($composer_items as $c_path => $c_spl_file_info) {
if ($c_spl_file_info->isFile()) {if (#unlink($c_path)) print "File '". $c_path. "' was removed.\n";}
if ($c_spl_file_info->isDir ()) {if (#rmdir ($c_path)) print "Directory '". $c_path. "/' was removed.\n";} }
if (#rmdir('vendors/packages/composer/')) {
print "Directory 'vendors/packages/composer/' was removed.\n";
}
}
# rm vendors/packages/autoload.php
if (file_exists('vendors/packages/autoload.php') &&
#unlink('vendors/packages/autoload.php')) {
print "File 'vendors/packages/autoload.php' was removed.\n";
}
There is an option --no-autoloader on install and update commands.
It's implemented in https://github.com/composer/composer/pull/3453 since the December of 2014.
Personally I added those files to .gitignore since the project I am working on has an autoloader that works fine