Skip to content

Commit 4987c9c

Browse files
committed
first commit
0 parents  commit 4987c9c

File tree

5 files changed

+268
-0
lines changed

5 files changed

+268
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Vasily Ostanin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
Faker Extension for Yii 2
2+
=========================
3+
4+
This extension provides a [`Faker`](https://github.com/fzaninotto/Faker) fixture command for Yii 2.
5+
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.
9+
10+
Installation
11+
------------
12+
13+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
14+
15+
Either run
16+
17+
```
18+
php composer.phar require --prefer-dist yiisoft/yii2-faker
19+
```
20+
21+
or add
22+
23+
```json
24+
"yiisoft/yii2-faker": "~2.0.0"
25+
```
26+
27+
to the require section of your composer.json.
28+
29+
30+
Usage
31+
-----
32+
33+
To use this extension, simply add the following code in your application configuration (console.php):
34+
35+
```php
36+
'controllerMap' => [
37+
'fixture' => [
38+
'class' => 'yii\faker\FixtureController',
39+
],
40+
],
41+
```
42+
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
96+
```
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'
105+
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+
```

StubsController.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace console\controllers;
4+
5+
use yii\console\Controller;
6+
use yii\console\Exception;
7+
8+
class StubsController extends Controller
9+
{
10+
public $outputFile = 'Yii.php';
11+
12+
protected function getTemplate()
13+
{
14+
return <<<TPL
15+
<?php
16+
17+
/**
18+
* Yii app stub file. Autogenerated by yii2-stubs-generator (stubs console command).
19+
* Used for enhanced IDE code autocompletion.
20+
* Updated on {time}
21+
*/
22+
class Yii extends \yii\BaseYii
23+
{
24+
/**
25+
* @var BaseApplication|WebApplication|ConsoleApplication the application instance
26+
*/
27+
public static \$app;
28+
}
29+
/**{stubs}
30+
**/
31+
abstract class BaseApplication extends yii\base\Application
32+
{
33+
}
34+
35+
/**{stubs}
36+
**/
37+
class WebApplication extends yii\web\Application
38+
{
39+
}
40+
41+
/**{stubs}
42+
**/
43+
class ConsoleApplication extends yii\console\Application
44+
{
45+
}
46+
TPL;
47+
}
48+
49+
public function actionIndex($app)
50+
{
51+
$path = \Yii::$app->getVendorPath() . DIRECTORY_SEPARATOR . 'Yii.php';
52+
53+
$components = [];
54+
55+
foreach (\Yii::$app->requestedParams as $app) {
56+
$configFile = $app . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'main.php';
57+
if (!file_exists($configFile)) {
58+
throw new Exception('Config file doesn\'t exists: ' . $configFile);
59+
}
60+
61+
$config = include($configFile);
62+
63+
foreach ($config['components'] as $name => $component) {
64+
if (!isset($component['class'])) {
65+
continue;
66+
}
67+
68+
$components[$name][] = $component['class'];
69+
}
70+
}
71+
72+
$stubs = '';
73+
foreach ($components as $name => $classes) {
74+
$classes = implode('|', array_unique($classes));
75+
$stubs .= "\n * @property {$classes} \$$name";
76+
}
77+
78+
$content = str_replace('{stubs}', $stubs, $this->getTemplate());
79+
$content = str_replace('{time}', date(DATE_ISO8601), $content);
80+
81+
file_put_contents($path, $content);
82+
}
83+
}

composer.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "bazilio/yii2-stubs-generator",
3+
"description": "Yii2 component stubs generator for Yii::$app",
4+
"type": "yii2-extension",
5+
"keywords": ["yii2", "stubs", "autocomplete", "phpstorm"],
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Vasily Ostanin",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"yiisoft/yii2": "*"
15+
}
16+
}

0 commit comments

Comments
 (0)