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.
Related
I'm on Drupal 9.3.x trying to add a script to run with composer, following this guide
I've tried either to add the autoload and script part to the drupal project composer.json file
{
"name": "drupal/d9-starter-kit",
"description": "Project template for D9 starter kit",
"type": "project",
"license": "GPL-2.0-or-later",
"homepage": "https://www.drupal.org/project/drupal",
"support": {
"docs": "https://www.drupal.org/docs/user_guide/en/index.html",
"chat": "https://www.drupal.org/node/314178"
},
"autoload": {
"classmap": [
"Drupal\\d9_starter_kit\\Starter.php"
]
},
"scripts": {
"doStart": [
"Drupal\\d9_starter_kit\\Starter::doStart"
]
},
// require, extra, etc...
}
or to create a composer.json inside the custom module I've made
{
"name": "drupal/d9_starter_kit",
"type": "drupal-custom-module",
"description": "a module to script the start",
"license": "proprietary",
"authors": [
{
"name": "My name",
"email": "mymail#mail.com"
}
],
"require": {},
"autoload": {
"classmap": [
"src\\Starter.php"
]
},
"scripts": {
"doStart": [
"Drupal\\d9_starter_kit\\Starter::doStart"
]
}
}
and include that to the project composer file, according the the guide on Drupal site
The path of the Start class is web/modules/custom/d9_starter_kit/src/Starter.php
<?php
namespace Drupal\d9_starter_kit;
class Starter {
public static function doStart() {
print('test');
// TODO
}
}
I've tried various other syntax for the autoload part - e.g. d9_starter_kit\\Starter.php - but none works.
I get the following error
Could not scan for classes inside "Drupal\d9_starter_kit\Starter.php" which does not appear to be a file nor a folder
Class Drupal\d9_starter_kit\Starter is not autoloadable, can not call doStart script
What is the correct way to autoload the class and run a script? The objective of the latter is the same of the guide, copy, rename and edit some files in order of automate the "start up" of a custom project template.
I have setup a folder structure like this for a package of legacy classes
vendorname/legacy/src/ClassA.php
namespace Vendorname\Legacy;
class ClassA{}
vendorname/legacy/src/Folder/Class2.php
namespace Vendorname\Legacy\Folder;
class FolderClass2{}
With composer I'm loading this from a github repo like this:
"repositories": [
{
"type": "vcs",
"url": "git#bitbucket.org:username/vendorname-legacy-classes.git"
}
],
"require": {
"vendorname/legacy": "master#dev"
}
When I load ClassA like this it works:
use Vendorname\Legacy\ClassA;
$a = new ClassA();
However none of my subfolder'd classes work:
use Vendorname\Legacy\Folder\FolderClassB;
$b = new FolderClassB();
Class 'Vendorname\\Legacy\\Folder\\FolderClassB' not found
I have already defined the source folder with a file vendor\vendorname\composer.json
{
"name": "vendorname/legacy",
"description": "Vendorname Legacy classes",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-4": {
"Vendorname\\Legacy\\": "src"
}
},
"extra": {
"branch-alias": {
"master": "master"
}
}
}
you need to define one thing more to your composer.json
{
"autoload": {
"psr-4": {"Vendorname\\Legacy\\": "vendorname/legacy/src/"}
}
}
I'm create composer package with type library. And trying to require it to Symfony2 project.
The package has following composer.json
{
"name": "vendor/package-sdk",
"description": "My private package",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {"Vendor\\PackageSDK\\": "src/"}
},
"require": {
"php": ">=5.3.3"
}
}
Then I require it to my SF2 project.
"repositories": [
{
"type": "git",
"url": "git#github.com:me/vendor-package-sdk.git"
},
],
"require": {
...
"vendor/package-sdk": "~0.0.1-alpha1"
...
}
When I calling
use Vendor\PackageSDK\Client;
...
$client = new Client();
```
And I got fatal error:
PHP Fatal error: Class 'Vendor\PackageSDK\Client' not found in /path
If I do
composer dump-autoload -o
It works, but
composer dump-autoload
not.
The file vendor/composer/autoload_psr4.php contain:
'Vendor\\PackageSDK\\' => array($vendorDir . '/vendor/package-sdk/src'),
Could anybody tell me what am I doing wrong?
In composer autoload_classmap.php file I saw the following line
'Vendor\PackageSDK\Client' => $vendorDir . '/vendor/package-sdk/src/Cilent.php',
So it's just a typo in filename of package
Cilent.php should be Client.php
In my project inside the vendor directory I created a new directory named Hello and created a new class HelloWorld.php with the namespace Hello\Test. Then in the root of Hello I created a composer.json with default meta data (author, license, desc, required), and added autoload PSR-4 Hello\\Test\\.
So what do I need to do next to autoload my package. I looked at some Symfony components and their composer.json package and configuration is the same.
Is it possible to autoload my local package from vendor like this?
Dir structure:
|-my_project
|-composer.json
|---vendor
|-------composer
|-------autoload.php
|-------Hello
|-----------composer.json
|-----------HelloWorld.php
./vendor/Hello/composer.json
{
"name": "Zend/Hello",
"description": "My test package",
"license": "MIT",
"authors": [
{
"name": "Zend Zend",
"email": "test#example.com"
}
],
"autoload": {
"psr-4": {
"Hello\\Test\\": ""
}
}
}
My HelloWorld.php class has namespace Hello\Test;
Inside index.php i do include 'vendor/autoload.php
And root composer.json
{
"autoload": {
"psr-4": {
"": "src/",
"Hello\\Test\\": "./vendor/Hello"
}
},
"require": {
"Hello/Test": "*"
}
}
composer update
Okay! Sorry for the late reply.
You only need one composer.json to make this work. The one in your main project directory.
Try using this:
{
"name": "Zend/Hello",
"description": "My test package",
"license": "MIT",
"authors": [
{
"name": "Zend Zend",
"email": "test#example.com"
}
],
"autoload": {
"psr-4": {
"Hello\\Test\\": "vendor/Hello"
}
}
}
And your HelloWorld.php file should be like:
<?php
namespace Hello\Test;
class HelloWorld {
// your methods goes here
}
Run a composer self update to get the latest composer the run composer update and it should now be added to your vendor/composer/autoload_psr4.php file
Hi i have this folder structure:
I'm using composer for autoloading my files but it is dont work .. i do it first time and i dont know how to implement this.
My composer.json
{
"name": "Some name",
"description": "Some Framework",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "Some names of authors",
"email": "some#gmail.com"
}
],
"autoload": {
"psr-4": {
"Apison": "/../sdk/"
}
}
}
And my index.php
<?php
require_once 'vendor/autoload.php';
$app = new \Apison\Sdk\App();
When i update my composer it will write: Nothing to load and PHP will catch exeption on line with $app = new \Apison\Sdk\App();
Thanks for your tips
namespaces need \\:
"Apison\\": "../sdk"
documentation:
https://getcomposer.org/doc/04-schema.md#psr-4
Based on our chat, the solution is this:
"autoload": {
"psr-4": {
"Apison\\Sdk\\": "sdk"
}
}
Then the namespaces and file structure was changed to comply with the psr-4 standard