Completed
Branch dev (80e245)
by
unknown
04:14
created

getCallerScriptPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2016 Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * Provides utility methods dealing with file paths which do not use WordPress functions.
12
 *
13
 * @since       2.0.0
14
 * @extends     AdminPageFramework_Utility_ArraySetter
15
 * @package     AdminPageFramework
16
 * @subpackage  Utility
17
 * @internal
18
 */
19
abstract class AdminPageFramework_Utility_Path extends AdminPageFramework_Utility_ArraySetter {
20
    
21
    /**
22
     * Calculates the relative path from the given path.
23
     * 
24
     * This function is used to generate a template path.
25
     * 
26
     * @since   2.1.5
27
     * @see     http://stackoverflow.com/questions/2637945/getting-relative-path-from-absolute-path-in-php/2638272#2638272
28
     */
29
    static public function getRelativePath( $from, $to ) {
30
        
31
        // some compatibility fixes for Windows paths
32
        $from = is_dir( $from ) ? rtrim( $from, '\/') . '/' : $from;
33
        $to   = is_dir( $to )   ? rtrim( $to, '\/') . '/'   : $to;
34
        $from = str_replace( '\\', '/', $from );
35
        $to   = str_replace( '\\', '/', $to );
36
37
        $from     = explode( '/', $from );
38
        $to       = explode( '/', $to );
39
        $relPath  = $to;
40
41
        foreach( $from as $depth => $dir ) {
42
            // find first non-matching dir
43
            if( $dir === $to[ $depth ] ) {
44
                // ignore this directory
45
                array_shift( $relPath );
46
            } else {
47
                // get number of remaining dirs to $from
48
                $remaining = count( $from ) - $depth;
49
                if( $remaining > 1 ) {
50
                    // add traversals up to first matching dir
51
                    $padLength = ( count( $relPath ) + $remaining - 1 ) * -1;
52
                    $relPath = array_pad( $relPath, $padLength, '..' );
53
                    break;
54
                } else {
55
                    $relPath[ 0 ] = './' . $relPath[ 0 ];
56
                }
57
            }
58
        }
59
        return implode( '/', $relPath );
60
        
61
    }
62
    
63
    /**
64
     * Attempts to find the caller scrip path.
65
     * 
66
     * @since       3.0.0
67
     * @since       3.7.9       Made the first parameter only accepts a string.
68
     * @since       3.8.2       deprecated caching results as it caused wrong path as the passed path can be the same for different scripts.
69
     * @return      string      The found caller file path.
70
     */
71
    static public function getCallerScriptPath( $sRedirectedFilePath ) {
72
73
        $_aRedirectedFilePaths = array( $sRedirectedFilePath, __FILE__ );
74
        $_sCallerFilePath      = '';
75
        $_aBackTrace           = call_user_func_array(
76
            'debug_backtrace',
77
            self::_getDebugBacktraceArguments()
78
        );
79
        foreach( $_aBackTrace as $_aDebugInfo )  {     
80
            $_sCallerFilePath = $_aDebugInfo[ 'file' ];
81
            if ( in_array( $_sCallerFilePath, $_aRedirectedFilePaths ) ) { 
82
                continue; 
83
            }
84
            break; // catch the first found item.
85
        }
86
        return $_sCallerFilePath;
87
        
88
    } 
89
        /**
90
         * @return      array
91
         * @since       3.8.9
92
         */
93
        static private function _getDebugBacktraceArguments() {
94
            
95
            $_aArguments = array(
96
                defined( 'DEBUG_BACKTRACE_IGNORE_ARGS' )
97
                    ? DEBUG_BACKTRACE_IGNORE_ARGS
98
                    : false, // DEBUG_BACKTRACE_PROVIDE_OBJECT for PHP 5.3.6+
99
                6, // the second parameter: limit
100
            );
101
            
102
            // The second parameter is only supported in v5.4.0 or above.
103
            if ( version_compare( PHP_VERSION, '5.4.0', '<' ) ) {
104
                unset( $_aArguments[ 1 ] ); 
105
            }
106
            return $_aArguments;
107
            
108
        }
109
        
110
}
111