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

Spyc Laravel Package

mustangostang/spyc

Spyc is a pure-PHP YAML 1.0 loader/dumper. Parse YAML documents into PHP arrays, or serialize arrays back to YAML for config files, logs, and data exchange. Simple class-based or functional API for loading files and strings.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install via Composer: composer require mustangostang/spyc
  2. Load YAML from a file or string using Spyc::YAMLLoad() or Spyc::YAMLLoadFile()
  3. Dump PHP arrays to YAML using Spyc::YAMLDump()

First use case: Load a simple config.yaml into your app:

$config = Spyc::YAMLLoadFile('config.yaml');
echo $config['app']['name'];

Implementation Patterns

  • Configuration Loading: Use YAML files for app/server config (e.g., config.local.yaml)—especially useful in CLI tools or microservices.
  • Dynamic Config Generation: In build/deployment scripts, generate YAML config from PHP arrays using YAMLDump().
  • Partial Parsing: Parse inline YAML strings (e.g., from database or environment variables) for flexible runtime configuration.
  • Versioned Configs: Use YAMLLoad() to read multiple YAML versions (2.0-style keys, basic inline comments, simple anchors) without needing symfony/yaml.

New in v0.6.3: Control how empty hashes ({}) are parsed:

$config = Spyc::YAMLLoadFile('config.yaml', [
    'setting_empty_hash_as_object' => true,
]);
// Now {} loads as stdClass instead of [] (empty array)

Integration Tip: Wrap Spyc in a ConfigLoader class for centralized parsing and fallback handling (e.g., try config.yaml, then config.yml, then defaults).

Gotchas and Tips

  • No YAML 1.2 support: Avoid complex features like multi-document streams (--- + ...), inline anchors/aliases, or numeric keys with trailing spaces—they may misparse.
  • Scalar quoting: Prefer unquoted scalars; quoted strings ("123", '0') parse as strings, but unquoted 123 becomes integer. Beware of 007 → integer 7.
  • Whitespace sensitivity: Indentation matters. Mixing tabs/spaces may cause parsing errors. Enforce consistent indentation (2-space recommended).
  • Last commit in 2019: Not actively maintained. For new projects, prefer symfony/yaml unless dependency size is critical. For legacy, Spyc is stable for basic configs.
  • Null/False handling: null, ~, NULL, false, FALSE are supported—but Off/On are not boolean aliases (unlike some YAML parsers).
  • Empty hash parsing: By default, {} loads as an empty array ([]). Set 'setting_empty_hash_as_object' => true in options to get new \stdClass() instead—useful for objects expecting JSON compatibility (e.g., APIs returning YAML from JavaScript sources).
  • Custom keys: Avoid special keys like &anchor or *ref unless you’re certain of compatibility—Spike fails silently with non-matching anchors.
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
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
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