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



试图在新创建的Laravel项目中要求CrestApps,会给我以下消息:类CrestApps\CodeGenerator\DatabaseParser\SqlServerParser位于C:\xamplep/htdocs/cn/vendor/cresapps/laravel code generator/src\DatabaseParsers\SqlServerParser.php中,不符合psr-4自动加载标准。跳过

作曲家2.0.7版CrestApp 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 CrestAppsCodeGeneratorDatabaseParser;
use App;
use CrestAppsCodeGeneratorModelsField;
use CrestAppsCodeGeneratorModelsFieldOptimizer;
use CrestAppsCodeGeneratorSupportDatabaseParserParserBase;
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 CrestAppsCodeGeneratorModelsField $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 CrestAppsCodeGeneratorModelsField $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 CrestAppsCodeGeneratorModelsField $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 CrestAppsCodeGeneratorModelsField $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 CrestAppsCodeGeneratorModelsField $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 CrestAppsCodeGeneratorModelsField $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 CrestAppsCodeGeneratorModelsField $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 CrestAppsCodeGeneratorModelsField $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');
}
}

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

问题已解决。命令";composer需要crestapps/laravel代码生成器-dev";不再出现任何错误,成功生成优化的自动加载文件。

最新更新