PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 31, 2022

[FIXED] Composer autoloading psr-4 not adding namespace to autoload_psr4.php

 January 31, 2022     autoload, composer-php, php, psr-4     No comments   

Issue

I am using in a project a custom repository in github that contains my set of instruments.

The project correctly imports the package inside the /vendor directory,but i cannot use any of the class, because it is not autoloading the contents of the package.

My package composer.json file looks as follows:

{
"name": "mynamespace/toolbox",
"description": "Asdfoobar.",
"keywords": ["mynamespace", "toolbox"],
"license": "MIT",
"require": {
    "php": ">=5.4"          
},
"require-dev": {
    "codeception/codeception": "2.0.2",
    "codeception/specify": "0.3.6",
    "codeception/verify": "0.2.7",
    "mockery/mockery": "0.9.1"
},
"autoload": {
    "psr-4": {
        "Mynamespace\\": "src/Mynamespace",
        "Mocks\\": "tests/Mocks"
    }
},
"scripts": {
    "post-install-cmd": [

    ],
    "post-update-cmd": [
        "php vendor/bin/codecept run"
    ]
},
"config": {
    "preferred-install": "dist"
},
"minimum-stability": "stable"

}

Inside the project instead, the composer.json file looks as follows:

    "require": {
       [...stuff...]
       "Mygitrepo/toolbox": "dev-master" 
    },
    "repositories": [        
    {
        "type": "package",
        "package": {
            "name": "Mygitrepo/toolbox",
            "version": "master",
            "source": {
                "url": "https://github.com/Mygitrepo/toolbox.git",
                "type": "git",
                "reference": "master"
            }
        }
    }
]

The final autoload_psr4.php after doing composer update, composer dump looks like following: No trace of "Mynamespace"

return array(
  'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'),
  'Ivory\\LuceneSearchBundle\\' => array($vendorDir . '/egeloen/lucene-search-bundle'),
  'Facebook\\' => array($vendorDir . '/facebook/php-sdk-v4/src/Facebook'),
);

Any help ?


Solution

This answer applies here as well.

Because you are providing a "type:package" entry in "repositories", you are disabling the composer.json file in the package.

And there is no autoloading defined in the package definition, so the package is not autoloaded at all.

Solution: Don't use the "type:package" entry for repositories you own and which have a composer.json file inside.



Answered By - Sven
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing