-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3595 from OriginalFunko/feature/cli-asset-indexing
Add asset indexing to Craft cli.
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 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
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,106 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\console\controllers; | ||
|
||
use Craft; | ||
|
||
use yii\helpers\Console; | ||
use yii\console\Controller; | ||
|
||
/** | ||
* Manages Craft asset indexing. | ||
* This command indexes assets on all volumes or specified volumes. | ||
* ~~~ | ||
* # Indexes all assets on all volumes. Optional argument is whether or not to cache images. | ||
* craft asset-indexing/all | ||
* # Indexes all assets on a specified volume (EXAMPLE_HANDLE). | ||
* Second (optional) argument is whether or not to cache images. | ||
* craft asset-indexing/one EXAMPLE_HANDLE | ||
* ~~~ | ||
* | ||
* @since 3.1 | ||
*/ | ||
class AssetIndexingController extends Controller | ||
{ | ||
/** | ||
* Indexes all assets on all volumes. | ||
* | ||
* ``` | ||
* craft asset-indexing/all | ||
* // Index all assets without caching images: | ||
* craft asset-indexing/all 0 | ||
* ``` | ||
* | ||
* @param boolean $cacheImages Whether or not to cache images. | ||
* @return boolean. | ||
* @throws \Throwable $e. | ||
*/ | ||
public function actionAll($cacheImages = 1) | ||
{ | ||
try { | ||
$session = Craft::$app->getAssetIndexer()->getIndexingSessionId(); | ||
if ($volumes = Craft::$app->getVolumes()->getAllVolumes()) { | ||
foreach ($volumes as $volume) { | ||
foreach (Craft::$app->getAssetIndexer()->getIndexListOnVolume($volume) as $item) { | ||
Craft::$app->getAssetIndexer()->indexFile( | ||
$volume, | ||
$item['path'], | ||
$session, | ||
$cacheImages | ||
); | ||
} | ||
} | ||
} else { | ||
return false; | ||
} | ||
return true; | ||
} catch (\Throwable $e) { | ||
echo $e->getMessage(); | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Indexes all assets on one specific volume. | ||
* | ||
* ``` | ||
* craft asset-indexing/one EXAMPLE_HANDLE | ||
* // Index volume without caching images: | ||
* craft asset-indexing/one EXAMPLE_HANDLE 0 | ||
* ``` | ||
* | ||
* @param string $name The name of the volume to index. This should only contain | ||
* letters, digits, and underscores. | ||
* @param boolean $cacheImages Whether or not to cache images. | ||
* @return boolean. | ||
* @throws \Throwable $e. | ||
*/ | ||
|
||
public function actionOne($name, $cacheImages = 1) | ||
{ | ||
try { | ||
$session = Craft::$app->getAssetIndexer()->getIndexingSessionId(); | ||
if ($volume = Craft::$app->getVolumes()->getVolumeByHandle($name)) { | ||
foreach (Craft::$app->getAssetIndexer()->getIndexListOnVolume($volume) as $item) { | ||
Craft::$app->getAssetIndexer()->indexFile( | ||
$volume, | ||
$item['path'], | ||
$session, | ||
$cacheImages | ||
); | ||
} | ||
} else { | ||
return false; | ||
} | ||
return true; | ||
} catch (\Throwable $e) { | ||
echo $e->getMessage(); | ||
return false; | ||
} | ||
} | ||
} |