sweetchuck / robo-hash
Create and validate checksums
Installs: 773
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 1
Type:robo-tasks
Requires
- php: >=8.2
- ext-hash: *
- consolidation/robo: ^4.0
Requires (Dev)
- ext-json: *
- codeception/codeception: ^5.0
- codeception/module-asserts: ^3.0
- nuvoleweb/robo-config: ^3.0
- squizlabs/php_codesniffer: ^3.5
- sweetchuck/codeception-module-robo-task-runner: 3.x-dev
- sweetchuck/git-hooks: ^2.x-dev
- sweetchuck/robo-git: 3.x-dev
- sweetchuck/robo-phpcs: 3.x-dev
- sweetchuck/robo-phpmd: 3.x-dev
- symfony/error-handler: ^5.0 || ^6.0
README
Wrapper for PHP Hash
<?php declare(strict_types = 1); use Robo\Collection\CollectionBuilder; use Robo\State\Data as RoboStateData; use Robo\Tasks; use Sweetchuck\Robo\Hash\HashTaskLoader; class RoboFile extends Tasks { use HashTaskLoader; /** * @command hash:file-name */ public function hashFileName( string $fileName, array $options = [ 'hash-algorithm' => 'sha256', 'hash-flags' => '0', 'hash-key' => '', 'chunk-size' => '-1', 'asset-name-prefix' => '', ] ): CollectionBuilder { $hashOptions = $this->commandOptionsToHashOptions($options); $hashOptions['fileName'] = $fileName; return $this ->collectionBuilder() ->addTask($this->taskHash($hashOptions)) ->addCode(function (RoboStateData $data) use ($hashOptions): int { $key = $hashOptions['assetNamePrefix'] . 'hash'; $this->output()->writeln("{$data[$key]} {$hashOptions['fileName']}"); return 0; }); } /** * @command hash:file-handler */ public function hashFileHandler( string $fileContent, array $options = [ 'hash-algorithm' => 'sha256', 'hash-flags' => '0', 'hash-key' => '', 'chunk-size' => '-1', 'file-name' => '/dummy/foo.txt', 'asset-name-prefix' => '', ] ): CollectionBuilder { $fileNameReal = 'data://text/plain;base64,' . base64_encode($fileContent); $fileNameDummy = $options['file-name']; $hashOptions = $this->commandOptionsToHashOptions($options); $hashOptions['fileName'] = $fileNameDummy; $hashOptions['fileHandler'] = fopen($fileNameReal, 'r'); return $this ->collectionBuilder() ->addTask($this->taskHash($hashOptions)) ->addCode(function (RoboStateData $data) use ($hashOptions): int { $key = $hashOptions['assetNamePrefix'] . 'hash'; $this->output()->writeln("{$data[$key]} {$hashOptions['fileName']}"); return 0; }); } protected function commandOptionsToHashOptions(array $options): array { return [ 'hashAlgorithm' => $options['hash-algorithm'] ?? 'sha256', 'hashFlags' => (int) ($options['hash-flags'] ?? 0), 'hashKey' => $options['hash-key'] ?: null, 'chunkSize' => (int) ($options['chunk-size'] ?? 0), 'assetNamePrefix' => $options['asset-name-prefix'] ?? '', ]; } }