IPERAMUNA.COM
Article February 24, 2026

Composer Package Switcher (composer-switch.sh)

A utility script to manage local package development in a Laravel/Composer project. This script allows you to quickly toggle between using local source code (for development) and remote/tagged releases (for publishing/testing).

Features

  • Automatic Package Discovery: Scans the packages/ directory for any composer packages staged as packages/{author}/{package}.
  • Development Mode:
    • Switches selected packages to use a path repository.
    • Updates composer.json to point to the local directory.
    • Requirements are set to @dev to allow immediate symlinking.
  • Publish Mode:
    • Unsets local path repositories.
    • Automatically detects the latest Git tag in each package's directory.
    • Switches requirements to the latest version (e.g., ^1.0) if a tag is found, otherwise defaults back to @dev for remote fetching.
  • Batch Processing: Allows switching all packages at once or selecting specific ones.

Requirements

  • Environment: Bash (macOS/Linux).
  • Composer: Must be installed and available in your $PATH.
  • Git: Required for automatic version detection in "Publish" mode.
  • Project Structure: Packages must be located in packages/{author}/{package_name}.

Usage

  1. Make the script executable (if not already):

    chmod +x composer-switch.sh
    
  2. Run the script:

    ./composer-switch.sh
    
  3. Choose an Action:

    • 1) Development: Switch packages to their local packages/ source.
    • 2) Publish: Switch packages back to standard remote/tagged versions.
  4. Select Packages (for Development mode):

    • Enter 0 to switch all discovered packages.
    • Enter specific numbers (e.g., 1 3) to switch only those packages.

How it Works

Development Mode

When you select a package for development, the script runs:

composer config repositories.<package-name> path packages/<author>/<package>
composer require <author>/<package>:@dev

This creates a symlink from vendor/<author>/<package> to your local packages/ folder, allowing you to edit package code and see changes instantly without running composer update.

Publish Mode

When switching to publish mode, the script:

  1. Removes the path repository configuration for all discovered packages.
  2. Fetches tags from the local Git repositories of each package.
  3. Identifies the latest version tag (e.g., v1.2.3).
  4. Runs composer require with the appropriate version constraint (e.g., ^1.2).

This ensures your composer.json and composer.lock are ready for production or sharing with other developers who don't have the local packages/ folder.

Shell Scripts

composer-switch.sh