1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* Helper class for working with paths and urls for App_Config. |
6
|
|
|
* |
7
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
8
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
10
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
11
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
12
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
13
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
14
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
15
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
16
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
17
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
18
|
|
|
* |
19
|
|
|
* @author Glynn Quelch <[email protected]> |
20
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
21
|
|
|
* @package PinkCrab\Perique |
22
|
|
|
* @since 0.4.0 |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace PinkCrab\Perique\Utils; |
26
|
|
|
|
27
|
|
|
class App_Config_Path_Helper { |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Normalises a path. |
31
|
|
|
* |
32
|
|
|
* Removes all trailing slashes, either forward or back. |
33
|
|
|
* |
34
|
|
|
* @param string $path |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public static function normalise_path( string $path ): string { |
38
|
|
|
return rtrim( $path, '/\\' ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Generate the assumed views paths from the base path. |
43
|
|
|
* |
44
|
|
|
* @param string $base_path |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public static function assume_view_path( string $base_path ): string { |
48
|
|
|
// Remove any trailing slash. |
49
|
|
|
$base_path = self::normalise_path( $base_path ); |
50
|
|
|
return $base_path . \DIRECTORY_SEPARATOR . 'views'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Generate the assumed base url from the base path. |
55
|
|
|
* |
56
|
|
|
* @param string $base_path |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public static function assume_base_url( string $base_path ): string { |
60
|
|
|
// Remove any trailing slash. |
61
|
|
|
$base_path = self::normalise_path( $base_path ); |
62
|
|
|
return plugins_url( basename( $base_path ) ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Generate the assumed assets url from both the base path and base view path. |
67
|
|
|
* |
68
|
|
|
* @param string $base_path |
69
|
|
|
* @param string $view_path |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public static function assume_view_url( string $base_path, string $view_path ): string { |
73
|
|
|
|
74
|
|
|
$base_path = self::normalise_path( $base_path ); |
75
|
|
|
$view_path = self::normalise_path( $view_path ); |
76
|
|
|
|
77
|
|
|
// Remove any trailing slash. |
78
|
|
|
$diff = ltrim( str_replace( $base_path, '', $view_path ), '/\\' ); |
79
|
|
|
|
80
|
|
|
// Return the base url with the diff. |
81
|
|
|
return self::assume_base_url( $base_path ) . '/' . $diff; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|