Skip to content

Commit d1130a1

Browse files
committed
Added packages table. Added duplication prevention.
1 parent eb0e60c commit d1130a1

12 files changed

+146
-45
lines changed

src/Bootstrap.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace hiqdev\assetpackagist;
4+
5+
use hiqdev\assetpackagist\repositories\PackageRepository;
6+
use Yii;
7+
use yii\base\Application;
8+
use yii\base\BootstrapInterface;
9+
10+
class Bootstrap implements BootstrapInterface
11+
{
12+
/**
13+
* Bootstrap method to be called during application bootstrap stage.
14+
* @param Application $app the application currently running
15+
*/
16+
public function bootstrap($app)
17+
{
18+
Yii::$container->set('db', function () { return Yii::$app->get('db'); });
19+
Yii::$container->set(PackageRepository::class, PackageRepository::class, [\yii\di\Instance::of('db')]);
20+
}
21+
}

src/commands/AbstractPackageCommand.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace hiqdev\assetpackagist\commands;
44

55
use hiqdev\assetpackagist\models\AssetPackage;
6+
use hiqdev\assetpackagist\repositories\PackageRepository;
7+
use Yii;
68
use yii\base\Component;
79
use zhuravljov\yii\queue\Job;
810

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

21+
/**
22+
* @var PackageRepository
23+
*/
24+
protected $packageRepository;
25+
1926
/**
2027
* Triggers event before run
2128
*/
@@ -35,13 +42,15 @@ public function afterRun()
3542
/**
3643
* CollectDependenciesCommand constructor.
3744
* @param AssetPackage $package
45+
* @param PackageRepository $packageRepository
3846
* @param array $config
3947
*/
40-
public function __construct(AssetPackage $package, $config = [])
48+
public function __construct(AssetPackage $package, PackageRepository $packageRepository, $config = [])
4149
{
4250
parent::__construct($config);
4351

4452
$this->package = $package;
53+
$this->packageRepository = $packageRepository;
4554
}
4655

4756
/**
@@ -52,6 +61,7 @@ public function __construct(AssetPackage $package, $config = [])
5261
public function __wakeup()
5362
{
5463
$this->package->load();
64+
$this->packageRepository = Yii::createObject(PackageRepository::class);
5565
}
5666

5767
/**

src/commands/CollectDependenciesCommand.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ public function run()
3232
}
3333

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

38-
Yii::trace(Console::renderColoredString('Created update command for %y' . count($requires) . "%n packages\n"), __CLASS__);
37+
if ($this->packageRepository->exists($assetPackage)) {
38+
Yii::trace(Console::renderColoredString('Package %N' . $assetPackage->getFullName() . "%n already exists. Skipping.\n"), __CLASS__);
39+
continue;
40+
}
41+
42+
Yii::$app->queue->push('package', Yii::createObject(PackageUpdateCommand::class, [$assetPackage]));
43+
Yii::trace(Console::renderColoredString('Created update command for %Y' . $assetPackage->getFullName() . "%n package\n"), __CLASS__);
44+
}
3945

4046
$this->afterRun();
4147
}

src/commands/PackageUpdateCommand.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ public function run()
1616
{
1717
$this->beforeRun();
1818

19-
if ($this->package->canBeUpdated()) {
19+
if (!$this->package->canBeUpdated()) {
20+
if (!$this->packageRepository->exists($this->package)) {
21+
$this->packageRepository->insert($this->package);
22+
}
23+
} else {
2024
$this->package->update();
25+
$this->packageRepository->save($this->packages);
2126
}
2227

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

2530
$this->afterRun();
2631
}

src/config/bootstrap.php

-29
This file was deleted.

src/config/hidev.php

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
return [
13+
'bootstrap' => ['log', \hiqdev\assetpackagist\Bootstrap::class],
1314
'components' => [
1415
'config' => [
1516
'include' => [

src/config/hisite.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
'id' => 'asset-packagist',
1414
'name' => 'Asset Packagist',
1515
'controllerNamespace' => 'hiqdev\assetpackagist\controllers',
16-
'bootstrap' => ['log'],
16+
'bootstrap' => ['log', \hiqdev\assetpackagist\Bootstrap::class],
1717
'components' => [
1818
'packageStorage' => [
1919
'class' => \hiqdev\assetpackagist\components\Storage::class,

src/console/AssetPackageController.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
namespace hiqdev\assetpackagist\console;
1313

14-
use hiqdev\assetpackagist\commands\CollectDependenciesCommand;
14+
use hiqdev\assetpackagist\commands\PackageUpdateCommand;
1515
use hiqdev\assetpackagist\models\AssetPackage;
16+
use hiqdev\assetpackagist\repositories\PackageRepository;
1617
use Yii;
1718
use yii\helpers\Console;
1819

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

33-
Yii::$app->queue->push('package', new CollectDependenciesCommand($package));
34-
3534
return true;
3635
} catch (\Exception $e) {
3736
echo Console::renderColoredString("%Rfailed%N $type/$name:%n {$e->getMessage()}\n");

src/console/QueueController.php

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace hiqdev\assetpackagist\console;
44

55
use hiqdev\assetpackagist\commands\AbstractPackageCommand;
6+
use hiqdev\assetpackagist\repositories\PackageRepository;
67
use Yii;
78
use yii\base\Event;
89
use yii\console\Controller;

src/controllers/SiteController.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
namespace hiqdev\assetpackagist\controllers;
1313

1414
use Exception;
15-
use hiqdev\assetpackagist\commands\CollectDependenciesCommand;
16-
use hiqdev\assetpackagist\commands\DependenciesUpdateCommand;
1715
use hiqdev\assetpackagist\commands\PackageUpdateCommand;
1816
use hiqdev\assetpackagist\models\AssetPackage;
17+
use hiqdev\assetpackagist\repositories\PackageRepository;
1918
use Yii;
2019
use yii\filters\VerbFilter;
2120

@@ -98,9 +97,7 @@ public function actionUpdate()
9897

9998
$package = $this->getAssetPackage($query);
10099
if ($package->canBeUpdated()) {
101-
$package->update();
102-
103-
Yii::$app->queue->push('package', new CollectDependenciesCommand($package));
100+
Yii::createObject(PackageUpdateCommand::class, [$package])->run(); // TODO: think of command bus
104101
} else {
105102
Yii::$app->session->addFlash('update-impossible', true);
106103
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace hiqdev\assetpackagist\migrations;
4+
5+
use yii\db\Migration;
6+
7+
/**
8+
* Migration for queue message storage
9+
*
10+
* @author Dmytro Naumenko <d.naumenko.a@gmail.com>
11+
*/
12+
class m161130_163000_package extends Migration
13+
{
14+
public $tableName = '{{%package}}';
15+
public $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
16+
17+
public function up()
18+
{
19+
$this->createTable($this->tableName, [
20+
'type' => $this->string()->notNull(),
21+
'name' => $this->string()->notNull(),
22+
'last_update' => $this->integer(),
23+
'PRIMARY KEY (type, name)'
24+
], $this->tableOptions);
25+
}
26+
27+
public function down()
28+
{
29+
$this->dropTable($this->tableName);
30+
}
31+
}
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace hiqdev\assetpackagist\repositories;
4+
5+
use hiqdev\assetpackagist\models\AssetPackage;
6+
use yii\db\Connection;
7+
use yii\db\Query;
8+
9+
class PackageRepository
10+
{
11+
/**
12+
* @var Connection
13+
*/
14+
protected $db;
15+
16+
public function __construct(Connection $db)
17+
{
18+
$this->db = $db;
19+
}
20+
21+
public function save(AssetPackage $package)
22+
{
23+
if ($this->exists($package)) {
24+
$this->update($package);
25+
} else {
26+
$this->insert($package);
27+
}
28+
}
29+
30+
public function insert(AssetPackage $package) {
31+
$this->db->createCommand()->insert('package', [
32+
'type' => $package->getType(),
33+
'name' => $package->getName(),
34+
'last_update' => $package->getUpdateTime(),
35+
])->execute();
36+
}
37+
38+
public function update(AssetPackage $package)
39+
{
40+
$this->db->createCommand()->update('package', [
41+
'last_update' => $package->getUpdateTime()
42+
], [
43+
'type' => $package->getType(),
44+
'name' => $package->getName(),
45+
])->execute();
46+
}
47+
48+
/**
49+
* @param AssetPackage $package
50+
* @return bool
51+
*/
52+
public function exists(AssetPackage $package)
53+
{
54+
return (new Query())
55+
->from('package')
56+
->where(['type' => $package->getType(), 'name' => $package->getName()])
57+
->exists($this->db);
58+
}
59+
}

0 commit comments

Comments
 (0)