Giter Club home page Giter Club logo

alias's Introduction

Fuel\Alias

Build Status

Package for lazy class aliasing.

Synopsys

Within FuelPHP class aliases are used to provide easy access to namespaced classes and facilitate class inheritance injection.

The package exposes an alias manager which lets you create 3 types of aliases:

  • Literal
    A one-to-one translation. Class "Namespaced\Classname" translates to "Another\Classname".
  • Replacement
    A pattern is matched an through replacements a new class is generated. "Namespace\*" maps to "Alias\$1".
  • Computed
    A callback (Closure or any other callable) computes the new class name. The callback is provides with an array containing the fully namespaced class name segments.

When registering the alias manager append or prepends itself to the autoload stack to act as a pre-processor or fallback. Depending on the amount of aliases and it could be beneficial to alternate between pre- or appending.

By default the manager will prepend itself to the autoloader stack.

Basic Usage

// Create a new alias manager
$manager = new Fuel\Alias\Manager;

// Register the manager
$manager->register();

// Alias one class
$manager->alias('Alias\Me', 'To\This');

// Or alias many
$manager->alias(array(
	'Alias\This' => 'To\Me',
	'AndAlias\This' => 'To\SomethingElse',
));

// 

Advanced Usage

$manager = new Fuel\Alias\Manager;

// Alias with wildcards
$manager->alias('Namespaced\*', 'Other\\$1');

// Or even computed classes
$manager->alias('*', function($segments){
	return 'Namespaced\'.reset($segments);
});

This can result into class resolving that doesn't exists. Luckily the package is smart enough the check wether the class exists and will continue to look for the correct class if the resolved class does not exist. This is also taken into account when it comes to caching. Only resolved classes that exist will be cached.

Caching

In order to get blazing fast aliasing you can enable caching. There are three types of caching: alias, class and unwind. They're each helpful in different situations. The default is alias because this is what the package does originally and reduces the most processing.

All cache is file based and loaded when available when the caching is added to the manager. You can add caching to in the following manner:

$manager = new Fuel\Alias\Manager();

$manager->cache(new Fuel\Alias\Cache('/path/to/cache.php'));
/**
 * Via direct cache object injection. This is handy when you want
 * To implement your own caching method.
 */

$manager->cache('/path/to/cache', 'unwind');
// Note the optional .php

Caching doesn't contain garbage collection as aliases are meant to never expire. It should however be part of your deploy routine to delete the cache files. If you do want a cache routine, feel free to use the Fuel\Alias\Cache::delete method to remove the cache manually or implemented in your own garbage collection:

$cache = new Fuel\Alias\Cache('/path/to/cache');

// Remove the cache
$cache->delete();

It's important to think about what type of caching you use. Let's look at them.

Alias Caching

Alias cashing generated a file that contains class_alias calls:

class_alias('ThisClass', 'ToThisClass`);

Class Caching

Class caching will create a file with actual class extensions:

namespace Something { class Nested extends \Some\Other\Class {} }

Unwind Caching

Unwind caching resolves all the computed aliases and loads them into the manager as plain aliases:

$manager->alias(array(
	'Something' => 'Something\ElseCLass',
));

alias's People

Contributors

frankdejonge avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.