oscarnevarezleal/laravel-sed

0.0.4 2021-06-18 18:45 UTC

This package is auto-updated.

Last update: 2024-09-17 05:12:37 UTC


README

A CLI utility that modifies Laravel configuration files.

注意: 此项目主要是一个CLI工具,用于从外部管理Laravel应用程序,尽管它包含几个Laravel命令,但大多数功能将无法作为Laravel命令使用。

安装

composer global require oscarnevarezleal/laravel-sed

使用方法

Larased 0.0.4

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display help for the given command. When no command is given display help for the list command
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  help                  Displays help for a command
  list                  Lists commands
 larased
  larased:config-edit   Edits a config file

命令

选项

概念

在深入示例之前,先明确一下配置路径的概念,这样你知道在编辑Laravel配置文件时如何使用它。

配置路径

大多数命令都需要一个有效的 config-path 才能工作。

配置路径由两部分组成,由斜杠连接。

第一部分是从项目根目录开始的要修改的文件的相对路径,没有特殊字符和文件扩展名。

第二部分是在文件中找到的数组属性路径。

示例

./config/app.php 中的属性 name 成为配置路径 config.app/name

嵌套属性,例如在 ./config/database.php 中配置的mysql连接的用户

示例

文字值

以配置文件 config/app.php 的以下部分为例。

# config/app.php
<?php

return [
    'name' => 'App',
    /*
    |--------------------------------------------------------------------------
    | Application Debug Mode
    |--------------------------------------------------------------------------
    |
    | When your application is in debug mode, detailed error messages with
    | stack traces will be shown on every error that occurs within your
    | application. If disabled, a simple generic error page is shown.
    |
    */
    'debug' => (bool) env('APP_DEBUG', false),
    /*
    |--------------------------------------------------------------------------
    | Application URL
    |--------------------------------------------------------------------------
    |
    | This URL is used by the console to properly generate URLs when using
    | the Artisan command line tool. You should set this to the root of
    | your application so that it is used when running Artisan tasks.
    |
    */
    'url' => env('APP_URL', 'https://'),
    'asset_url' => env('ASSET_URL', null)
    
    #....
];

如果您想更改文字值,例如将 name 更改为 MyAwesomeApp,请运行以下命令

larased larased:config-edit config.app/name my-awesome-app  

它将得到以下结果

<?php

return [
    'name' => 'my-awesome-app',

关于环境值检查默认值怎么办?在这种情况下,我们需要使用 -e 标志,如下所示

larased larased:config-edit config.app/name -e APP_NAME my-awesome-app  

它将得到以下结果

<?php

return [
    'name' => env('APP_NAME', 'MyAwesomeApp'),

嵌套属性

一些其他属性不是很容易找到,而是嵌套在数组路径下。以mysql连接的用户名为例。

larased larased:config-edit config.database/connections.mysql.username noroot

它将成功更新,如下所示。

# ...
	'connections' => [
		'sqlite' => [
			'driver' => 'sqlite',
			'url' => env('DATABASE_URL'),
			'database' => env('DB_DATABASE', database_path('database.sqlite')),
			'prefix' => '',
			'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
		],
		'mysql' => [
			'driver' => 'mysql',
			'url' => env('DATABASE_URL'),
			'host' => env('DB_HOST', '127.0.0.1'),
			'port' => env('DB_PORT', '3306'),
			'database' => env('DB_DATABASE', 'forge'),
			'username' => 'no-root',

# ...

带有默认值的链式环境变量

有时没有单个环境变量,而是多个环境变量需要在使用默认值之前使用。在这种情况下,-e 标志有一个方便的变体,我们可以使用。环境变量名称的列表可以用竖线 | 分隔。

larased larased:config-edit config.database/connections.mysql.username noroot -e "DB_USER|DB_USER_ENV"

Docker

# Pull latest
docker pull docker pull docker.pkg.github.com/oscarnevarezleal/laravel-sed/laravel-sed:dev

# create an alias
alias larased='docker run --rm -it -v `pwd`:/var/laraseed:ro laravel-sed:latest'
#windows
docker run --rm -it -v ${PWD}:/var/laraseed:ro laravel-sed:latest config.app/name my-awesome-app 

#linux and MacOS
docker run --rm -it -v `pwd`:/var/laraseed:ro laravel-sed:latest config.app/name my-awesome-app   

已知缺点

如何应用编码标准?

此包使用基于称为 抽象语法树 (AST) 的技术的 nikic/php-parser。AST不知道空格,在写入文件时,它会产生格式较差的代码,无论是PHP还是docblock注释。因此,您的项目需要有一个编码标准工具,如 ECS