Asked  1 Year ago    Answers:  5   Viewed   6 times

I have trouble getting a project correctly installed through composer. I have a own custom package (library) hosted in a non public git repo (but centralized) which is fetched by composer (dummy project containing a composer.json just for testing my package).

So the structure is like that:


/test/project/composer.json
              index.php

Content of composer.json:

{
    "name": "vendor/test",
    "description": "Test-description",
    "authors": [{
        "name": "Benjamin Carl",
        "email": "email@testdomain.com",
        "homepage": "http://www.testdomain.com",
        "role": "Developer"
    }],
    "keywords": [
        "foo",
        "bar"
    ],
    "homepage" : "http://www.testdomain.com/",
    "license" : [
        "The BSD License"
    ],
    "repositories": [{
        "type": "package",
        "package": {
            "name": "foo/bar",
            "version": "0.0.1",
            "source": {
            "url": "git@git.testdomain.local:benjamin.carl/bar.git",
                "type": "git",
            "reference": "master"
    }
        }
    }],
    "require": {
        "foo/bar": "0.0.1"   
    }
}

So when i run the composer install "php composer.phar install" within the folder containing the test-project and the composer.json you see above - everything seems to be fine - but - the autoloader information is missing - the map (array) in "autoload_namespaces.php" (files getting generated) keeps empty.

I assumed that when i install a package with composer and the package (in my case the package foo/bar) contains a composer.json file - this file is also executed/processed during installation and the information for autoloading is taken from this (package) composer.json file. Am i right? Or am i doing something wrong?


Here is the content of the "composer.json" file from package foo/bar:

{
    "name": "foo/bar",
    "description": "foo - the project for all bars out there.",
    "authors": [{
        "name": "Benjamin Carl",
        "email": "email@testdomain.com",
        "homepage": "http://www.testdomain.com",
        "role": "Developer"
    }],
    "keywords": [
    "php",
        "foo",
    "bar",
        "baz"
    ],
    "homepage": "http://testdomain.com/",
    "license": [
        "The BSD License"
    ],
    "require": {
        "php": ">=5.3.0"
    },
    "autoload": {
        "psr-0": {
        "Foo": "Framework/"
    }
    },
    "include-path": ["Framework/"],
    "support": {
        "email": "email@testdomain.com",
        "issues": "https://testdomain.com/issues",
    "wiki": "https://testdomain.com/wiki"
    },
    "repositories": [{
        "type": "vcs",
    "url": "https://git.testdomain.local/test/bar.git"
    }]
}

As the result of the configuration(s) above i want the autoloading information like this:

$data = array(
    'Foo' => $vendorDir . '/foo/bar/Framework'
);

If i insert this line "'Foo' => ..." manually everything works fine. But i can't figure out why this information isn't written by composer to the "autoload_namespaces.php" file.

Thanks for any help :)

 Answers

3

If you try to install a private library/package via the "repositories" node of composer.json you have to re-specify the "autoload" node as well apparently.

{
  "repositories": [{
    "type": "package",
    "package": {
      "name": "foo/bar",
      "version": "0.0.1",
      "source": {
        "url": "git@git.testdomain.local:benjamin.carl/bar.git",
        "type": "git",
        "reference": "master"
      },
      "autoload": {
        "psr-0": {
        "Foo": "Framework/"
      }
    }
  }]
}

I just spent a couple of hours figuring this out. Good job, Composer!

Saturday, May 29, 2021
 
4

You can just require the composer autoloader. The only feature it lacks is the ApcClassLoader which speeds things up with APC but introduces some complexity (you have to clear the cache when deploying). Using composer's -o flag (when installing or running dump-autoload) will give you a classmap one which is more or less equivalent to APC in terms of speed but without the complexity.

Thursday, April 1, 2021
 
Teno
 
3

As stated in the question you already have both the versions of PHP on your system, As Laravel uses the cli version you need to enable 7.X and disable 5.X.

You can achieve that by below commands

$ sudo a2dismod php5.6 // disable the loaded version
$ sudo a2enmod php7.0 // enable the desired version
$ sudo service apache2 restart // restart apache to get it in action

For More information You can install different version of PHP using the below commands

For Apache

$ sudo apt install php5.6   [PHP 5.6]
$ sudo apt install php7.0   [PHP 7.0]
$ sudo apt install php7.1   [PHP 7.1]

For Ngix

$ sudo apt install php5.6-fpm   [PHP 5.6]
$ sudo apt install php7.0-fpm   [PHP 7.0]
$ sudo apt install php7.1-fpm   [PHP 7.1]

To install any PHP modules, simply specify the PHP version and use the auto-completion functionality to view all modules as follows.

------------ press Tab key for auto-completion ------------ 
$ sudo apt install php5.6 
$ sudo apt install php7.0 
$ sudo apt install php7.1 

Now you can install most required PHP modules as per your requirements.

------------ Install PHP Modules ------------
$ sudo apt install php5.6-cli php5.6-xml php5.6-mysql 
$ sudo apt install php7.0-cli php7.0-xml php7.0-mysql 
$ sudo apt install php7.1-cli php7.1-xml php7.1-mysql 
Thursday, April 1, 2021
 
4

You can autoload specific files by editing your composer.json file like this:

"autoload": {
    "files": ["src/helpers.php"]
}

(thanks Kint)

Saturday, July 3, 2021
 
Arman
 
1

There are two ways to fix it.

  1. change composer.json to

    "Portal\Core\": "src/core/"
    
  2. Or rename the core directory to Core

https://getcomposer.org/doc/04-schema.md#psr-4

The subdirectory name MUST match the case of the sub-namespace names.

http://www.php-fig.org/psr/psr-4/

Wednesday, August 11, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :