summaryrefslogtreecommitdiffstats
path: root/system/classes/misc.php
blob: b14e9ebb13ff7e22a3eabb921d00572adc7792d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php

/**
 * Miscellaneous useful functions.
 * @package Core
 */
class Misc
{

	/**
	 * Retrieve value from array by key, with default value support.
	 *
	 * @param array  $array   Input array
	 * @param string $key     Key to retrieve from the array
	 * @param mixed  $default Default value to return if the key is not found
	 * @return mixed An array value if it was found or default value if it is not
	 * @access public
	 * @static
	 */
	public static function arr($array, $key, $default = null)
	{
		if (isset($array[$key]))
		{
			return $array[$key];
		}
		return $default;
	}

	/**
	 * Finds full path to a specified file
	 * It will search in the /application folder first, then in all enabled modules
	 * and then the /system folder
	 *
	 * @param string  $subfolder  Subfolder to search in e.g. 'classes' or 'views'
	 * @param string  $name       Name of the file without extension
	 * @param string  $extension  File extension
	 * @param boolean $return_all If 'true' returns all mathced files as array,
	 *                            otherwise returns the first file found
	 * @return mixed  Full path to the file or False if it is not found
	 * @access public
	 * @static
	 */
	public static function find_file($subfolder, $name, $extension = 'php', $return_all = false)
	{
		$folders = array(APPDIR);

		foreach (Config::get('core.modules', array()) as $module)
		{
			$folders[] = MODDIR.$module.'/';
		}
		$folders[] = SYSDIR;

		$fname = $name.'.'.$extension;
		$found_files = array();

		foreach ($folders as $folder)
		{
			$file = $folder.$subfolder.'/'.$fname;
			if (file_exists($file))
			{
				if (!$return_all)
				{
					return($file);
				}
				$found_files[] = $file;
			}
		}

		if (!empty($found_files))
		{
			return $found_files;
		}

		return false;
	}

}