Viewed   101 times

I use psr-4 autoloader from composer:

"autoload": {
    "psr-4": {
        "DG\Munchkin\": "src/DG/Munch/"
    }
}

This loads classes from /var/www/html/xxx/vendor/yyy/src/DG/Munch

But how can I load classes from /var/www/html/xxx/?

I wrote my own autoloader, but when I require vendor/autoload.php (composer autoload) and my autoloader, it won't work until I create instance of a class in my own autoloader.

 Answers

1

The src directory would be in your project root. Its on the same level as vendor directory is.

If you define

"autoload": {
    "psr-4": {
        "DG\Munchkin\": "src/DG/Munch/"
    }
}

this will not load classes from /var/www/html/xxx/vendor/yyy/src/DG/Munch, like you stated.

Because your project structure is:

/var/www/html/
 +- /xxx (project)
     - composer.json
     +- /src
        +- DG
           +- Munch
     +- /vendor
        - autoload.php
        +- vendor-projectA
        +- vendor-projectB
        +- yyy

The DGMunchkin namespace would map to classes inside

/var/www/html/xxx/src/DG/Munch and not inside

/var/www/html/xxx/vendor/yyy/src/DG/Munch.

But how can I load classes from /var/www/html/xxx/?

Define the paths in the composer.json (inside /var/www/html/xxx/) of your project:

"autoload": {
    "psr-4": {
        "ProjectRoot\" : "", 
        "NamspaceInSourceDir\" : "src/"         
    }
 }

or load the composer autoloader in your index.php or during it's bootstrap and add the paths manually:

$loader = require 'vendor/autoload.php';
$loader->add('Namespace\Somewhere\Else\', __DIR__);
$loader->add('Namespace\Somewhere\Else2\', '/var/www/html/xxx');

Referencing: https://getcomposer.org/doc/04-schema.md#autoload

Saturday, September 17, 2022
5

Why use a PSR-0 or PSR-4 autoload in composer if classmap is actually faster?

Because it's more practical.

In production, you can use a classmap (with composer dumpautoload -o) because you won't add any new class, but in dev environment it's interesting to have the flexibility provided by PSR-0 or PSR-4 (i.e. nothing to do when adding new classes).

Update: you can also use composer install -o, it's simpler.

Monday, December 12, 2022
3

Yes, it is possible, require 'vendor/autoload.php' actually returns an autoloader instance:

/* @var $loader ComposerAutoloadClassLoader */
$loader = require 'vendor/autoload.php';

$class = MonologLogger::class;

$loggerPath = $loader->findFile($class);
if (false === $loggerPath) {
    throw new RuntimeException("Cannot find file for class '$class'");
}
$realLoggerPath = realpath($loggerPath);
if (false === $realLoggerPath) {
    throw new RuntimeException("File '$loggerPath' found for class '$class' does not exists");
}

var_dump($realLoggerPath);

Outputs:

string(64) "/home/user/src/app/vendor/monolog/monolog/src/Monolog/Logger.php"
Tuesday, November 1, 2022
 
karacas
 
3

There is only one way to grab the head of the repository:

"require": { "behat/mink-selenium2-driver" : "dev-master" }
"minimum-stability": "dev"

Oh well, at least two ways:

"require": { "behat/mink-selenium2-driver" : "dev-master as 1.1.x-dev" }
"minimum-stability": "dev"

Probably at least three ways:

"require": { "behat/mink-selenium2-driver" : "dev-master#2e73d8134ec8526b6e742f05c146fec2d5e1b8d6" }
"minimum-stability": "dev"

Because that repository actually aliased the master branch as 1.1.x-dev, this would also work without the minimum-stability affecting all other packages:

"require": { "behat/mink-selenium2-driver" : "1.1.*@dev" }
Tuesday, November 15, 2022
 
3

You should rather take out the implementation details from the war and put in a separate module.

.
├── pom.xml
├── service-api
|   ├── pom.xml
|   └── src
|       └── main
|           └── java
|               └── com
|                   └── 
|                       └── SomeDao.java
├── service-impl
|   ├── pom.xml
|   └── src
|       └── main
|           └── java
|               └── com
|                   └── 
|                       └── SomeDaoImpl.java
└── service-web
    ├── pom.xml
    └── src
        └── main
            └── webapp
                └── WEB-INF
                    └── web.xml

Let service-impl depend on service-api. And service-web will depend on the service-impl.

Thursday, September 8, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share