Composer Plugin for automatically including files for easing function usage in php.

Overview

Php Inc

PHP Requirements

Php inc is a composer plugin for automatically including certain files into composer's autoload and autoload-dev files config. Given a set of file matchers, on the the dump-autoload event, php-inc will automatically include any matched files into the dumped autoloaded files.

This ameliorates the issues that come about when you want to include certain files that contain functions or maybe multiple classes but don't want to constantly update the composer autoload files configuration which can get hard to deal with when you start including more files.

Installation

Install via composer at krak/php-inc

Usage

With the default configuration, simply name any file in your src or tests directory starting with a lower case letter with a .php extension will automatically be included in the dumped autoload files when composer's dump-autoload event is triggered. This happens on install, update, or dump-autoload commands.

The composer plugin is automatically loaded after it is installed, so in most scenarios, you shouldn't have to do anything for the files to be automatically included. However, if you add files and want to include them right away during development, you can run composer dump-autoload to make sure they are included.

Configuration

Here's an example of the default configuration that is applied:

{
  "extra": {
    "php-inc": {
      "src-path": "src",
      "test-path": "tests",
      "matches": {
        "type": "and",
        "matches": [
          {"type":  "ext", "exts":  ["php"]},
          {"type":  "lowerCase"},
          {"type":  "excludePath", "path":  "@.*/(Resources|Tests)/.*@"}
        ]
      },
      "matches-dev-src": {
        "type": "and",
        "matches": [
          {"type":  "ext", "exts":  ["php"]},
          {"type":  "lowerCase"},
          {"type":  "includePath", "path":  "@.*/Tests/.*@"},
          {"type":  "excludePath", "path":  "@.*/Tests/.*/Fixtures/.*@"}
        ]
      },
      "matches-dev-test": {
        "type": "and",
        "matches": [
          {"type":  "ext", "exts":  ["php"]},
          {"type":  "lowerCase"},
          {"type":  "excludePath", "path":  "@.*/Fixtures/.*@"}
        ]
      }
    }
  }
}

Let's go through and explain what each part means and refers to.

src-path

src-path will determine the path to your source code where any autoload files will be searched in.

If you are working with the standard Laravel file structure, you'll want to change the src-path to app instead of src.

test-path

test-path will determine the path to your test code where the autoload-dev files will be searched.

matches

matches can be any hierarchy of configured matches to determine how you want the src folder to be searched for files to be included in autoload.files. The default configuration ensures that all files that start with a lower case file name, have a php extension, and are not inside of a Resources or Tests directory will be included in the autoload.files composer configuration.

matches-dev-src

matches-dev-src can be any hierarchy of configured matches to determine how you want the src folder to be searched for files to be included in autoload-dev.files. The default configuration ensures that all files that start with a lower case file name, have a php extension, are inside of a Tests directory, and not apart of a Fixtures directory will be included in the autoload-dev.files composer configuration.

matches-dev-test

matches-dev-test can be any hierarchy of configured matches to determine how you want the test folder to be searched for files to be included in autoload-dev.files. The default configuration ensures that all files that start with a lower case file name, have a php extension, and are not apart of a Fixtures directory will be included in the autoload-dev.files composer configuration.

Debugging

If you are ever curious what files are being included, you can simply run composer dump-autoload -v and view the php-inc output to see which files are being merged with which composer files definition.

Managing Dependencies

With extended use, you may come into a situation where one file included needs to be loaded before another. If this comes up, the best solution I've found for now is to prefix those files with an _ and just create a new file named inc.php which loads them in the correct order.

For example:

src/a.php
src/b.php

a.php depends on b.php loading first. To enforce loading order, we'd make the following change:

src/_a.php
src/_b.php
src/inc.php

Where inc.php is as follows:

<?php

require_once __DIR__ . '/_b.php';
require_once __DIR__ . '/_a.php';

When you run composer dump-autoload, only inc.php will be included and will make sure to include those files correctly.

Why is this useful?

https://nikic.github.io/2012/08/10/Are-PHP-developers-functophobic.html

Until php includes a spec for function autoloading, creating and using standard functions within a modern psr-4 codebase is cumbersome, especially compared to the simplicity of using autoloaded classes. Most devs will give up on using functions and just create abstract classes with static functions to circumvent the autoloading constraints instead of manually registering individual files in the composer autoload sections.

This is useful for more than just functions however. There are plenty of cases where one file with multiple definitions would make sense to keep together instead of splitting into several files which clutter the filesystem.

This plugin is an attempt to help php devs who use composer to have the ability to

Drawbacks

The main drawbacks to automatically including the php files in the composer autoload files section is that those files will be included anytime the composer autoloader is loaded. In larger projects, this can be a concern if you are loading files that are only needed during certain, less frequent paths of execution.

Opcache does mitigate this problem tremendously, but it is something to consider when you start sprinkling files to be included throughout your codebase.

You might also like...
Source control integration plugin framework for MantisBT, including support for Github, Gitlab, Bitbucket, Gitweb, Cgit, Subversion, Mercurial and more

Source control integration plugin framework for MantisBT, including support for Github, Gitlab, Bitbucket, Gitweb, Cgit, Subversion, Mercurial and more

This composer plugin removes unnecessary development files and directories from vendor directory

Composer Vendor Cleaner This composer plugin removes unnecessary development files and directories from vendor directory. Installation Local installat

Helper to automatically load various Kirby extensions in a plugin

Autoloader for Kirby Helper to automatically load various Kirby extensions in a plugin Commerical Usage This package is free but if you use it in a co

Dependency graph visualization for composer.json (PHP + Composer)
Dependency graph visualization for composer.json (PHP + Composer)

clue/graph-composer Graph visualization for your project's composer.json and its dependencies: Table of contents Usage graph-composer show graph-compo

Get the system resources in PHP, as memory, number of CPU'S, Temperature of CPU or GPU, Operating System, Hard Disk usage, .... Works in Windows & Linux

system-resources. A class to get the hardware resources We can get CPU load, CPU/GPU temperature, free/used memory & Hard disk. Written in PHP It is a

An utility component for XML usage and best practices in PHP

An utility component for XML usage and best practices in PHP

Magento-composer-installer - Composer installer for Magento modules
Magento-composer-installer - Composer installer for Magento modules

!!! support the maintainer of this project via Patreon: https://www.patreon.com/Flyingmana Magento Composer Installer The purpose of this project is t

Composer Repository Manager for selling Magento 2 extension and offering composer installation for ordered packages.

Magento 2 Composer Repository Credits We got inspired by https://github.com/Genmato. Composer Repository for Magento 2 This extension works as a Magen

Composer registry manager that help to easily switch to the composer repository you want

CRM - Composer Registry Manager Composer Registry Manager can help you easily and quickly switch between different composer repositories. 简体中文 Install

Comments
  • composer.json set the required version to php8

    composer.json set the required version to php8

    hi RJ

    thx for this work! i think you are the only one world wide with a package for this use case :-D

    you should upgrade it. I testet it with php 8.0. i did not have any problems

    best regards martin

    opened by mstuder 0
Releases(v0.2.4)
Owner
Krak
General PHP Libraries brought to you by RJ Garcia
Krak
Easy management of Virtualization technologies including KVM, Xen, OpenVZ, Virtuozzo, and LXC/LXD including unified commands, monitoring, template management, and many more features.

ProVirted About Easy management of Virtualization technologies including KVM, Xen, OpenVZ, Virtuozzo, and LXC/LXD including unified commands, monitori

null 2 Aug 22, 2022
Making multiple identical function calls has the same effect as making a single function call.

Making multiple identical function calls has the same effect as making a single function call.

李铭昕 4 Oct 16, 2021
A composer plugin, to install differenty types of composer packages in custom directories outside the default composer default installation path which is in the vendor folder.

composer-custom-directory-installer A composer plugin, to install differenty types of composer packages in custom directories outside the default comp

Mina Nabil Sami 136 Dec 30, 2022
Ied plugin composer - Inspired Plugin Composer: Create, publish and edit plugins from within Textpattern CMS.

ied_plugin_composer Create, publish and edit plugins from within Textpattern CMS. Creates a new page under the Extensions tab where you can edit and e

Stef Dawson 8 Oct 3, 2020
Opinionated version of Wikimedia composer-merge-plugin to work in pair with Bamarni composer-bin-plugin.

Composer Inheritance Plugin Opinionated version of Wikimedia composer-merge-plugin to work in pair with bamarni/composer-bin-plugin. Usage If you are

Théo FIDRY 25 Dec 2, 2022
Victor The Cleaner for Composer - This tool removes unnecessary files and directories from Composer vendor directory.

Victor The Cleaner for Composer This tool removes unnecessary files and directories from Composer vendor directory. The Cleaner leaves only directorie

David Grudl 133 Oct 26, 2022
Allows generate class files parse from json and map json to php object, including multi-level and complex objects;

nixihz/php-object Allows generate class files parse from json and map json to php object, including multi-level and complex objects; Installation You

zhixin 2 Sep 9, 2022
This Pocketmine-MP plugin is a plugin including a complete faction system.

SimpleFaction Simple faction plugin replacing FactionsPro which is no longer updated. Commands Command Name Command Description Available for /f help

Ayzrix 33 Dec 19, 2022
Composer plugin that wraps all composer vendor packages inside your own namespace. Intended for WordPress plugins.

Imposter Plugin Composer plugin that wraps all composer vendor packages inside your own namespace. Intended for WordPress plugins. Built with ♥ by Typ

Typist Tech 127 Dec 17, 2022
Composer Registrar Composer Plugin for Magento 2

This module add a global registration.php that replace the default glob search performed for each request to discover the components not installed from composer.

OpenGento 3 Mar 22, 2022