brightnucleus/namespace-backtracer

Get the namespace of the calling object.

v0.1.1 2016-06-18 08:44 UTC

This package is auto-updated.

Last update: 2024-11-16 06:34:03 UTC


README

Scrutinizer Code Quality Code Coverage Build Status Codacy Badge Code Climate

Latest Stable Version Total Downloads Latest Unstable Version License

Get the namespace of the calling object, by scanning the debug backtrace, skipping a known set of namespaces & functions in the process.

Table Of Contents

Overview

This class/trait allows you to traverse the debug_backtrace() to find out what the calling class/function or the calling namespace is. This is useful in cases where you need to find out at runtime in what context a given method was called.

Installation

The best way to install this package is through Composer:

composer require brightnucleus/namespace-backtracer

Basic Usage

Setting Things Up

To create a new class that can fetch the caller's name or namespace, you can either extend the BrightNucleus\NamespaceBacktracer\NamespaceBacktracerClass class or, should you already have a class you need to extend, you can import the BrightNucleus\NamespaceBacktracer\NamespaceBacktracerTrait trait.

In both cases you'll want to override one or more of the three methods that allow you to adapt the behavior to your environment:

  • getIgnoredInterfaces()

This gets the list of interfaces/classes to ignore while traversing the backtrace.

  • getIgnoredFunctions()

This gets the list of functions to ignore while traversing the backtrace.

  • getGlobalNamespace()

This defines by what string the global namespace is represented.

Making The Call

To get the caller's namespace for a specific call, you pass the output of debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) that was executed from within the callee to the getCallingNamespace() method. If you don't provide a debug_backtrace output, it will get fetched within the trait (which might be a different context than the one you want to check).

Example:

<?php

namespace CalleeNamespace {

    use BrightNucleus\NamespaceBacktracer\NamespaceBacktracerTrait;

    class Callee {

        use NamespaceBacktracerTrait;

        protected function getIgnoredInterfaces() {
            return [
                'CalleeNamespace\Callee',
            ];
        }

        public function calledFunctionGetCaller() {
            echo $this->getCaller();
        }

        public function calledFunctionGetNamespace() {
            echo $this->getCallingNamespace();
        }
    }

}

namespace CallerNamespace {

    use CalleeNamespace\Callee;

    class Caller {

        public function callingFunctionGetCaller() {
            $callee = new Callee();
            $callee->calledFunctionGetCaller();
        }

        public function callingFunctionGetNamespace() {
            $callee = new Callee();
            $callee->calledFunctionGetNamespace();
        }
    }
}

$caller = new CallerNamespace\Caller();

// This will echo "CallerNamespace\Caller" from within the
// CalleeNamespace\Callee\calledFunction() method.
$caller->callingFunctionGetCaller();

// This will echo "CallerNamespace" from within the
// CalleeNamespace\Callee\calledFunction() method.
$caller->callingFunctionGetNamespace();

Contributing

All feedback / bug reports / pull requests are welcome.

License

Copyright (c) 2016 Alain Schlesser, Bright Nucleus

This code is licensed under the MIT License.