diff --git a/src/Chunker.php b/src/Chunker.php new file mode 100644 index 0000000..b4afa74 --- /dev/null +++ b/src/Chunker.php @@ -0,0 +1,44 @@ + + */ + public function __invoke($text, $length) + { + $chunks = []; + $buf = null; + + $lines = explode(PHP_EOL, $text); + foreach ($lines as $line) { + if (strlen($line) >= $length) { + foreach (str_split($line, $length) as $chunk) { + $chunks[] = $chunk; + } + + continue; + } + + $line .= PHP_EOL; + + if (strlen($buf . $line) > $length) { + $chunks[] = $buf; + $buf = null; + } + + $buf .= $line; + } + + if ($buf !== null) { + $chunks[] = $buf; + } + + return $chunks; + } +} diff --git a/src/Command/DeployCommand.php b/src/Command/DeployCommand.php index 4b3f145..c521697 100644 --- a/src/Command/DeployCommand.php +++ b/src/Command/DeployCommand.php @@ -2,6 +2,7 @@ namespace Rocket\Command; +use Rocket\Chunker; use Rocket\CommandInterface; use Rocket\Configure; use Rocket\Main; @@ -37,6 +38,8 @@ public function execute() $configPath = realpath($this->options->getConfig()); $configure = new Configure($configPath); + $chunker = new Chunker(); + $self = $this; if (posix_getpwuid(posix_geteuid())['name'] !== $configure->read('user')) { @@ -282,7 +285,7 @@ public function execute() ) ); - $chunks = str_split($gitPullLog, SlackSection::TEXT_MAX_LENGTH - 6); + $chunks = $chunker($gitPullLog, SlackSection::TEXT_MAX_LENGTH - 6); foreach ($chunks as $chunk) { $message ->addBlock( @@ -306,7 +309,7 @@ public function execute() ) ); - $chunks = str_split($syncLog, SlackSection::TEXT_MAX_LENGTH - 6); + $chunks = $chunker($syncLog, SlackSection::TEXT_MAX_LENGTH - 6); foreach ($chunks as $chunk) { $message ->addBlock( diff --git a/src/Command/SlackNotificationCommand.php b/src/Command/SlackNotificationCommand.php index 64283e4..7d0e043 100644 --- a/src/Command/SlackNotificationCommand.php +++ b/src/Command/SlackNotificationCommand.php @@ -2,6 +2,7 @@ namespace Rocket\Command; +use Rocket\Chunker; use Rocket\CommandInterface; use Rocket\Configure; use Rocket\Options; @@ -30,14 +31,16 @@ public function execute() $configPath = realpath($this->options->getConfig()); $configure = new Configure($configPath); - $lines = []; + $content = null; while ($line = fgets(STDIN)) { - $lines[] = trim($line); + $content .= $line; } $message = new SlackMessage(); - $chunks = str_split(implode(PHP_EOL, $lines), SlackSection::TEXT_MAX_LENGTH - 6); + $chunker = new Chunker(); + $chunks = $chunker($content, SlackSection::TEXT_MAX_LENGTH - 6); + foreach ($chunks as $chunk) { $message ->addBlock(