1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Gitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) 2015-2016 The Gitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
use Gitamin\Facades\Setting; |
13
|
|
|
use Illuminate\Support\Facades\Config; |
14
|
|
|
use Illuminate\Support\Facades\Request; |
|
|
|
|
15
|
|
|
use Jenssegers\Date\Date; |
16
|
|
|
|
17
|
|
|
if (! function_exists('back_url')) { |
18
|
|
|
/** |
19
|
|
|
* Create a new back url. |
20
|
|
|
* |
21
|
|
|
* @param string|null $route |
22
|
|
|
* @param array $parameters |
23
|
|
|
* @param int $status |
24
|
|
|
* @param array $headers |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
function back_url($route = null, $parameters = [], $status = 302, $headers = []) |
29
|
|
|
{ |
30
|
|
|
$url = app('url'); |
31
|
|
|
|
32
|
|
|
if ($route !== null && $url->previous() === $url->full()) { |
33
|
|
|
return $url->route($route, $parameters, $status, $headers); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $url->previous(); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (! function_exists('set_active')) { |
41
|
|
|
/** |
42
|
|
|
* Set active class if request is in path. |
43
|
|
|
* |
44
|
|
|
* @param string $path |
45
|
|
|
* @param array $classes |
46
|
|
|
* @param string $active |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
function set_active($path, array $classes = [], $active = 'active') |
51
|
|
|
{ |
52
|
|
|
if (Request::is($path)) { |
53
|
|
|
$classes[] = $active; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$class = e(implode(' ', $classes)); |
57
|
|
|
|
58
|
|
|
return empty($classes) ? '' : "class=\"{$class}\""; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (! function_exists('formatted_date')) { |
63
|
|
|
/** |
64
|
|
|
* Formats a date with the user timezone and the selected format. |
65
|
|
|
* |
66
|
|
|
* @param string $date |
67
|
|
|
* |
68
|
|
|
* @return \Jenssegers\Date\Date |
69
|
|
|
*/ |
70
|
|
|
function formatted_date($date) |
71
|
|
|
{ |
72
|
|
|
$dateFormat = Setting::get('date_format', 'jS F Y'); |
73
|
|
|
|
74
|
|
|
return (new Date($date))->format($dateFormat); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (! function_exists('subscribers_enabled')) { |
79
|
|
|
/** |
80
|
|
|
* Is the subscriber functionality enabled and configured. |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
function subscribers_enabled() |
85
|
|
|
{ |
86
|
|
|
$isEnabled = Setting::get('enable_subscribers', false); |
87
|
|
|
$mailAddress = Config::get('mail.from.address', false); |
88
|
|
|
$mailFrom = Config::get('mail.from.name', false); |
89
|
|
|
|
90
|
|
|
return $isEnabled && $mailAddress && $mailFrom; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (! function_exists('color_darken')) { |
95
|
|
|
/** |
96
|
|
|
* Darken a color. |
97
|
|
|
* |
98
|
|
|
* @param string $hex |
99
|
|
|
* @param int $percent |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
function color_darken($hex, $percent) |
104
|
|
|
{ |
105
|
|
|
$hex = preg_replace('/[^0-9a-f]/i', '', $hex); |
106
|
|
|
$new_hex = '#'; |
107
|
|
|
|
108
|
|
|
if (strlen($hex) < 6) { |
109
|
|
|
$hex = $hex[0] + $hex[0] + $hex[1] + $hex[1] + $hex[2] + $hex[2]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
for ($i = 0; $i < 3; ++$i) { |
113
|
|
|
$dec = hexdec(substr($hex, $i * 2, 2)); |
114
|
|
|
$dec = min(max(0, $dec + $dec * $percent), 255); |
115
|
|
|
$new_hex .= str_pad(dechex($dec), 2, 0, STR_PAD_LEFT); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $new_hex; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (! function_exists('color_contrast')) { |
123
|
|
|
/** |
124
|
|
|
* Calculates colour contrast. |
125
|
|
|
* |
126
|
|
|
* https://24ways.org/2010/calculating-color-contrast/ |
127
|
|
|
* |
128
|
|
|
* @param string $hexcolor |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
function color_contrast($hexcolor) |
133
|
|
|
{ |
134
|
|
|
$r = hexdec(substr($hexcolor, 0, 2)); |
135
|
|
|
$g = hexdec(substr($hexcolor, 2, 2)); |
136
|
|
|
$b = hexdec(substr($hexcolor, 4, 2)); |
137
|
|
|
$yiq = (($r * 100) + ($g * 400) + ($b * 114)) / 1000; |
138
|
|
|
|
139
|
|
|
return ($yiq >= 128) ? 'black' : 'white'; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: