Skip to content

Commit

Permalink
Merge pull request #29 from bvanleeuwen1995/master
Browse files Browse the repository at this point in the history
Added possibility to change the DB component that is used by the image manager
  • Loading branch information
noam148 authored Jun 11, 2017
2 parents a1ceb0c + 88c0c80 commit 4f4568e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ yii migrate --migrationPath=@noam148/imagemanager/migrations
'useFilename' => true,
//show full url (for example in case of a API)
'absoluteUrl' => false,
'databaseComponent' => 'db' // The used database component by the image manager, this defaults to the Yii::$app->db component
],
],
```
Expand Down
42 changes: 40 additions & 2 deletions components/ImageManagerGetPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace noam148\imagemanager\components;

use Yii;
use yii\base\Component;
use noam148\imagemanager\models\ImageManager;
use yii\base\InvalidConfigException;
use yii\db\Connection;

class ImageManagerGetPath extends Component {

Expand All @@ -27,10 +30,16 @@ class ImageManagerGetPath extends Component {
*/
public $absoluteUrl = false;

/*
/**
* @var string The DB component name that the image model uses
* This defaults to the default Yii DB component: Yii::$app->db
* If this component is not set, the model will default to DB
*/
public $databaseComponent = 'db';

/**
* Init set config
*/

public function init() {
parent::init();
// initialize the compontent with the configuration loaded from config.php
Expand All @@ -40,6 +49,14 @@ public function init() {
'useFilename' => $this->useFilename,
'absoluteUrl' => $this->absoluteUrl,
]);

if (is_callable($this->databaseComponent)) {
// The database component is callable, run the user function
$this->databaseComponent = call_user_func($this->databaseComponent);
}

// Check if the user input is correct
$this->_checkVariables();
}

/**
Expand Down Expand Up @@ -78,4 +95,25 @@ public function getImagePath($ImageManager_id, $width = 400, $height = 400, $thu
return $return;
}

/**
* Check if the user configurable variables match the criteria
* @throws InvalidConfigException
*/
private function _checkVariables() {
// Check to make sure that the $databaseComponent is a string
if (! is_string($this->databaseComponent)) {
throw new InvalidConfigException("Image Manager Component - Init: Database component '$this->databaseComponent' is not a string");
}

// Check to make sure that the $databaseComponent object exists
if (Yii::$app->get($this->databaseComponent, false) === null) {
throw new InvalidConfigException("Image Manager Component - Init: Database component '$this->databaseComponent' does not exists in application configuration");
}

// Check to make sure that the $databaseComponent is a yii\db\Connection object
if (($databaseComponentClassName = get_class(Yii::$app->get($this->databaseComponent))) !== ($connectionClassName = Connection::className())) {
throw new InvalidConfigException("Image Manager Component - Init: Database component '$this->databaseComponent' is not of type '$connectionClassName' instead it is '$databaseComponentClassName'");
}
}

}
24 changes: 23 additions & 1 deletion models/ImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ public static function tableName() {
return 'ImageManager';
}

/**
* Get the DB component that the model uses
* This function will throw error if object could not be found
* The DB connection defaults to DB
* @return null|object
*/
public static function getDb() {
// Get the image manager object
$oImageManager = Yii::$app->get('imagemanager', false);

if($oImageManager === null) {
// The image manager object has not been set
// The normal DB object will be returned, error will be thrown if not found
return Yii::$app->get('db');
}

// The image manager component has been loaded, the DB component that has been entered will be loaded
// By default this is the Yii::$app->db connection, the user can specify any other connection if needed
return Yii::$app->get($oImageManager->databaseComponent);
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -92,8 +113,9 @@ public function afterDelete()
parent::afterDelete();

// Check if file exists
if (file_exists($this->getImagePathPrivate()))
if (file_exists($this->getImagePathPrivate())) {
unlink($this->getImagePathPrivate());
}
}

/**
Expand Down

0 comments on commit 4f4568e

Please sign in to comment.