Skip to content
Register PHP imports via JSON.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Add file_get_contents error handling to Config::getClassImports() Mar 16, 2019
.gitignore
LICENSE Update README, composer.json Mar 8, 2019
README.md Update README Mar 20, 2019
composer.json Update composer.json Mar 8, 2019
functions.php Add missing return type hints, refactoring in Config Mar 15, 2019
imports.json.sample Update imports.json.sample Mar 15, 2019

README.md

ezimports

Register PHP imports via JSON.

Table of Contents

Installation

composer require nickolasburr/ezimports

Configuration

Prior to searching, ezimports looks for the following constants:

EZIMPORTS_MODULE_PATH: The path to the ezimports module directory. Defaults to vendor/nickolasburr/ezimports. <VENDOR>_<MODULE>_IMPORTS_FILE_NAME: Imports file basename. Defaults to imports.json. <VENDOR>_<MODULE>_IMPORTS_FILE_PATH: Imports file path. Defaults to vendor/<VENDOR>/<MODULE>/imports.json.

Usage

Instead of including imports statically:

<?php

namespace Example;

use Dictionary\WordInterface;
use External\Entity;
use Somewhere\Outside\ThisNamespace as Outsider;

class Name implements WordInterface
{
}

Invoke include_imports with FQCN and module name:

<?php

namespace Example;

\include_imports(Name::class, '<VENDOR>/<MODULE>');

class Name implements WordInterface
{
}

Next, add the imports to imports.json in the module root directory:

[
  {
    "class": "Example\\Name",
    "imports": [
      {
        "use": "Dictionary\\WordInterface"
      },
      {
        "use": "External\\Entity"
      },
      {
        "use": "Somewhere\\Outside\\ThisNamespace",
        "as": "Outsider"
      }
    ]
  }
]

Alternatively, specify the path via <VENDOR>_<MODULE>_IMPORTS_FILE_PATH. The easiest way to achieve this is to add a bootstrap file to the module, which can be loaded first.

<?php
/**
 * bootstrap.php
 *
 * As an example, assume the module is
 * named 'nickolasburr/testmodule'.
 */

if (!defined('NICKOLASBURR_TESTMODULE_IMPORTS_FILE_PATH')) {
    define('NICKOLASBURR_TESTMODULE_IMPORTS_FILE_PATH', '/path/to/module/imports.json');
}

Benefits

The ezimports mission statement is simple:

  • Reduce size of PHP source files
  • Provide an easier way to map dependencies

Caveats

Just like anything else, there are tradeoffs, such as:

  • Less performant (no formal benchmarks currently)
  • Less secure (but not insecure when managed properly)
You can’t perform that action at this time.