ComposerとはPHPのパッケージ管理システム。
ライブラリをzip等でダウンロードせずに利用できる。
$ php -v
PHP 7.4.11 (cli) (built: Oct 21 2020 19:12:26) ( NTS )
$ composer
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.0.4 2020-10-30 22:39:11
composer.json
{ "require": { "ircmaxell/random-lib": "1.0.0" } }
$ composer install
require_once "vendor/autoload.php"; $factory = new RandomLib\Factory; $generator = $factory->getMediumStrengthGenerator(); $randomInt = $generator->generateInt(5, 15); echo $randomInt; echo "\n"; $randomString = $generator->generateString(32, 'abcdef'); echo $randomString;
$ php app.php
12
cffccdccbcaadcffbbddbffbdadbfefd
requrire_onceした後にclassを作って読み込めば良いのね
/vendor/ircmaxell/random-lib/lib/RandomLib/Factory.php
public function getGenerator(\SecurityLib\Strength $strength) { $sources = $this->getSources(); $newSources = array(); foreach ($sources as $source) { if ($strength->compare($source::getStrength()) <= 0) { $newSources[] = new $source; } } $mixer = $this->findMixer($strength); return new Generator($newSources, $mixer); } // 省略 public function getMediumStrengthGenerator() { return $this->getGenerator(new Strength(Strength::MEDIUM)); }
generateIntのclassも同様に、RandomLib/Generator.phpで書かれていますね。
なるほど。