当前位置: 首页 > 知识库问答 >
问题:

SqlServerParser.php不符合Crestapps中的psr-4自动加载标准

魏俊茂
2023-03-14

试图在新创建的Laravel项目中要求CrestApps,给我以下msg:位于C:/xampp/htdocs/cn/供应商/crestapps/laravel-code-Generer/src\数据库解析\SqlServerParser.php中的类CrestApps\CodeGenerator\数据库解析\SqlServerP不符合psr-4自动加载标准。跳过。

作曲家版本2.0.7 CrestApps版本2.4 Laravel安装程序4.1.1

Composer.json文件:

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.3|^8.0",
        "fideloper/proxy": "^4.4",
        "fruitcake/laravel-cors": "^2.0",
        "guzzlehttp/guzzle": "^7.0.1",
        "laravel/framework": "^8.12",
        "laravel/tinker": "^2.5"
    },
    "require-dev": {
        "crestapps/laravel-code-generator": "^2.4",
        "facade/ignition": "^2.5",
        "fakerphp/faker": "^1.9.1",
        "mockery/mockery": "^1.4.2",
        "nunomaduro/collision": "^5.0",
        "phpunit/phpunit": "^9.3.3"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
                "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}

SqlServerParser.php文件:

<?php
namespace CrestApps\CodeGenerator\DatabaseParser;

use App;
use CrestApps\CodeGenerator\Models\Field;
use CrestApps\CodeGenerator\Models\FieldOptimizer;
use CrestApps\CodeGenerator\Support\DatabaseParser\ParserBase;
use DB;
use Exception;

class SqlServerParser extends ParserBase
{
    /**
     * The table name.
     *
     * @var array
     */
    protected $tableName;

    /**
     * The databasename
     *
     * @var array
     */
    protected $databaseName;

    /**
     * The locale value
     *
     * @var array
     */
    protected $locale;

    /**
     * The final fields.
     *
     * @var array
     */
    protected $fields;

    /**
     * Creates a new field instance.
     *
     * @param string $tableName
     * @param string $databaseName
     *
     * @return void
     */
    public function __construct($tableName, $databaseName)
    {
        $this->tableName = $tableName;
        $this->databaseName = $databaseName;
        $this->locale = App::getLocale();
    }

    /**
     * Gets the final fields.
     *
     * @return array
     */
    public function getFields()
    {
        if (is_null($this->fields)) {
            $columns = $this->getColumn();

            if (empty($columns)) {
                throw new Exception('The table ' . $this->tableName . ' was not found in the ' . $this->databaseName . ' database.');
            }

            $this->fields = $this->transfer($columns);
        }

        return $this->fields;
    }

    /**
     * Gets column meta info from the information schema.
     *
     * @return array
     */
    protected function getColumn()
    {
        return DB::select(
            'SELECT
                             c.COLUMN_NAME
                            ,c.COLUMN_DEFAULT
                            ,c.IS_NULLABLE
                            ,c.DATA_TYPE
                            ,c.CHARACTER_MAXIMUM_LENGTH
                            ,pk.CONSTRAINT_TYPE AS EXTRA
                            FROM INFORMATION_SCHEMA.COLUMNS AS c
                            LEFT JOIN (
                                SELECT ku.TABLE_CATALOG,ku.TABLE_SCHEMA,ku.TABLE_NAME,ku.COLUMN_NAME, tc.CONSTRAINT_TYPE
                                FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc
                                INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS ku ON tc.CONSTRAINT_NAME = ku.CONSTRAINT_NAME
                            ) AS pk ON  c.TABLE_CATALOG = pk.TABLE_CATALOG
                                        AND c.TABLE_SCHEMA = pk.TABLE_SCHEMA
                                        AND c.TABLE_NAME = pk.TABLE_NAME
                                        AND c.COLUMN_NAME = pk.COLUMN_NAME
                            WHERE c.TABLE_NAME = ? AND c.TABLE_CATALOG = ? ',
            [$this->tableName, $this->databaseName]
        );
    }

    /**
     * Gets array of field after transfering each column meta into field.
     *
     * @param array $columns
     *
     * @return array
     */
    protected function transfer(array $columns)
    {
        $fields = [];

        foreach ($columns as $column) {
            $field = new Field($column->COLUMN_NAME);

            $this->setIsNullable($field, $column->IS_NULLABLE)
                ->setMaxLength($field, $column->CHARACTER_MAXIMUM_LENGTH)
                ->setDefault($field, $column->COLUMN_DEFAULT)
                ->setDataType($field, $column->DATA_TYPE)
                ->setKey($field, $column->COLUMN_KEY, $column->EXTRA)
                ->setLabel($field, $column->COLUMN_NAME)
                ->setHtmlType($field, $column->DATA_TYPE);

            $optimizer = new FieldOptimizer($field);

            $fields[] = $optimizer->optimize()->getField();
        }

        return $fields;
    }

    /**
     * Set the unsiged flag for a given field.
     *
     * @param CrestApps\CodeGenerator\Models\Field $field
     * @param string $type
     *
     * @return $this
     */
    protected function setUnsigned(Field &$field, $type)
    {
        if (strpos($type, 'unsigned') !== false) {
            $field->isUnsigned = true;
            $field->validationRules[] = sprintf('min:%s', 0);
        }

        return $this;
    }

    /**
     * Set the html type for a given field.
     *
     * @param CrestApps\CodeGenerator\Models\Field $field
     * @param string $type
     *
     * @return $this
     */
    protected function setHtmlType(Field &$field, $type)
    {
        $map = $this->getMap();

        if (array_key_exists($type, $map)) {
            $field->htmlType = $map[$type];
        }

        return $this;
    }

    /**
     * Set the data type for a given field.
     *
     * @param CrestApps\CodeGenerator\Models\Field $field
     * @param string $type
     *
     * @return $this
     */
    protected function setDataType(Field &$field, $type)
    {
        $map = $this->dataTypeMap();

        if (array_key_exists($type, $map)) {
            $field->dataType = $map[$type];
        }

        return $this;
    }

    /**
     * Set the nullable for a given field.
     *
     * @param CrestApps\CodeGenerator\Models\Field $field
     * @param string $nullable
     *
     * @return $this
     */
    protected function setIsNullable(Field &$field, $nullable)
    {
        $field->isNullable = (strtoupper($nullable) == 'YES');

        return $this;
    }

    /**
     * Set the max length for a given field.
     *
     * @param CrestApps\CodeGenerator\Models\Field $field
     * @param string $length
     *
     * @return $this
     */
    protected function setMaxLength(Field &$field, $length)
    {
        if (($value = intval($length)) > 0) {
            $field->validationRules[] = sprintf('max:%s', $value);
            $field->methodParams[] = $value;
        }

        return $this;
    }

    /**
     * Set the default value for a given field.
     *
     * @param CrestApps\CodeGenerator\Models\Field $field
     * @param string $default
     *
     * @return $this
     */
    protected function setDefault(Field &$field, $default)
    {
        if (!empty($default)) {
            $field->dataValue = $default;
        }

        return $this;
    }

    /**
     * Set the labels for a given field.
     *
     * @param CrestApps\CodeGenerator\Models\Field $field
     * @param string $name
     *
     * @return $this
     */
    protected function setLabel(Field &$field, $name)
    {
        $field->addLabel($this->getLabelName($name), $this->tableName, true, $this->locale);

        return $this;
    }

    /**
     * Set the keys for a given field.
     *
     * @param CrestApps\CodeGenerator\Models\Field $field
     * @param string $key
     * @param string $extra
     *
     * @return $this
     */
    protected function setKey(Field &$field, $key, $extra)
    {
        $key = strtoupper($key);

        if ($key == 'PRIMARY KEY') {
            $field->isPrimary = true;
        }

        if ($key == 'MUL') {
            $field->isIndex = true;
        }

        if ($key == 'UNI') {
            $field->isUnique = true;
        }

        if (strtolower($extra) == 'auto_increment') {
            $field->isAutoIncrement = true;
        }

        return $this;
    }

    /**
     * Gets a labe field's label from a given name.
     *
     * @return string
     */
    protected function getLabelName($name)
    {
        return trim(ucwords(str_replace(['-', '_'], ' ', $name)));
    }

    /**
     * Gets the eloquent method to html
     *
     * @return array
     */
    protected function getMap()
    {
        return config('codegenerator.eloquent_type_to_html_type');
    }

    /**
     * Gets the eloquent type to method collection
     *
     * @return array
     */
    public function dataTypeMap()
    {
        return config('codegenerator.eloquent_type_to_method');
    }
}

共有1个答案

归星驰
2023-03-14

根据Composer版本2关于psr-4自动加载标准的更改,文件SqlServerParser.php文件必须更改如下:行:命名空间CrestApps\CodeGenerator\数据库解析;要更改为:命名空间crestapps\laravelCodeGenerator\src\数据库解析;

问题解决了。命令“composer require crestaps/laravel代码生成器--dev”不再给出任何错误,成功生成优化的自动加载文件。

 类似资料:
  • 原始关闭原因未解决 我尝试使用作曲家自动加载,但我得到了这个错误 composer.json 我的终端输出

  • 我和许多其他人一样,似乎很难弄清楚何时以及如何使用自动加载。我想我理解作曲家和PSR-0/PSR-4的概念和这需要的目录结构。但是如果我用自己的MVC框架构建自己的项目 > 由于composer自带了自动加载器,它将加载我希望包含在项目中的所有依赖项,如果我不打算将我的网站变成分布式项目,我甚至需要自己的名称空间吗?为什么我不坚持使用include/requires? 最后,如果我采用名称空间,使

  • 我有一个在本地运行良好的Laravel项目(Mavericks),但是psr-4下的类没有加载到我们的后台服务器(CentOS)上。每次尝试composer更新或运行artisan命令时,我都会收到一个反射“未找到类”错误。 我的所有特定于应用程序的类都存储在我的Laravel项目的app/heatherland下,例如: 我的composer.json包含以下条目: 在本地,psr-4类被添加到

  • 1. 概述 本 PSR 是关于由文件路径 自动载入 对应类的相关规范, 本规范是可互操作的,可以作为任一自动载入规范的补充,其中包括 PSR-0,此外, 本 PSR 还包括自动载入的类对应的文件存放路径规范。 关于「能愿动词」的使用 为了避免歧义,文档大量使用了「能愿动词」,对应的解释如下: 必须 (MUST):绝对,严格遵循,请照做,无条件遵守; 一定不可 (MUST NOT):禁令,严令禁止;

  • 运行composer的,,,等时。;我突然收到一张黄色的反对通知,上面写着: 位于./Foo/Bar/utility/Baz.php中的类Foo\Bar\Baz不符合psr-4自动加载标准。跳过 在Composer 2.0之前,人们习惯于获得: 弃用通知:位于./Foo/Bar/Baz.php中的类Foo\Bar\Baz不符合psr-4自动加载标准。它将不再在Composer v2.0中自动加载。

  • 我开始学习Composer,并正在开发一个系统,在该系统中,我将文件与核心应用程序文件分开,如下所示: 所以,要在composer中设置此设置。json文件并访问所有类/App much/Core会这样吗? 还是有更正确的方法? 我也读过PSR-0和PSR-4的比较,我仍然有点怀疑应该使用哪一种。在我的情况下,我应该实现什么,PSR-0还是PSR-4?