-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prof: add more str,json... util commands
- Loading branch information
Showing
22 changed files
with
1,243 additions
and
113 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,97 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Inhere\Kite\Console\Component; | ||
|
||
use Inhere\Kite\Kite; | ||
use Toolkit\Cli\Cli; | ||
use Toolkit\FsUtil\File; | ||
use Toolkit\Stdlib\Obj\AbstractObj; | ||
use function is_file; | ||
use function substr; | ||
|
||
/** | ||
* class ContentsTryReader | ||
*/ | ||
class ContentsTryReader extends AbstractObj | ||
{ | ||
public const TYPE_CLIPBOARD = 'clipboard'; | ||
|
||
public const TYPE_FILE = 'file'; | ||
public const TYPE_STDIN = 'stdin'; | ||
public const TYPE_STRING = 'string'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected string $srcType = self::TYPE_STRING; | ||
|
||
/** | ||
* try read contents | ||
* | ||
* - input empty or '@i' or '@stdin' - will read from STDIN | ||
* - input '@c' or '@cb' or '@clipboard' - will read from Clipboard | ||
* - input '@l' or '@load' - will read from loaded file | ||
* - input '@FILEPATH' or FILEPATH - will read from the filepath. | ||
* | ||
* @param string $source the input text | ||
* @param array{print: bool, loadedFile: string} $opts | ||
* | ||
* @return string | ||
*/ | ||
public function read(string $source, array $opts = []): string | ||
{ | ||
$print = $opts['print'] ?? false; | ||
$lFile = $opts['loadedFile'] ?? ''; | ||
|
||
$str = $source; | ||
if (!$source) { | ||
$this->srcType = self::TYPE_STDIN; | ||
$print && Cli::info('try read contents from STDIN'); | ||
$str = Kite::cliApp()->getInput()->readAll(); | ||
|
||
// is one line text | ||
} elseif (!str_contains($source, "\n")) { | ||
if (str_starts_with($source, '@')) { | ||
if ($source === '@c' || $source === '@cb' || $source === '@clipboard') { | ||
$this->srcType = self::TYPE_CLIPBOARD; | ||
$print && Cli::info('try read contents from Clipboard'); | ||
$str = Clipboard::new()->read(); | ||
} elseif ($source === '@i' || $source === '@stdin') { | ||
$this->srcType = self::TYPE_STDIN; | ||
$print && Cli::info('try read contents from STDIN'); | ||
$str = Kite::cliApp()->getInput()->readAll(); | ||
// $str = File::streamReadAll(STDIN); | ||
// $str = File::readAll('php://stdin'); | ||
// vdump($str); | ||
// Cli::info('try read contents from STDOUT'); // error | ||
// $str = Kite::cliApp()->getOutput()->readAll(); | ||
} elseif (($source === '@l' || $source === '@load') && ($lFile && is_file($lFile))) { | ||
$this->srcType = self::TYPE_FILE; | ||
$print && Cli::info('try read contents from file: ' . $lFile); | ||
$str = File::readAll($lFile); | ||
} else { | ||
$filepath = substr($source, 1); | ||
if (is_file($filepath)) { | ||
$this->srcType = self::TYPE_FILE; | ||
$print && Cli::info('try read contents from file: ' . $filepath); | ||
$str = File::readAll($filepath); | ||
} | ||
} | ||
} elseif (is_file($source)) { | ||
$this->srcType = self::TYPE_FILE; | ||
$print && Cli::info('try read contents from file: ' . $source); | ||
$str = File::readAll($source); | ||
} | ||
} | ||
|
||
return $str; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getSrcType(): string | ||
{ | ||
return $this->srcType; | ||
} | ||
} |
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
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
Oops, something went wrong.