2017 © Pedro Peláez
 

library import

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

image

brad-jones/import

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

  • Thursday, February 25, 2016
  • by brad-jones
  • Repository
  • 1 Watchers
  • 0 Stars
  • 62 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Brads PHP File Importer

Build Status Latest Stable Version Total Downloads License HHVM Tested, (*1)

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., (*2)

Credit: https://gist.github.com/Eraknelo/6795b983825fc6a720ef, (*3)

Installation:

composer require brad-jones/import

Getting access to the import function:

There are various ways you can access the import function., (*4)

  • If using PHP 5.6 or greater, you can import the import function., (*5)

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

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

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

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

Dependency Injection:

The importer is Di & Test friendly, it implements the ImporterInterface., (*9)

A simple contrived php-di example:, (*10)

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:, (*11)

<?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., (*12)

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., (*13)

$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:, (*14)

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

With our import function this is what happens:, (*15)

$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., (*16)

$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., (*17)

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

The Versions

25/02 2016

dev-master

9999999-dev https://github.com/brad-jones/import

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

  Sources   Download

MIT

The Development Requires

include import require isolate

25/02 2016

v0.0.4

0.0.4.0 https://github.com/brad-jones/import

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

  Sources   Download

MIT

The Development Requires

include import require isolate

23/02 2016

v0.0.3

0.0.3.0 https://github.com/brad-jones/import

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

  Sources   Download

MIT

The Development Requires

include import require isolate

23/02 2016

v0.0.2

0.0.2.0 https://github.com/brad-jones/import

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

  Sources   Download

MIT

The Development Requires

include import require isolate

22/02 2016

v0.0.1

0.0.1.0 https://github.com/brad-jones/import

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

  Sources   Download

MIT

include import require isolate