Skip to content

Commit

Permalink
feature: user identity stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
bazilio91 committed Apr 2, 2017
1 parent 3bf31cf commit 53e499d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Yii::$app stubs generator for Yii 2
[![Latest Unstable Version](https://poser.pugx.org/bazilio/yii2-stubs-generator/v/unstable)](https://packagist.org/packages/bazilio/yii2-stubs-generator)
[![License](https://poser.pugx.org/bazilio/yii2-stubs-generator/license)](https://packagist.org/packages/bazilio/yii2-stubs-generator)

This extension provides no-more-butthurt components autocomplete generator command for Yii 2.
This extension provides no-more-butthurt components & user identity autocomplete generator command for Yii 2.

![in action](https://monosnap.com/file/oHUjBSw7oIJHYAEpQKs4mVVJLfMLrM.png)

Expand Down
37 changes: 35 additions & 2 deletions StubsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,26 @@ class ConsoleApplication extends yii\console\Application
TPL;
}

protected function getUserTemplate()
{
return <<<TPL
/**
* @property {user_identities} \$identity
*/
class User extends \yii\web\User {
}
TPL;
}


public function actionIndex()
{
$path = $this->outputFile ? $this->outputFile : \Yii::$app->getVendorPath() . DIRECTORY_SEPARATOR . 'Yii.php';
$path = $this->outputFile ? $this->outputFile :
\Yii::$app->getVendorPath() . DIRECTORY_SEPARATOR . 'Yii.php';

$components = [];
$userIdentities = [];

foreach (\Yii::$app->requestedParams as $configPath) {
if (!file_exists($configPath)) {
Expand All @@ -64,20 +79,38 @@ public function actionIndex()
continue;
}

if ($name === 'user' && isset($component['identityClass'])) {
$userIdentities[] = $component['identityClass'];
}

$components[$name][] = $component['class'];
}
}

$stubs = '';
$userStubs = '';

if (sizeof($userIdentities)) {
$components['user'][] = 'User';

$userIdentities = implode('|', array_unique($userIdentities));
$userStubs = str_replace(
'{user_identities}',
$userIdentities,
$this->getUserTemplate()
);
}

foreach ($components as $name => $classes) {
$classes = implode('|', array_unique($classes));
$stubs .= "\n * @property {$classes} \$$name";
}

$content = str_replace('{stubs}', $stubs, $this->getTemplate());
$content = str_replace('{time}', date(DATE_ISO8601), $content);
$content .= $userStubs;

if($content!=@file_get_contents($path)) {
if ($content != @file_get_contents($path)) {
file_put_contents($path, $content);
}
}
Expand Down

0 comments on commit 53e499d

Please sign in to comment.