Composer autoload-dev does not work - php

The file autoload_psr4.php not contains the namespace from "autoload-dev" section only from "autoload" section.
When my composer.json reads
"autoload": {
"psr-4": {
"Namespace\\": "src/"
}
},
"autoload-dev": {
"prs-4": {
"Namespace\\Tests\\": "tests/"
}
}
And I run
composer require vendor/namespace 1.0-dev
My /vendor/composer/autoload_prs4.php file appears as
// autoload_psr4.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Namespace\\' => array($baseDir . '/src'),
);
Thanks!

My guess is that you're showing the composer.json of vendor/namespace package. If that's the case:
Take a look at the docs. It says: "autoload-dev (root only)". root only means it only applies to the root package. As you included the package, the shown composer.json file is not the root package and the autoload-dev section is thus ignored.

Since this is the first Search Engine result when searching for "autoload-dev not working": In composer.json, if "autoload-dev" was added after defining and using "autoload", run $ composer dump-autoload.

In name error, psr, prs, fffff
"autoload": {
"psr-4": {
"Namespace\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Namespace\\Tests\\": "tests/"
}
}

Related

Composer autoload can't find class

I'm trying to create a MVC structure and use composer to autoload everything.
But I keep getting this error:
Fatal error: Uncaught Error: Class 'App\Init' not found in C:\wamp64\www\activity\Public\index.php on line 5
|MainFolder
|App
|Public
|Vendor
|ACT
|composer
|autoload.php
|composer.json
composer.json:
{
"name": "vendor/activity",
"description": "descrip",
"require": {
"php": ">=5.6.25"
},
"authors":[
{
"name": "John Doe",
"email": "johndoe#gmail.com"
}
],
"autoload":{
"psr-4": {
"ACT\\": "vendor/",
"App\\": "/"
}
},
"config":{
"bin-dir": "bin"
}
}
App\init.php
<?php
namespace App;
class Init
{
public function __construct()
{
echo "Loaded!!";
}
}
Public\index.php
<?php
require_once '../vendor/autoload.php';
$init = new \App\Init;
\Vendor\composer\autoload_namespaces.php
<?php
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
"ACT" => array($vendorDir . false),
"App" => array($baseDir . '/'),
);
Obs: Already did composer dump-autoload
Don't manually put things in /vendor.
While adhering to #1, don't reference /vendor in autoload, the packages should all have their own fully-functionaly autoloaders that composer will find and use.
You need to specify more of the path in your autoload.
"autoload":{
"psr-4": {
"App\\": "App/"
}
},
Think of it like telling composer "look for things starting with the namespace foo\bar\ in the following folder".
Note: The folder name doesn't have to match the namespace.
Eg: Following the suggested Vendor\Package\ scheme for PSR/Composer
{
"autoload": {
"psr-4": {
"sammitch\\meatstacker\\": "src/"
}
}
}
And then:
\sammitch\meatstacker\Client maps to src/Client.php
\sammitch\meatstacker\Bread\Rye maps to src/Bread/Rye.php
and so on

PHPUnit autoloader classes with composer

My project's structure is:
--/
--src
--tests
--phpunit.xml
--composer.json
I want to use composer for autoloading my classes from src folder in tests.
My composer.json:
{
"name": "codewars/pack",
"description": "Codewars project",
"type": "project",
"require": {
"fxp/composer-asset-plugin": "^1.2.0",
"phpunit/phpunit": "5.5.*",
"phpunit/dbunit": "2.0.*"
},
"autoload": {
"psr-4": {"Source\\": "src/"
}
}
}
Autoloder files generated:
<?php
// autoload_psr4.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Source\\' => array($baseDir . '/src'),
);
My phpunit.xml:
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
And my test file example:
class Task2Test extends PHPUnit_Framework_TestCase
{
public function testTask2(){
$list=[1,3,5,9,11];
$this->assertEquals(7,\Source\findMissing($list));
$list=[1,5,7];
$this->assertEquals(3,\Source\findMissing($list));
}
}
And when I run tests I get error such as Fatal error: Call to undefined function Source\findMissing()
Please, help me, how can I solve this problem?
PSR-4 designed for autoloading classes, not for function. Because php cant find files with your functions.
May be better way write code with classes and static methods? Than you can use PSR-4 autoloading. Another way is specify "files" section into autoload section
{
"name": "codewars/pack",
"description": "Codewars project",
"type": "project",
"require": {
"fxp/composer-asset-plugin": "^1.2.0",
"phpunit/phpunit": "5.5.*",
"phpunit/dbunit": "2.0.*"
},
"autoload": {
"files": [
"src/my_cool_function1.php",
"src/my_cool_function2.php"
],
"psr-4": {
"Source\\": "src/"
}
}
For more information see this question

how to use composer autoloader with __autoload?

my composer.json content is:
{
"require": {
"rlanvin/php-rrule": "1.*"
}
}
my own autolader is:
function __autoload($class_name) {
include __DIR__.'/classes/'.$class_name . '.php';
}
Problem is: when I want to add tha composer autoloader with
require_once 'vendor/autoload.php';
My own autoloader does not work any more. (Class not found !)
How to make noth work (the composer autolader and mine) ?
regards
I tried:
<?php
require 'vendor/autoload.php';
User::get(1);
User.php is in /classes
composer.json
{
"require": {
"rlanvin/php-rrule": "1.*"
},
"autoload": {
"psr-0": {
"": "classes"
}
}
}
and 'got Class User not found'
It works find with my autloader:
function autoload($class_name) {
include __DIR.'/classes/'.$class_name . '.php';
}
You don't need your autoloader. Composer has plenty of options: PSR-0, PSR-4, classmap and files. Try classmap.
{
"require": {
"rlanvin/php-rrule": "1.*"
},
"autoload": {
"classmap": [
"classes"
]
}
}
UPDATED: Or even better use psr-0 in the following way
{
"require": {
"rlanvin/php-rrule": "1.*"
},
"autoload": {
"psr-0": {
"": "classes"
}
}
}

Composer autoloading with PSR4

Probably something trivial but I have a problem with basic autoloading. I wanna create sandbox project just for testing new solutions so I've created following structure:
Sandbox
|- index.php
|- composer.json
|- vendor
| |- {autogenerated content}
|- src
|- Working.php
File composer.json looks like this:
{
"name": "vendor/sandbox",
"authors": [
{
"name": "foo",
"email": "bar#example.com"
}
],
"require": {
"phpunit/phpunit": "dev-master",
"phpunit/phpunit-mock-objects": "dev-master"
},
"psr-4": {
"Sandbox\\": "src/"
}
}
Of course I've run composer.update after changes. Then I wrote a trivial body of Working.php:
<?php
namespace Sandbox;
class Working
{
public function __construct() {
echo "Hello World";
}
}
And of course index.php as well:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Sandbox\Working;
new Working();
?>
I checked permissions to those files just to be sure but when I try to run I get
PHP Fatal error: Class 'Sandbox\Working' not found in /var/www/Sandbox/index.php on line 6
I realize it's probably something trivial but what can be wrong here?
At your composer.json you are missing autoload key.
It should be like
"autoload": {
"psr-4": {
"Sandbox\\": "src/"
}
}
I believe
"psr-4": {
"Sandbox\\": "src/"
}
Should be:
"autoload": {
"psr-4": {
"Sandbox\\": "src/"
}
So your file would be:
{
"name": "vendor/sandbox",
"authors": [
{
"name": "foo",
"email": "bar#example.com"
}
],
"require": {
"phpunit/phpunit": "dev-master",
"phpunit/phpunit-mock-objects": "dev-master"
},
"autoload": {
"psr-4": {
"Sandbox\\": "src/"
}
}

Composer doesnt update autoload namespace

Composer not updating autoload_namespaces.php file, despite downloading package normaly. Can't understand where i did a mistake.
If i load something from packagist, namespaces file updating successfully.
Project structure
Main Composer.json
{
"repositories":[
{
"type": "package",
"package": {
"name": "test/framework",
"version": "1.0.0.1",
"dist": {
"url": "http://localhost/repo/1.zip",
"type": "zip"
}
}
}
],
"require": {
"test/framework": "*"
}
}
Package composer.json
{
"name": "test/framework",
"type": "library",
"require": {
"php": ">=5.2.4"
},
"autoload": {
"psr-0" : {
"Test" : "lib/"
}
}
}
autoload_namespaces.php
<?php
// autoload_namespaces.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
);
You are downloading the package defined in the repositories via zip.
https://getcomposer.org/doc/05-repositories.md#package-2
In this case I feel you should define the package definition at the same place. See the above link for the example which contains autoload definition defined.
{
"repositories":[
{
"type": "package",
"package": {
"name": "test/framework",
"version": "1.0.0.1",
"dist": {
"url": "http://localhost/repo/1.zip",
"type": "zip"
},
"autoload": {
"psr-4" : {
"Test\\": "lib"
}
}
}
}
],
"require": {
"test/framework": "*"
}
}
You can also try some variation see my post over http://harikt.com/blog/2014/05/29/hidden-gems-of-composer/
PS : psr-4 is the recommended way for it can autoload psr-0 structured classes. See https://getcomposer.org/doc/04-schema.md#autoload
Just to add to what Hari K T said, be sure to remove the vendor directory after making an update to the composer.json file as composer uses the installed.json file in the ./vendor/composer directory to generate the autoload php files.
I had setup the composer.json correctly, but did not remove the existing vendor directory, so I assumed the answer provided by Hari K T didn't work.

Categories