Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

composer autoload file after classmaps are loaded

I have

"autoload": {
      "psr-4": {
          "ACME": "src/",
      },
      "classmap": ["src/"],
      "files": ["mapper.php"],
      "exclude-from-classmap": ["mapper.php"]
  },

in mapper.php I'm trying to give different namespaces for some legacy stuff.

<?php
class_alias(Some_Class::class, 'Cool\NameSpaced\Class');

I think this fails to build because mapper.php is using classes in src/ and they have not been loaded by composer yet. Is there a way to do this?

Command that I run is composer install --optimize-autoloader --no-dev

like image 778
Thellimist Avatar asked Nov 07 '25 10:11

Thellimist


1 Answers

I've tried you example and it works well.

It might be related to composer command you use. Try this one instead:

composer dump-autoload

This will refresh anything from autoload section.


Here is the setup file by file:

composer.json

{
    "autoload": {
        "classmap": ["src/"],
        "files": ["mapper.php"]
    }
}


mapper.php

<?php

class_alias(Some_Class::class, 'Cool\NameSpaced\Class');


index.php

<?php 

require __DIR__ . '/vendor/autoload.php';

var_dump(class_exists(Some_Class::class));
var_dump(class_exists('Cool\NameSpaced\Class'));


Test in CLI

$ composer dump-autoload
$ php index.php
bool(true);
bool(true);

How to find out aliasing works?

commposer.json

{
    "autoload": {
        "classmap": ["src/"]
    }
}


Test in CLI

$ composer dump-autoload
$ php index.php
bool(true);
bool(false);
like image 172
Tomas Votruba Avatar answered Nov 10 '25 01:11

Tomas Votruba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!