Skip to content

Commit 74ee91d

Browse files
committed
update
1 parent 4987c9c commit 74ee91d

File tree

5 files changed

+23
-114
lines changed

5 files changed

+23
-114
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
/vendor/
2+
3+
# ide
4+
.idea
5+
yii2-stubs-generator.iml

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014 Vasily Ostanin
3+
Copyright (c) 2015 Vasily Ostanin
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 12 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
Faker Extension for Yii 2
2-
=========================
1+
Yii::$app stubs generator extension for Yii 2
2+
=============================================
33

4-
This extension provides a [`Faker`](https://github.com/fzaninotto/Faker) fixture command for Yii 2.
4+
This extension provides no-more-butthurt generator command for Yii 2.
55

6-
This repository is a git submodule of <https://github.com/yiisoft/yii2>.
7-
Please submit issue reports and pull requests to the main repository.
8-
For license information check the [LICENSE](LICENSE.md)-file.
6+
![in action](http://monosnap.com/image/vCnH1SWaXwKXLuchkutNGZXkeeoYWK.png)
97

108
Installation
119
------------
@@ -15,13 +13,13 @@ The preferred way to install this extension is through [composer](http://getcomp
1513
Either run
1614

1715
```
18-
php composer.phar require --prefer-dist yiisoft/yii2-faker
16+
php composer.phar require --prefer-dist bazilio/yii2-stubs-generator
1917
```
2018

2119
or add
2220

2321
```json
24-
"yiisoft/yii2-faker": "~2.0.0"
22+
"bazilio/yii2-stubs-generator": "~0.0.1"
2523
```
2624

2725
to the require section of your composer.json.
@@ -35,113 +33,15 @@ To use this extension, simply add the following code in your application config
3533
```php
3634
'controllerMap' => [
3735
'fixture' => [
38-
'class' => 'yii\faker\FixtureController',
36+
'class' => 'bazilio\stubsgenerator\StubsController',
3937
],
4038
],
4139
```
4240

43-
Define a `tests` alias in your console config. For example, for the `basic` application template, this should be added
44-
to the `console.php` configuration: `Yii::setAlias('tests', __DIR__ . '/../tests');`
45-
To start using this command you need to be familiar (read guide) with the [Faker](https://github.com/fzaninotto/Faker) library and
46-
generate fixture template files, according to the given format:
47-
48-
```php
49-
// users.php file under template path (by default @tests/unit/templates/fixtures)
50-
/**
51-
* @var $faker \Faker\Generator
52-
* @var $index integer
53-
*/
54-
return [
55-
'name' => $faker->firstName,
56-
'phone' => $faker->phoneNumber,
57-
'city' => $faker->city,
58-
'password' => Yii::$app->getSecurity()->generatePasswordHash('password_' . $index),
59-
'auth_key' => Yii::$app->getSecurity()->generateRandomString(),
60-
'intro' => $faker->sentence(7, true), // generate a sentence with 7 words
61-
];
62-
```
63-
64-
As you can see, the template file is just a regular PHP script. The script should return an array of key-value
65-
pairs, where the keys represent the table column names and the values the corresponding value. When you run
66-
the `fixture/generate` command, the script will be executed once for every data row being generated.
67-
In this script, you can use the following two predefined variables:
68-
69-
* `$faker`: the Faker generator instance
70-
* `$index`: the current fixture index. For example if user need to generate 3 fixtures for user table, it will be 0..2.
71-
72-
With such a template file, you can generate your fixtures using the commands like the following:
73-
74-
```
75-
# generate fixtures from user fixture template
76-
php yii fixture/generate user
77-
78-
# to generate several fixture data files
79-
php yii fixture/generate user profile team
80-
```
81-
82-
In the code above `users` is template name. After running this command, a new file with the same template name
83-
will be created under the fixture path in the `@tests/unit/fixtures`) folder.
84-
85-
```
86-
php yii fixture/generate-all
87-
```
88-
89-
This command will generate fixtures for all template files that are stored under template path and
90-
store fixtures under fixtures path with file names same as templates names.
91-
You can specify how many fixtures per file you need by the `--count` option. In the code below we generate
92-
all fixtures and in each file there will be 3 rows (fixtures).
93-
94-
```
95-
php yii fixture/generate-all --count=3
9641
```
97-
You can specify different options of this command:
98-
99-
```
100-
# generate fixtures in russian language
101-
php yii fixture/generate User --count=5 --language='ru_RU'
102-
103-
# read templates from the other path
104-
php yii fixture/generate-all --templatePath='@app/path/to/my/custom/templates'
42+
# generate fixtures for console application
43+
php yii stubs console
10544
106-
# generate fixtures into other directory.
107-
php yii fixture/generate-all --fixtureDataPath='@tests/acceptance/fixtures/data'
108-
```
109-
110-
You can see all available templates by running command:
111-
112-
```
113-
# list all templates under default template path (i.e. '@tests/unit/templates/fixtures')
114-
php yii fixture/templates
115-
116-
# list all templates under specified template path
117-
php yii fixture/templates --templatePath='@app/path/to/my/custom/templates'
118-
```
119-
120-
You also can create your own data providers for custom tables fields, see [Faker](https://github.com/fzaninotto/Faker) library guide for more info;
121-
After you created custom provider, for example:
122-
123-
```php
124-
class Book extends \Faker\Provider\Base
125-
{
126-
127-
public function title($nbWords = 5)
128-
{
129-
$sentence = $this->generator->sentence($nbWords);
130-
return mb_substr($sentence, 0, mb_strlen($sentence) - 1);
131-
}
132-
133-
}
134-
```
135-
136-
You can use it by adding it to the `$providers` property of the current command. In your console.php config:
137-
138-
```php
139-
'controllerMap' => [
140-
'fixture' => [
141-
'class' => 'yii\faker\FixtureController',
142-
'providers' => [
143-
'app\tests\unit\faker\providers\Book',
144-
],
145-
],
146-
]
147-
```
45+
# to generate stubs for several apps
46+
php yii stubs console common frontend
47+
```

StubsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace console\controllers;
3+
namespace bazilio\stubsgenerator;
44

55
use yii\console\Controller;
66
use yii\console\Exception;

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@
1212
],
1313
"require": {
1414
"yiisoft/yii2": "*"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"bazilio\\stubsgenerator\\": ""
19+
}
1520
}
1621
}

0 commit comments

Comments
 (0)