nWidart / laravel-modules

Module Management In Laravel
https://docs.laravelmodules.com
MIT License
5.38k stars 946 forks source link

Class CreateTestTable located in ./database/Migrations/2021_05_03_093750_create_test_table.php does not comply with psr-4 autoloading standard Skipping. #1877

Open sudheerkumarala opened 3 weeks ago

sudheerkumarala commented 3 weeks ago

Hello @all,

I have been trying to understand what this issue is for almost 2 days and hence need your support to understand.

I am using PHPStorm and I have been using 8.1 and updated to 8.2, with the changes now I am getting a warning saying Class CreateTestTable located in ./database/Migrations/2021_05_03_093750_create_test_table.php doesn't comply with psr-4 autoloading standard.

For Info: I am using the nwidart/laravel-modules(v9) saying that this file is in one of the modules.

here is my composer.json for psr-4 section

 "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Modules\\": "Modules/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/",
        },
    },

and this is the composer.json in the test module in the directory project/Modules/Test making the directory to root

"autoload": {
        "psr-4": {
            "Modules\\Test\\": ""
        }
    }

This is an issue as I am not able to install the composer into my modules as necessary.

Can anyone help me to understand this?

sudheerkumarala commented 3 weeks ago

Hi @alissn,

I am currently using Laravel 11 and laravel-modules(v9).

alissn commented 3 weeks ago

You can update your package version to 11 and read the upgrade section.

Also, migrations in the root of the project do not need a namespace (only factories and seeders in the database folder need namespaces). You can remove the namespace and change it to the new migration style:

Example:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Your migration code here
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Your rollback code here
    }
};
sudheerkumarala commented 3 weeks ago

Hi @alissn,

That might work if I have a single migration or 2, however I currently have more than 10 modules and each module has a minimum of 10-20 migrations which is tiresome. Can we know what the issue is here and any possible way to resolve it using composer.json or is it just because of laravel-modules version update alone??

FYI, I am trying this with an IIS server, composer 2.7, Laravel 11, and PHP 8.2

but it was working with the following setup(previous setup) [where everything was working as expected] Apache2.4 PHP 8.1 Laravel 10 composer 2.6

will this information help to solve this issue?

Please let me know.

alissn commented 3 weeks ago

With the old version, your folder structure should look like this:

.
├── Database
│   ├── Migrations
│   │   └── 2020_07_21_121709_create_permission_tables.php
│   ├── Seeders
│   │   └── BaseDatabaseSeeder.php
│   └── factories

Ensure that the Database and Migrations folder names are correct (not 'database' or 'migration').

Check this and share the results.

sudheerkumarala commented 3 weeks ago

Hi @alissn,

I have checked it and made sure it is the same image

and I just found that by using the older version of composer, it is working good.

Do you happen to know why??

alissn commented 3 weeks ago

on error say CreateTestTable located in ./database/Migrations/2021_05_03_093750_create_test_table

you have folder with name database. find it and change it to Database

sudheerkumarala commented 3 weeks ago

Hi @alissn ,

Class CreateThemeTable located in ./Modules/Admin/Database/Migrations/2021_05_03_093750_create_theme_table.php does not comply with psr-4 autoloading standard (rule: Modules\ => ./Modules). Skipping.

This is the exact error message that I got in the messages and many more in the same format.

There is no folder named database and I made sure of it.

alissn commented 3 weeks ago

@sudheerkumarala Add a namespace to your migration, but I'm not sure if it's correct. 🤔 Migrations typically don't need a namespace.

Try adding this to your migration file:

namespace Modules\Admin\Database\Migrations;

Check if this solution works.

sudheerkumarala commented 3 weeks ago

I have checked it and didn't work.

As I said, I am seeing this issue with the newer version of the composer and not with an older version, don't know why?

alissn commented 3 weeks ago

i don't know more.

latest version of composer is 2.7.7 and work very well on my system:

 Composer version 2.7.7 2024-06-10 22:11:12
solomon-ochepa commented 3 weeks ago

Hello @ALL,

I have been trying to understand what this issue is for almost 2 days and hence need your support to understand.

I am using PHPStorm and I have been using 8.1 and updated to 8.2, with the changes now I am getting a warning saying Class CreateTestTable located in ./database/Migrations/2021_05_03_093750_create_test_table.php doesn't comply with psr-4 autoloading standard.

For Info: I am using the nwidart/laravel-modules(v9) saying that this file is in one of the modules.

here is my composer.json for psr-4 section

 "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Modules\\": "Modules/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/",
        },
    },

and this is the composer.json in the test module in the directory project/Modules/Test making the directory to root

"autoload": {
        "psr-4": {
            "Modules\\Test\\": ""
        }
    }

This is an issue as I am not able to install the composer into my modules as necessary.

Can anyone help me to understand this?

The issue is coming from your migration definition. Can you please share a sample of any of your migration files?

For instance, it is recommended to use an anonymous class.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('cache', function (Blueprint $table) {
            $table->string('key')->primary();
            $table->mediumText('value');
            $table->integer('expiration');
        });

        Schema::create('cache_locks', function (Blueprint $table) {
            $table->string('key')->primary();
            $table->string('owner');
            $table->integer('expiration');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('cache');
        Schema::dropIfExists('cache_locks');
    }
};
sudheerkumarala commented 3 weeks ago

I understand that the problem is with the migration file names however, there are like more than 70 migration files, and is not really a good option to change all.

On top of that, this new problem I am getting is only when I use the IIS server, not with Apache server, and even with IIS only when the composer version is 2.7

any idea why??

solomon-ochepa commented 3 weeks ago

I understand that the problem is with the migration file names however, there are more than 70 migration files, and is not really a good option to change all.

On top of that, this new problem I am getting is only when I use the IIS server, not with Apache server, and even with IIS only when the composer version is 2.7

any idea why??

Your code should be updated at all times, and this is one of those instances. You are advised to update your migrations.

You can write a command to automate the process or handle it manually.

sudheerkumarala commented 3 weeks ago

we can do that in a module, yes of course? but can we do it for all modules with a command??

solomon-ochepa commented 3 weeks ago

we can do that in a module, yes of course? but can we do it for all modules with a command??

Yes!

Please can I see a sample of your migration file? Past the full code here (remove all sensitive data).

sudheerkumarala commented 3 weeks ago

Sure,

here is the migration file code

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateThemeTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('themes', function (Blueprint $table) {
            $table->id();

            $table->timestamps();
            //other columns
            $table->boolean('active')->default(false);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('themes');
    }
}
sudheerkumarala commented 2 weeks ago

Hello @solomon-ochepa,

I have made a command to change the class names of the migration files in the module. However, I am getting this error from an autoload PHP file.

Class ComposerAutoloaderInit0cc84ad303d6b5be7a4fe417aa3e165b located in ./Modules/module1/vendor/composer/autoload_real.php does not comply with psr-4 autoloading standard (rule: Modules\ => ./Modules). Skipping.
Class Composer\Autoload\ComposerStaticInit0cc84ad303d6b5be7a4fe417aa3e165b located in ./Modules/module1/vendor/composer/autoload_static.php does not comply with psr-4 autoloading standard (rule: Modules\ => ./Modules). Skipping.
Class Composer\Autoload\ClassLoader located in ./Modules/module1/vendor/composer/ClassLoader.php does not comply with psr-4 autoloading standard (rule: Modules\ => ./Modules). Skipping.
Class Composer\InstalledVersions located in ./Modules/module1/vendor/composer/InstalledVersions.php does not comply with psr-4 autoloading standard (rule: Modules\ => ./Modules). Skipping.
Class ComposerAutoloaderInit6d564eb7ce242f8db23ca4216db25b70 located in ./Modules/module2/vendor/composer/autoload_real.php does not comply with psr-4 autoloading standard (rule: Modules\ => ./Modules). Skipping.
Class Composer\Autoload\ComposerStaticInit6d564eb7ce242f8db23ca4216db25b70 located in ./Modules/module2/vendor/composer/autoload_static.php does not comply with psr-4 autoloading standard (rule: Modules\ => ./Modules). Skipping.
Class Composer\Autoload\ClassLoader located in ./Modules/module2/vendor/composer/ClassLoader.php does not comply with psr-4 autoloading standard (rule: Modules\ => ./Modules). Skipping.
Class Composer\InstalledVersions located in ./Modules/module2/vendor/composer/InstalledVersions.php does not comply with psr-4 autoloading standard (rule: Modules\ => ./Modules). Skipping.

I am getting these for every module after the class names are changed.

Any idea what the issue can be and how to resolve it??

New information The new version of Composer 2.7.7 with strict compliance checks doesn't support having vendors in the modules and is affecting the classNames when autoloading the files.

solomon-ochepa commented 2 weeks ago

Can I look into your code for more personalized support? WhatsApp: https://wa.me/2348162672618 Telegram: https://t.me/solomon_ochepa

Please disregard any concerns about cost. Additionally, the final solution will be made available here for future reference.