-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added packages table. Added duplication prevention.
- Loading branch information
1 parent
eb0e60c
commit d1130a1
Showing
12 changed files
with
146 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace hiqdev\assetpackagist; | ||
|
||
use hiqdev\assetpackagist\repositories\PackageRepository; | ||
use Yii; | ||
use yii\base\Application; | ||
use yii\base\BootstrapInterface; | ||
|
||
class Bootstrap implements BootstrapInterface | ||
{ | ||
/** | ||
* Bootstrap method to be called during application bootstrap stage. | ||
* @param Application $app the application currently running | ||
*/ | ||
public function bootstrap($app) | ||
{ | ||
Yii::$container->set('db', function () { return Yii::$app->get('db'); }); | ||
Yii::$container->set(PackageRepository::class, PackageRepository::class, [\yii\di\Instance::of('db')]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace hiqdev\assetpackagist\migrations; | ||
|
||
use yii\db\Migration; | ||
|
||
/** | ||
* Migration for queue message storage | ||
* | ||
* @author Dmytro Naumenko <d.naumenko.a@gmail.com> | ||
*/ | ||
class m161130_163000_package extends Migration | ||
{ | ||
public $tableName = '{{%package}}'; | ||
public $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; | ||
|
||
public function up() | ||
{ | ||
$this->createTable($this->tableName, [ | ||
'type' => $this->string()->notNull(), | ||
'name' => $this->string()->notNull(), | ||
'last_update' => $this->integer(), | ||
'PRIMARY KEY (type, name)' | ||
], $this->tableOptions); | ||
} | ||
|
||
public function down() | ||
{ | ||
$this->dropTable($this->tableName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace hiqdev\assetpackagist\repositories; | ||
|
||
use hiqdev\assetpackagist\models\AssetPackage; | ||
use yii\db\Connection; | ||
use yii\db\Query; | ||
|
||
class PackageRepository | ||
{ | ||
/** | ||
* @var Connection | ||
*/ | ||
protected $db; | ||
|
||
public function __construct(Connection $db) | ||
{ | ||
$this->db = $db; | ||
} | ||
|
||
public function save(AssetPackage $package) | ||
{ | ||
if ($this->exists($package)) { | ||
$this->update($package); | ||
} else { | ||
$this->insert($package); | ||
} | ||
} | ||
|
||
public function insert(AssetPackage $package) { | ||
$this->db->createCommand()->insert('package', [ | ||
'type' => $package->getType(), | ||
'name' => $package->getName(), | ||
'last_update' => $package->getUpdateTime(), | ||
])->execute(); | ||
} | ||
|
||
public function update(AssetPackage $package) | ||
{ | ||
$this->db->createCommand()->update('package', [ | ||
'last_update' => $package->getUpdateTime() | ||
], [ | ||
'type' => $package->getType(), | ||
'name' => $package->getName(), | ||
])->execute(); | ||
} | ||
|
||
/** | ||
* @param AssetPackage $package | ||
* @return bool | ||
*/ | ||
public function exists(AssetPackage $package) | ||
{ | ||
return (new Query()) | ||
->from('package') | ||
->where(['type' => $package->getType(), 'name' => $package->getName()]) | ||
->exists($this->db); | ||
} | ||
} |