Skip to content

Commit

Permalink
asset-indexing command cleanup (#3595)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Jan 18, 2019
1 parent c674183 commit b1bc585
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 107 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
## Unreleased

### Added
- Added the `index-assets <volume>` and `index-assets/all` console commands. ([#3595](https://github.com/craftcms/cms/pull/3595))
- Added `craft\base\FieldTrait::$oldSettings`.
- Added `craft\helpers\Install`.
- Added `craft\services\Fields::prepFieldForSave()`.
- Added `craft\services\Path::getProjectConfigFilePath()`.
- Added the `craft asset-indexing/all` and `craft asset-indexing/one` console commands which can be used to index asset volumes.

### Changed
- The installer now checks `project.yaml` when determining the default site name, handle, base URL, and language values. ([#3661](https://github.com/craftcms/cms/issues/3661))
Expand Down
106 changes: 0 additions & 106 deletions src/console/controllers/AssetIndexingController.php

This file was deleted.

120 changes: 120 additions & 0 deletions src/console/controllers/IndexAssetsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\console\controllers;

use Craft;

use craft\base\Volume;
use craft\base\VolumeInterface;
use yii\console\ExitCode;
use yii\helpers\Console;
use yii\console\Controller;

/**
* Re-indexes assets in volumes.
*
* @since 3.1.2
*/
class IndexAssetsController extends Controller
{
/**
* @inheritdoc
*/
public $defaultAction = 'one';

/**
* @var bool Whether remote-stored images should be locally cached in the process.
*/
public $cacheRemoteImages = false;

/**
* @inheritdoc
*/
public function options($actionID)
{
$options = parent::options($actionID);
$options[] = 'cacheRemoteImages';
return $options;
}

/**
* Re-indexes assets across all volumes.
*
* @return int
*/
public function actionAll(): int
{
$volumes = Craft::$app->getVolumes()->getAllVolumes();

if (empty($volumes)) {
$this->stdout('No volumes exist.' . PHP_EOL, Console::FG_YELLOW);
return ExitCode::OK;
}

return $this->_indexAssets($volumes);
}

/**
* Re-indexes assets from the given volume handle.
*
* @param string $handle The handle of the volume to index
* @return int
*/
public function actionOne($handle)
{
$volume = Craft::$app->getVolumes()->getVolumeByHandle($handle);

if (!$volume) {
$this->stdout('No volume exists with the handle “' . $handle . '”.', Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}

return $this->_indexAssets([$volume]);
}

/**
* Indexes the assets in the given volumes.
*
* @param VolumeInterface[] $volumes
* @return int
*/
private function _indexAssets(array $volumes): int
{
$assetIndexer = Craft::$app->getAssetIndexer();
$session = $assetIndexer->getIndexingSessionId();

$this->stdout(PHP_EOL);

foreach ($volumes as $volume) {
/** @var Volume $volume */
$this->stdout('Indexing assets in ', Console::FG_YELLOW);
$this->stdout($volume->name, Console::FG_CYAN);
$this->stdout(' ...' . PHP_EOL, Console::FG_YELLOW);
foreach ($assetIndexer->getIndexListOnVolume($volume) as $item) {
$this->stdout(' > ');
$this->stdout($item['path'], Console::FG_CYAN);
$this->stdout(' ... ');
try {
$assetIndexer->indexFile($volume, $item['path'], $session, $this->cacheRemoteImages);
} catch (\Throwable $e) {
$this->stdout('error: ' . $e->getMessage() . PHP_EOL . PHP_EOL, Console::FG_RED);
Craft::$app->getErrorHandler()->logException($e);
return ExitCode::UNSPECIFIED_ERROR;
}

$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
}

$this->stdout('Done indexing assets in ', Console::FG_GREEN);
$this->stdout($volume->name, Console::FG_CYAN);
$this->stdout('.' . PHP_EOL . PHP_EOL, Console::FG_GREEN);
}

return ExitCode::OK;
}
}

0 comments on commit b1bc585

Please sign in to comment.