Yii 1.1: Directory independent extensions

7 followers

How to implement directory independent extensions ΒΆ

Many people want to use downloaded extensions in their own way. As for me most of the extensions require me to correct Yii::import() instructions inside their source code. All these extensions are directory dependent. To avoid the situation described above, I suggest to implement pseudo-anonymous aliases:

// take the current extension path
$dir = dirname(__FILE__);
// generate alias name
$alias = md5($dir);
// create alias
Yii::setPathOfAlias($alias,$dir);
// import other classes
Yii::import($alias.'.anotherClass');
Yii::import($alias.'.andAnoTherClass');
//...
//if you want, you can destroy an alias created
Yii::setPathOfAlias($alias,'');

Using this technique you can create really directory-independent extensions.

Total 3 comments

#2206 report it
GOsha at 2010/11/25 03:42pm
@Yeti

I think there is no need to modify Yii::import(). You can write simple helper to yii.

#2202 report it
Yeti at 2010/11/24 10:11am
Correct code

Dohhh!!

public static function import($alias,$forceInclude=false,$baseDir='')
{
  if($baseDir)
  {
    $pathAlias=md5($dir);
    Yii::setPathOfAlias($pathAlias,$baseDir);
    $alias="pathAlias.$alias";
  }
 
  // Current Yii::import code
}
#2201 report it
Yeti at 2010/11/24 10:02am
Include in Yii core

Brilliant!!

Perhaps Yii::import could be modified to include this feature

Something like

public static function import($alias,$forceInclude=false,$baseDir='')
{
  if($baseDir)
    $alias=md5($dir).".$alias";
 
  // Current Yii::import code
}

then extensions can import with

Yii::import('Class',false,dirname(__FILE__));

Leave a comment

Please to leave your comment.

Write new article
  • Written by: GOsha
  • Category: Tips
  • Yii Version: 1.1
  • Votes: +15 / -1
  • Viewed: 8,772 times
  • Created on: Nov 19, 2010
  • Last updated: Nov 20, 2010
  • Tags: libraries, extensions, paths