Skip to content

Commit

Permalink
prepare for new release
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmunir committed Mar 11, 2016
1 parent 745363f commit 1986249
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
54 changes: 37 additions & 17 deletions Classes/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,45 @@ class Import
* ```
* Import::using('yii\bootstrap\*');
* Import::using('yii\widgets\ActiveForm');
*
* Import::using([
* 'yii\helpers\Html',
* 'yii\bootstrap\Html' => 'BHtml'
* ]);
* ```
* @param string $namespace
* @throws BadMethodCallException
*/
public static function using($namespace)
public static function using($namespace, $as = null)
{
if (!static::$_registered) {
spl_autoload_register(['Import', 'load']);
static::$_registered = true;
}
if (is_array($namespace)) {
foreach ($namespace as $class => $alias) {
if (is_int($class)) {
static::using($alias);
} else {
static::using($class, $alias);
}
}
return;
}

$namespace = trim($namespace, '\\');
if (($pos = strrpos($namespace, '\\')) !== false) {
$ns = trim(substr($namespace, 0, $pos), '\\');
$alias = substr($namespace, $pos + 1);
if ($alias === '*') {
static::$_paths[] = $ns;
static::$_paths = array_unique(static::$_paths);
} else {
static::$_classMap[$alias] = $namespace;
}
} else {
throw new BadMethodCallException("Invalid import alias: $namespace");
$ns = '';
$alias = $namespace;
}

if ($alias === '*') {
static::$_paths[$ns] = true;
} else {
static::$_classMap[$as ? : $alias] = $namespace;
}
}

Expand All @@ -52,16 +69,19 @@ public static function load($class)
if (empty(static::$_paths) && empty(static::$_classMap)) {
return;
}
if (strpos($class, '\\') === false) {
if (isset(static::$_classMap[$class])) {
return class_alias(static::$_classMap[$class], $class);
} else {
foreach (static::$_paths as $path) {
if (class_exists($path . '\\' . $class)) {
return class_alias($path . '\\' . $class, $class);
}
if (($pos = strrpos($class, '\\')) !== false) {
$alias = substr($class, $pos + 1);
} else {
$alias = $class;
}
if (isset(static::$_classMap[$alias])) {
return class_alias(static::$_classMap[$alias], $class);
} else {
foreach (array_keys(static::$_paths) as $path) {
if (class_exists(rtrim($path . '\\' . $alias, '\\'))) {
return class_alias(rtrim($path . '\\' . $alias, '\\'), $class);
}
}
}
}
}
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@ Once the extension is installed, use in your code:
<?php
Import::using('yii\bootstrap\Button'); // import yii\bootstrap\Button
Import::using('yii\widgets\*'); // import all class under namespace yii\widgets
Import::using('yii\bootstrap\Html', 'BootstrapHtml'); // import with alias
Import::using([
'yii\helpers\Html' => 'Html',
'yii\helpers\ArrayHelper',
]);
?>
<?php
echo Button::widget([
'label' => 'Action Test',
'options' => ['class' => 'btn-lg'],
]);

echo BootstrapHtml::icon('star');
?>

<?php Spaceless::begin(); ?>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "1.x-dev"
}
}
}

0 comments on commit 1986249

Please sign in to comment.