whitecube/laravel-preset

适用于Whitecube工作流的定制Laravel预设。

v2.0.0 2023-04-13 09:48 UTC

README

此预设将为Whitecube的新Laravel项目安装和设置所需的一切。

要开始一个新项目

  1. 创建新的Laravel项目
laravel new my-project
cd my-project
  1. 安装预设
composer require whitecube/laravel-preset
  1. 激活预设
php artisan ui whitecube
  1. 完成!现在您可以编译、观察等!
yarn dev
yarn watch
yarn icons
yarn watch-icons

您期望的一切都应该在那里,您可以立即开始工作。

设置新的Hiker项目

执行上述命令后,执行以下操作

  1. composer.json 中添加hiker和trail仓库
{
    "repositories": {
        "hiker": {
            "type": "vcs",
            "url": "https://github.com/whitecube/hiker.git"
        },
        "trail": {
            "type": "vcs",
            "url": "https://github.com/whitecube/trail.git"
        }
    }
}
  1. 创建一个包含以下内容的 composer.local.json 文件
{
    "repositories": {
        "hiker": {
            "type": "path",
            "url": "../hiker/",
            "options": {
                "symlink": true
            }
        },
        "trail": {
            "type": "path",
            "url": "../trail/",
            "options": {
                "symlink": true
            }
        }
    }
}
  1. 在项目的文件夹中运行以下终端命令
composer require whitecube/hiker
php artisan hiker:install

创建新的可发布组件

为了创建一个可发布组件,只需在 src/Components/Publishers 中创建一个新的“Publisher”类,并实现 Whitecube\LaravelPreset\Components\PublisherInterface

namespace Whitecube\LaravelPreset\Components\Publishers;

use Whitecube\LaravelPreset\Components\File;
use Whitecube\LaravelPreset\Components\FilesCollection;
use Whitecube\LaravelPreset\Components\PublisherInterface;

class Wysiwyg implements PublisherInterface
{
    /**
     * Get the component's displayable name.
     */
    public function label(): string
    {
        return 'WYSIWYG section';
    }

    /**
     * Let the publisher prompt for eventual extra input
     * and return a collection of publishable files.
     */
    public function handle(): FilesCollection
    {
        $style = File::makeFromStub(
            stub: 'components/wysiwyg/part.scss',
            destination: resource_path('sass/parts/_wysiwyg.scss'),
        );

        $view = File::makeFromStub(
            stub: 'components/wysiwyg/view.blade.php',
            destination: resource_path('views/components/wysiwyg.blade.php'),
        );

        return FilesCollection::make([$style, $view]);
    }

    /**
     * Get the component's usage instructions
     */
    public function instructions(): ?string
    {
        return "1. Add `@import 'parts/wysiwyg';` to `resources/sass/app.scss`\r\n2. Use the blade component: `<x-wysiwyg><p>Some content</p></x-wysiwyg>`";
    }
}

大部分的重活将在发布者的 handle() 方法中完成。例如,这是一个提示额外组件特定信息并相应配置可发布文件的好地方。

handle() 方法的主要目的是收集和返回可发布文件,这就是为什么这个包提供了一个带有几个有用方法和功能的 File 类。首先,您可以选择使用以下方法之一创建一个 File 实例:

  • File::makeFromStub(string $destination, string $stub):当处理现有文件时很有用;
  • File::make(string $destination, string $content, ?string $origin = null):当从头创建文件时很有用。

大多数时候,应该使用 File::makeFromStub,以便在包的 components/[your-component] 目录中保持组件原始文件的清晰的提交历史。

这些 File 实例可以通过一些有用的方法在发布之前进行操作

  • $sassFile->replaceVariableValue('wysiwyg_width_columns', 10);
  • $sassFile->replaceBemBase('wysiwyg', 'foo');
  • $bladeFile->replaceBemBase('wysiwyg', 'foo');

当然,可以在发布者的 handle() 方法的任何地方使用 Laravel Prompts,这对于文件配置非常有用。

use function Laravel\Prompts\text;

$style = File::makeFromStub(
    stub: 'components/wysiwyg/part.scss',
    destination: resource_path('sass/parts/_wysiwyg.scss'),
);

$width = text(
    label: 'How many columns should the WYSIWYG\'s container width be?',
    default: 10,
    hint: 'Based on a 12 columns grid',
);

$style->replaceVariableValue('wysiwyg_width_columns', $width);