2
votes

I have a question with regards to refactoring a legacy PHP application to be compatible with PHP's PSR-4 standards. I have some classes located at app/classes/ folder which are not yet namespaced properly and I want them to be autoloaded directly when I call composer's vendor/autoload.php. I added a name space according to \<NamespaceName>(\<SubNamespaceNames>)*\<ClassName> and I have tried creating a vendor/myapp/classes/src/ directory under the vendor folder of composer, and executed a dump-autoload command but to no avail. The class doesn't get loaded up and composer can't figure out where to find it. Any pointers on how to do this?

Thanks,

Jan

Thing/s to Note: -> I don't want to upload the source code to any repository that can be publicly searchable like Packagist as the code is for internal use only.

EDIT: Here is my composer.json:

...
  "autoload":{
    "psr-4": {
      "Myapp\\" : "Myapp"
    }
  },
...

But now the structure looks like:

->Myapp
-->Classes
--->Module1
---->submodule.php
--->Module2
---->submodule2.php
--->Module3
---->submodule3.php
--->Config
---->config.db.php

->vendor
-->autoload.php

Here are my issues/questions:

  1. When I try to load submodule.php, which in turn would load submodule2.php and submodule3.php, it would tell me that submodule3.php is not found. The namespace of submodule3.php is Myapp\Classes\Module3 but it says its not found.
  2. I want to include forcibly, config.db.php, on every call of autoload.php
2
Please post your composer.json. You would need to tell Composer where to find these classes with a "psr-4": directive like getcomposer.org/doc/01-basic-usage.md#autoloadingMichael Berkowski

2 Answers

1
votes

I now figured it out. But the source files will not reside in the /vendor folder, which is okay for my case. If you want to make your files be autoloaded automatically no matter which folder, just add it to the psr-4 block in the composer.json, or create it if it's not yet there. In my composer.json, I have this block on the top level object, which adds the folder I want to get autoloaded and includes also the specific file I want to include, like a config file of some sorts:

  ... 
  "autoload":{
    "files": [
      "somefolder/somefile.php"
    ],
    "psr-4": {
      "MyApp\\" : "MyApp"
    }
  },
  ...

Which simply means that composer should autoload files residing in the MyApp directory which will also have a namespace of MyApp.

So my folder structure looks like this now:

->MyApp
-->Classes
--->MyClass1.php   --> namespace MyApp\Classes classname: MyClass1
-->Components
--->MyClass2.php   --> namespace MyApp\Classes classname: MyClass2
->somefolder
-->somefile.php
->vendor
-->autoload.php
->index.php

So if I want to include the file MyClass1.php in index.php, I will just add it like:

include_once( __DIR__ . "/vendor/autoload.php");

use \MyApp\Classes\MyClass1;

$foo = new MyClass1();
$foo->bar();

Hope it helps.

0
votes

You could approach this by creating your own composer package, adding it to your private git repository, and adding the package to your project's composer.json file:

"require": {
    "{your-vendor}/{package-name}": "dev-master"
},
"repositories": [
    {
        "type": "vcs",
        "url": "git@bitbucket.org:{your-vendor}/{package-name}.git"
    }
],

Once this is done, run composer update.