brad-jones/import

This package is abandoned and no longer maintained. No replacement package was suggested.

Includes a PHP file in such a way as to isolate it from where it was included.

v0.0.4 2016-02-25 03:12 UTC

This package is not auto-updated.

Last update: 2021-05-01 00:26:39 UTC


README

Build Status Latest Stable Version Total Downloads License HHVM Tested

Includes a PHP file in such a way as to isolate it from where it was included. This package provides a new pseudo language construct, you will be familiar with how include and require work, well we have now added import.

Credit: https://gist.github.com/Eraknelo/6795b983825fc6a720ef

Installation:

composer require brad-jones/import

Getting access to the import function:

There are various ways you can access the import function.

  • If using PHP 5.6 or greater, you can import the import function.

    use function Brads\import;
    import(...args...);
  • Or you can call the static method on the Importer class.

    use Brads\Importer;
    Importer::import(...args...);
  • Or you can create an instance of the Importer.

    use Brads\Importer;
    $importer = new Importer;
    $importer->newImport(...args...);
  • Or if you would rather install the import function globally.

    Brads\Importer::globalise();
    import(...args...);

Dependency Injection:

The importer is Di & Test friendly, it implements the ImporterInterface.

A simple contrived php-di example:

use Brads\Importer;
use Brads\ImporterInterface;

$builder = new DI\ContainerBuilder();
$builder->addDefinitions
([
    ImporterInterface::class => DI\object(Importer::class)
]);
$container = $builder->build();

$container->get(ImporterInterface::class)->newImport(...args...);

Example Usage:

Firstly for these examples assume the contents of foo.php is:

<?php return get_defined_vars(); ?>

Secondly assume we are just using the globalised import function. Regardless of how you access it the result is the same.

Exported Value:

The import function will return any value that is returned by the imported file. Just the same as include or require. When combined with closures and a di container, you can end up with something akin to the node.js CommonJs module system.

$exported = import('foo.php');

Isolated Import:

The whole point of this project is to ensure the imported file does not have access to any variables from the parent file. So with a normal include or require you would have something like this:

$abc = '123';
$exported = include('foo.php');
var_dump($exported == ['abc' => '123']); // true

With our import function this is what happens:

$abc = '123';
$exported = include('foo.php');
var_dump($exported == []); // true

Providing a Custom Scope:

Sometimes we might want the imported file to have access to some specific data.

$abc = '123';
$scope = ['bar' => 'baz'];
$exported = include('foo.php', $scope);
var_dump($exported == $scope); // true

Include or Require:

Under the hood import does use the normal include or require.
By default we use require but you can changes this easily.

import('foo.php', null, true);  // requires foo.php
import('foo.php', null, false); // includes foo.php