Completed
Push — master ( 03df00...648c2d )
by Anu
07:35 queued 05:02
created

bootstrap/helpers.php (3 issues)

1
<?php
2
3
function f3($get=null){$f3=Base::instance();return $get?$f3->get($get):$f3;}
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $f3. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
This method's name is shorter than the configured minimum length of 3 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
4
5
function base_path($inner = ''){return realpath(__DIR__.'/../').'/'.$inner;}
6
function storage_path($inner = ''){return base_path('storage/').'/'.$inner;}
7
function db_path($db = ''){return storage_path('db/').$db;}
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
8
function lang_path($lang = ''){return base_path('resources/lang/').'/'.$lang;}
9
function views_path(){return base_path('resources/views/');}
10
function config_path($config = ''){return base_path('config/').'/'.$config;}
11
12
function abort(){f3()->abort();}
13
function status($code=404){f3()->error($code);}
14
function reroute($where){f3()->reroute($where);}
15
function is_api($path){if(is_string($path)) {return explode('/', $path)[1] === 'api';}return false;}
16
17
function template($template, array $params = [], $mime = 'text/html') {
18
	$f3 = f3();
19
	if(!empty($params)) {
20
		$f3->mset($params);
21
	}
22
	if(is_array($template)){
23
		$layout = $template[0];
24
		$view = $template[1];
25
	}else{
26
		$layout = 'layouts/app.htm';
27
		$view = $template;
28
	}
29
	$f3->set('user', App::instance()->user());
30
	$f3->set('content', extension($view, 'htm'));
31
	echo Template::instance()->render($layout, $mime);
32
}
33
34
function str_contains($haystack,$needles){foreach((array)$needles as $needle){if($needle!=''&&mb_strpos($haystack,$needle)!==false){return true;}}return false;}
35
function extension($file,$default='json'){return $file.'.'.(pathinfo($file,PATHINFO_EXTENSION)?:$default);}
36
function flash($message, $type = 'success') {Flash::instance()->addMessage($message, $type);}
37
function trans($key,$params=null){return f3()->format(f3()->get($key),($params?:''));}
38
function error($error) {
39
	if(null === $error) {return;}
40
	if(is_array($error)) {
41
		foreach($error as $err) {
42
			if(is_array($err)) {
43
				foreach($err as $e) {
44
					flash($e, 'danger');
45
				}
46
			}else{
47
				flash($err, 'danger');
48
			}
49
		}
50
	}else{
51
		flash($error, 'danger');
52
	}
53
}