Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Indent Laravel Package

cebe/indent

cebe/indent is a small PHP utility to indent or outdent multi-line strings. Useful for formatting generated code, logs, and console output; it normalizes leading whitespace and helps you apply consistent indentation levels with simple helpers.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing via Composer: composer require cebe/indent. The library is tiny — just one main class, cebe\indent\Indent. The first use case is aligning multi-line strings in CLI output or generated code. For example, to indent a help block by two spaces:

use cebe\indent\Indent;

$output = Indent::indent("Line 1\nLine 2\nLine 3", 2);
// Returns: "  Line 1\n  Line 2\n  Line 3"

For generated code blocks inside templates, you can also auto-detect and strip common leading whitespace:

$code = Indent::dedent("    echo 'foo';\n    return true;");
// Returns: "echo 'foo';\nreturn true;"

Check the README or source (src/) for minimal, well-documented code — it’s just a few hundred lines.

Implementation Patterns

  • CLI Formatting: Use indent() to nicely align help or usage messages in console commands. Combine with str_repeat(' ', $depth) for dynamic nesting levels.

  • Code Generation: In scaffolders or code builders, dedent() lets you write clean multi-line strings in templates without worrying about indentation in source code. Wrap your template strings in dedent() before appending to output.

  • Nested Structures: For hierarchical output (e.g., generating JSON/YAML-like structures), chain operations:

    $block = Indent::indent(
        Indent::dedent("key:\n  value1\n  value2"), 
        4
    );
    // "    key:\n      value1\n      value2"
    
  • Configurable Indentation: Use indent() with a second argument of ' ' or "\t" instead of an integer to use custom prefixes (e.g., for editor-config consistency).

  • Integrating with Livewire/Blade: When outputting multi-line text from Blade ({{ Indent::indent(...) }}), ensure you’re not double-escaping newlines — use implode("\n", $lines) when building arrays.

Gotchas and Tips

  • No PSR compliance: This package has noPSR-12/PSR-2 alignment features — it’s purely about whitespace manipulation, not code style.
  • Line ending handling: Works with \n, \r\n, etc., but does not normalize endings — preserve consistency in your input strings.
  • Trailing newline matters: dedent() removes common leading whitespace per line, so a trailing newline that’s indented separately (e.g., " foo\n \nbar") can behave unexpectedly — trim inputs or use trim() if needed.
  • No whitespace-only lines: dedent() treats lines consisting of only whitespace as contributing to the minimum indentation — they’ll be reduced to nothing. For logs where blank lines must remain, sanitize before dedent().
  • Extensibility: Since it’s just static methods on a single class, subclassing isn’t supported — but you can easily wrap it in your own Indenter service for DI or project-specific defaults (e.g., always indent by 4 spaces).
  • Stale but stable: Last release in 2019; extremely low risk (no active maintenance needed), but assume bug fixes won’t happen — good for embedding locally if you need more control.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation