You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A new open-source tool to rapidly generate multiple Laravel components using an expressive, human readable syntax.
1
+

3
2
4
-
Follow along with the development of Blueprint by [watching live streams](https://www.youtube.com/playlist?list=PLmwAMIdrAmK5q0c0JUqzW3u9tb0AqW95w) or [reviewing issues](https://github.com/laravel-shift/blueprint/issues).
3
+
_Blueprint_ is an open-source tool for **rapidly generating multiple** Laravel components from a **single, human readable** definition.
5
4
6
-
_**v0.1 Tagged**: A beta release of Blueprint is now available which supports generating components using `models` definitions._
5
+
Watch a quick [demo of Blueprint](https://twitter.com/gonedark/status/1192513115826589698?s=20) in action and continue reading this document to get started.
6
+
7
+
-[Installation](#installation)
8
+
-[Requirements](#requirements)
9
+
-[Basic Usage](#basic-usage)
10
+
-[Defining Components](#defining-components)
11
+
-[Contributing](#contributing)
7
12
8
13
9
14
## Installation
10
-
You can install this package via composer using this command:
15
+
You can install Blueprint via composer using the following command:
11
16
12
17
```sh
13
18
composer require --dev laravel-shift/blueprint
14
19
```
15
20
16
-
The package will automatically register itself.
21
+
Blueprint will automatically register itself using [package discovery](https://laravel.com/docs/packages#package-discovery).
22
+
23
+
## Requirements
24
+
Blueprint requires a Laravel application running version 6.0 or higher.
17
25
26
+
While Blueprint may be more flexible in a future version, it currently assumes a standard project structure using the default `App` namespace.
18
27
19
-
## Usage
28
+
## Basic Usage
20
29
Blueprint adds an artisan command.
21
30
22
31
```sh
23
32
php artisan blueprint:build [draft]
24
-
25
33
```
26
34
27
-
The `draft` file contains your definition of the components to generate. By default, the `blueprint:build` command automatically loads a `draft.yaml` file.
35
+
The _draft_ file contains a [definition of the components](#defining-components) to generate. By default, the `blueprint:build` command attempts to load a `draft.yaml` file from the project root folder.
36
+
28
37
38
+
## Defining Components
39
+
Use Blueprint's `artisan` commands you may generate multiple Laravel components from a single definition called a _draft_ file.
29
40
30
-
---
41
+
Within this draft file you define _models_ and _controllers_ using an expressive, human-readable YAML syntax.
31
42
43
+
Let's review the following draft file:
32
44
33
-
**Example Syntax**
34
45
```yaml
35
46
models:
36
47
Post:
@@ -54,18 +65,248 @@ controllers:
54
65
redirect: post.index
55
66
```
56
67
57
-
**Generated Components**
58
-
- [x] Migration
59
-
- [x] Model
60
-
- [x] Factory
61
-
- [x] Route
62
-
- [x] Controller
63
-
- [x] Form Request
64
-
- [x] Mailable
65
-
- [x] Job
66
-
- [x] Event
67
-
- [x] View (stub)
68
+
From these simple 20 lines of YAML, Blueprint will generate all of the following Laravel components:
69
+
70
+
- A _model_ class for `Post` complete with `fillable`, `casts`, and `dates` properties, as well as relationships methods.
71
+
- A _migration_ to create the `posts` table.
72
+
- A [_factory_](https://laravel.com/docs/database-testing) intelligently setting columns with fake data.
73
+
- A _controller_ class for `PostController` with `index` and `store` actions complete with code generated for each [statement](#statements).
74
+
- _Routes_ for the `PostController` actions.
75
+
- A [_form request_](https://laravel.com/docs/validation#form-request-validation) of `StorePostRequest` validating `title` and `content` based on the `Post` model definition.
76
+
- A _mailable_ class for `ReviewNotification` complete with a `post` property set through the _constructor_.
77
+
- A _job_ class for `SyncMedia` complete with a `post` property set through the _constructor_.
78
+
- An _event_ class for `NewPost` complete with a `post` property set through the _constructor_.
79
+
- A _Blade template_ of `post/index.blade.php` rendered by `PostController@index`.
80
+
81
+
While this draft file only defines a single model and controller, you may define multiple [models](#models) and [controllers](#controllers).
82
+
83
+
### Models
84
+
Within the `models` section of a draft file you may define multiple models. Each model begins with a _name_ followed by a list of columns. Columns are `key: value` pairs where `key` is the column name and `value` defines its attributes.
85
+
86
+
Expanding on the example above, this draft file defines multiple models:
87
+
88
+
```yaml
89
+
models:
90
+
Post:
91
+
title: string:400
92
+
content: longtext
93
+
published_at: nullable timestamp
94
+
95
+
Comment:
96
+
content: longtext
97
+
published_at: nullable timestamp
98
+
99
+
# additional models...
100
+
```
101
+
102
+
From this definition, Blueprint creates two models: `Post`and `Comment`, respectively. You may continue to define additional models.
103
+
104
+
Blueprint recommends defining the model name in its _StudlyCase_, singular form to follow Laravel naming conventions. For example, `Post` instead of `post` or `posts`.
105
+
106
+
Similarly, column names will be used as-is. The attributes of these columns may be any of the [column types](https://laravel.com/docs/migrations#creating-columns) and [column modifiers](https://laravel.com/docs/migrations#column-modifiers) available in Laravel. You may define these as-is or using lowercase.
107
+
108
+
For complex attributes, you may use a `key:value` pair. From these example above, `string:400` defines a `string` column type with a maximum length of `400` characters. Other examples include `enum:'foo','bar','baz'` or `decimal:10,2`.
109
+
110
+
By default, each model will automatically be defined with an `id` and _timestamps_ columns. To disable these columns you may define them with a `false` value. For example, `timestamps: false`.
111
+
112
+
Blueprint also offers additional _shorthands_ which will be expanded into valid YAML. Shorthands include an `id` data type, as well as defining [soft deleting](https://laravel.com/docs/eloquent#soft-deleting) models.
113
+
114
+
For example:
115
+
116
+
```yaml
117
+
models:
118
+
Comment:
119
+
user_id: id
120
+
softDeletes
121
+
# ...
122
+
```
123
+
124
+
Using these shorthands, Blueprint will generate a `Comment` class using the `SoftDeletes` trait. It will also create a `user_id` column with the appropriate data type for an integer foreign key.
125
+
126
+
Blueprint also inspects columns and assigns them to `fillable`, `casts`, and `dates` properties, as well as generate relationships methods.
127
+
128
+
By default, all columns except for `id` and _timestamps_ will be added to the `fillable` property.
129
+
130
+
Where appropriate, Blueprint will [cast](https://laravel.com/docs/5.8/eloquent-mutators#attribute-casting) columns to `integer`, `boolean`, and `decimal` types. Any _date_ columns will be added to the `dates` properties.
131
+
132
+
Columns which use an `id` data type or end with `_id` will be used to generate `belongsTo` relationships. By default, Blueprint uses the column name prefix for the related model. If you define a relationship for a different model, you may use a `id:model` syntax.
133
+
134
+
For example:
135
+
136
+
```yaml
137
+
models:
138
+
Post:
139
+
author_id: id:user
140
+
# ...
141
+
```
142
+
143
+
144
+
### Controllers
145
+
Similar to `models`, you may also define multiple `controllers`. Within the `controllers` section you define a _controller_ by name. Each controller may define multiple `actions` which contain a list of [statements](#statements).
146
+
147
+
Expanding on the example above, this draft file defines multiple controllers:
148
+
149
+
```yaml
150
+
controllers:
151
+
Post:
152
+
index:
153
+
query: all
154
+
render: post.index with:posts
155
+
create:
156
+
render: post.create
157
+
store:
158
+
validate: title, content
159
+
save: post
160
+
redirect: post.index
161
+
162
+
Comment:
163
+
show:
164
+
render: comment.show with:show
165
+
166
+
# additional controller...
167
+
```
168
+
169
+
From this definition, Blueprint will generate two controllers. A `PostController` with `index`, `create`, and `store` actions. And a `CommentController` with a `show` action.
170
+
171
+
While you may specify the full name of a controller, Blueprint will automatically suffix names with `Controller`.
172
+
173
+
Blueprint encourages you to define [resource controllers](https://laravel.com/docs/controllers#resource-controllers). Doing so allows Blueprint to infer details and generate even more code automatically.
174
+
175
+
176
+
#### Statements
177
+
Blueprint comes with an expressive set of statements which implicitly define additional components to generate. Each statement is a `key: value` pair.
178
+
179
+
The `key` defines the _type_ of statement to generate. Currently, Blueprint supports the following types of statements:
180
+
181
+
<dl>
182
+
<dt>validate</dt>
183
+
<dd>
184
+
185
+
Generates a form request with _rules_ based on the referenced model definition. Blueprint accepts a `value` containing a comma separated list of column names.
186
+
187
+
For example:
188
+
189
+
```yaml
190
+
validate: title, content, author_id
191
+
```
192
+
193
+
Blueprint also updates the type-hint of the injected request object.</dd>
194
+
195
+
<dt>find</dt>
196
+
<dd>
197
+
198
+
Generates an Eloquent `find` statement. If the `value` provided is a qualified [reference](#references), Blueprint will expand the reference to determine the model. Otherwise, Blueprint will attempt to use the controller to determine the related model.</dd>
199
+
200
+
<dt>query</dt>
201
+
<dd>
202
+
203
+
Generates an Eloquent query statement using `key:value` pairs provided in `value`. Keys may be any of the basic query builder methods for [`where` clauses](https://laravel.com/docs/queries#where-clauses) and [ordering](https://laravel.com/docs/queries#ordering-grouping-limit-and-offset).
Currently, Blueprint supports generating query statements for `all`, `get`, `pluck`, and `count`.</dd>
212
+
213
+
214
+
<dt>save/delete</dt>
215
+
<dd>
216
+
217
+
Generates an Eloquent statement for saving a model. Blueprint uses the controller action to infer which statement to generate.
218
+
219
+
For example, for a `store` controller action, Blueprint will generate a `Model::create()` statement. Otherwise, a `$model->save()` statement will be generated.
220
+
221
+
Similarly, within a `destroy` controller action, Blueprint will generate a `$model->delete()` statement. Otherwise, a `Model::destroy()` statement will be generated.</dd>
222
+
223
+
<dt>flash</dt>
224
+
<dd>
225
+
226
+
Generates a statement to [flash data](https://laravel.com/docs/session#flash-data) to the session. Blueprint will use the `value` as the session key and expands the reference as the session value.</dd>
227
+
228
+
<dt>render</dt>
229
+
<dd>Generates a `return view();` statement complete with a template reference and data.
230
+
231
+
For example:
232
+
233
+
```yaml
234
+
view: post.show with:post
235
+
```
236
+
237
+
When the template does not exist, Blueprint will generate the Blade template for the view.</dd>
238
+
68
239
240
+
<dt>redirect</dt>
241
+
<dd>Generates a `return redirect()` statement using the `value` as a reference to a named route passing any data as parameters.
242
+
243
+
For example:
244
+
245
+
```yaml
246
+
redirect: post.show with:post
247
+
```
248
+
</dd>
249
+
250
+
251
+
<dt>dispatch</dt>
252
+
<dd>
253
+
254
+
Generates a statement to dispatch a [Job](https://laravel.com/docs/queues#creating-jobs) using the `value` to instantiate an object and pass any data.
255
+
256
+
For example:
257
+
258
+
```yaml
259
+
dispatch: SyncMedia with:post
260
+
```
261
+
262
+
If the referenced _job_ class does not exist, Blueprint will create one using any data to define properties and a `__construct` method which assigns them.</dd>
263
+
264
+
<dt>fire</dt>
265
+
<dd>
266
+
267
+
Generates a statement to dispatch a [Event](https://laravel.com/docs/events#defining-events) using the `value` to instantiate the object and pass any data.
268
+
269
+
For example:
270
+
271
+
```yaml
272
+
fire: NewPost with:post
273
+
```
274
+
275
+
If the referenced _event_ class does not exist, Blueprint will create one using any data to define properties and a `__construct` method which assigns them.</dd>
276
+
277
+
278
+
<dt>send</dt>
279
+
<dd>
280
+
281
+
Generates a statement to send a [Mailable](https://laravel.com/docs/mail#generating-mailables) using the `value` to instantiate the object, specify the recipient, and pass any data.
282
+
283
+
For example:
284
+
285
+
```yaml
286
+
send: ReviewNotification to:post.author with:post
287
+
```
288
+
289
+
If the referenced _mailable_ class does not exist, Blueprint will create one using any data to define properties and a `__construct` method which assigns them.</dd>
290
+
</dl>
291
+
292
+
#### References
293
+
For convenience, Blueprint will use the name of a controller to infer the related model. For example, Blueprint will assume a `PostController` relates to a `Post` model.
294
+
295
+
Blueprint also supports a dot (`.`) syntax for more complex references. This allows you to define values which reference columns on other models.
296
+
297
+
For example, to _find_ a `User` model within the `PostController` you may use:
298
+
299
+
```yaml
300
+
controllers:
301
+
Post:
302
+
show:
303
+
find: user.id
304
+
# ...
305
+
```
306
+
307
+
While these references will often be used within _Eloquent_ and `query` statements, they may be used in other statements as well. When necessary, Blueprint will convert these into variable references using an arrow (`->`) syntax.
69
308
70
309
## Contributing
71
-
Contributions should be submitted to the `master` branch. Any submissions should be complete with tests and adhere to the [PSR-2 code style](https://www.php-fig.org/psr/psr-2/). You may also contribute by [opening an issue](https://github.com/laravel-shift/blueprint/issues).
310
+
Contributions may be made by submitting a Pull Request against the `master` branch. Any submissions should be complete with tests and adhere to the [PSR-2 code style](https://www.php-fig.org/psr/psr-2/).
311
+
312
+
You may also contribute by [opening an issue](https://github.com/laravel-shift/blueprint/issues) to report a bug or suggest a new feature.
0 commit comments