Skip to content

Commit

Permalink
Added packages table. Added duplication prevention.
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire committed Nov 30, 2016
1 parent eb0e60c commit d1130a1
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 45 deletions.
21 changes: 21 additions & 0 deletions src/Bootstrap.php
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')]);
}
}
12 changes: 11 additions & 1 deletion src/commands/AbstractPackageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace hiqdev\assetpackagist\commands;

use hiqdev\assetpackagist\models\AssetPackage;
use hiqdev\assetpackagist\repositories\PackageRepository;
use Yii;
use yii\base\Component;
use zhuravljov\yii\queue\Job;

Expand All @@ -16,6 +18,11 @@ abstract class AbstractPackageCommand extends Component implements Job
*/
protected $package;

/**
* @var PackageRepository
*/
protected $packageRepository;

/**
* Triggers event before run
*/
Expand All @@ -35,13 +42,15 @@ public function afterRun()
/**
* CollectDependenciesCommand constructor.
* @param AssetPackage $package
* @param PackageRepository $packageRepository
* @param array $config
*/
public function __construct(AssetPackage $package, $config = [])
public function __construct(AssetPackage $package, PackageRepository $packageRepository, $config = [])
{
parent::__construct($config);

$this->package = $package;
$this->packageRepository = $packageRepository;
}

/**
Expand All @@ -52,6 +61,7 @@ public function __construct(AssetPackage $package, $config = [])
public function __wakeup()
{
$this->package->load();
$this->packageRepository = Yii::createObject(PackageRepository::class);
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/commands/CollectDependenciesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ public function run()
}

foreach (array_keys($requires) as $name) {
Yii::$app->queue->push('package', new PackageUpdateCommand(AssetPackage::fromFullName($name)));
}
$assetPackage = AssetPackage::fromFullName($name);

Yii::trace(Console::renderColoredString('Created update command for %y' . count($requires) . "%n packages\n"), __CLASS__);
if ($this->packageRepository->exists($assetPackage)) {
Yii::trace(Console::renderColoredString('Package %N' . $assetPackage->getFullName() . "%n already exists. Skipping.\n"), __CLASS__);
continue;
}

Yii::$app->queue->push('package', Yii::createObject(PackageUpdateCommand::class, [$assetPackage]));
Yii::trace(Console::renderColoredString('Created update command for %Y' . $assetPackage->getFullName() . "%n package\n"), __CLASS__);
}

$this->afterRun();
}
Expand Down
9 changes: 7 additions & 2 deletions src/commands/PackageUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ public function run()
{
$this->beforeRun();

if ($this->package->canBeUpdated()) {
if (!$this->package->canBeUpdated()) {
if (!$this->packageRepository->exists($this->package)) {
$this->packageRepository->insert($this->package);
}
} else {
$this->package->update();
$this->packageRepository->save($this->packages);
}

Yii::$app->queue->push('package', new CollectDependenciesCommand($this->package));
Yii::$app->queue->push('package', Yii::createObject(CollectDependenciesCommand::class, [$this->package]));

$this->afterRun();
}
Expand Down
29 changes: 0 additions & 29 deletions src/config/bootstrap.php

This file was deleted.

1 change: 1 addition & 0 deletions src/config/hidev.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

return [
'bootstrap' => ['log', \hiqdev\assetpackagist\Bootstrap::class],
'components' => [
'config' => [
'include' => [
Expand Down
2 changes: 1 addition & 1 deletion src/config/hisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
'id' => 'asset-packagist',
'name' => 'Asset Packagist',
'controllerNamespace' => 'hiqdev\assetpackagist\controllers',
'bootstrap' => ['log'],
'bootstrap' => ['log', \hiqdev\assetpackagist\Bootstrap::class],
'components' => [
'packageStorage' => [
'class' => \hiqdev\assetpackagist\components\Storage::class,
Expand Down
7 changes: 3 additions & 4 deletions src/console/AssetPackageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

namespace hiqdev\assetpackagist\console;

use hiqdev\assetpackagist\commands\CollectDependenciesCommand;
use hiqdev\assetpackagist\commands\PackageUpdateCommand;
use hiqdev\assetpackagist\models\AssetPackage;
use hiqdev\assetpackagist\repositories\PackageRepository;
use Yii;
use yii\helpers\Console;

Expand All @@ -27,11 +28,9 @@ public function actionUpdate($type, $name)
{
try {
$package = new AssetPackage($type, $name);
$package->update();
Yii::createObject(PackageUpdateCommand::class, [$package])->run();
echo 'updated ' . $package->getHash() . ' ' . $package->getFullName() . "\n";

Yii::$app->queue->push('package', new CollectDependenciesCommand($package));

return true;
} catch (\Exception $e) {
echo Console::renderColoredString("%Rfailed%N $type/$name:%n {$e->getMessage()}\n");
Expand Down
1 change: 1 addition & 0 deletions src/console/QueueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace hiqdev\assetpackagist\console;

use hiqdev\assetpackagist\commands\AbstractPackageCommand;
use hiqdev\assetpackagist\repositories\PackageRepository;
use Yii;
use yii\base\Event;
use yii\console\Controller;
Expand Down
7 changes: 2 additions & 5 deletions src/controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
namespace hiqdev\assetpackagist\controllers;

use Exception;
use hiqdev\assetpackagist\commands\CollectDependenciesCommand;
use hiqdev\assetpackagist\commands\DependenciesUpdateCommand;
use hiqdev\assetpackagist\commands\PackageUpdateCommand;
use hiqdev\assetpackagist\models\AssetPackage;
use hiqdev\assetpackagist\repositories\PackageRepository;
use Yii;
use yii\filters\VerbFilter;

Expand Down Expand Up @@ -98,9 +97,7 @@ public function actionUpdate()

$package = $this->getAssetPackage($query);
if ($package->canBeUpdated()) {
$package->update();

Yii::$app->queue->push('package', new CollectDependenciesCommand($package));
Yii::createObject(PackageUpdateCommand::class, [$package])->run(); // TODO: think of command bus
} else {
Yii::$app->session->addFlash('update-impossible', true);
}
Expand Down
31 changes: 31 additions & 0 deletions src/migrations/m161130_163000_package.php
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);
}
}
59 changes: 59 additions & 0 deletions src/repositories/PackageRepository.php
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);
}
}

0 comments on commit d1130a1

Please sign in to comment.