[
class tree: tangra_lib
] [
index: tangra_lib
] [
all elements
]
tangra_lib
Packages:
tangra_lib
Source for file config_loader_file.class.php
Documentation is available at
config_loader_file.class.php
<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//
/**
* Contains class Config_Loader_File
*
@package
tangra_lib
*
@subpackage
core
*/
/**
* Loading parent class
*/
require_once
(
TANGRA_MAIN_DIR
.
'core/config_loader.class.php'
)
;
/**
* Loading exception
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_config_loader_file_error_fatal.class.php'
)
;
/**
* Loads configuration data from file.
*
* Loads configuration data from file. According to Tangra Filenames Convention (TODO - provide link) configuration files has *.conf extention.
*
*
@package
tangra_lib
*
@subpackage
core
*/
class
Config_Loader_File
extends
Config_Loader
{
private
$imports
=
array
(
)
;
/**
* Loads the configuration file and parses it. Ater construction object is ready for use (calls to get_conf_value() method)
*
*
@param
string
$file
- full path to fconfiguration file
*/
function
__construct
(
$file
)
{
if
(
file_exists
(
$file
))
{
if
(
is_file
(
$file
))
{
if
(
is_readable
(
$file
))
{
$file_content
=
$this
->
load_file
(
$file
)
;
$conf_arr
=
$this
->
parse_file_arr
(
$file_content
)
;
$tmp_arr
=
$this
->
parse_conf_arr
(
$conf_arr
)
;
$this
->
set_pairs
(
array_merge
(
$this
->
pairs
,
$tmp_arr
))
;
if
(
$this
->
imports
)
{
foreach
(
$this
->
imports
as
$import_file
)
{
$this
->
import_file
(
$import_file
,
dirname
(
$file
))
;
}
}
foreach
(
$this
->
pairs
as
$pair_key
=>
&
$pair_value
)
{
$pair_value
=
trim
(
$pair_value
)
;
$pair_value
=
$this
->
expand
(
$pair_value
,
$pair_key
)
;
}
}
else
{
throw
new
TE_Config_Loader_File_Error_Fatal
(
'Config file is not readable. File: '
.
$file
)
;
}
}
else
{
throw
new
TE_Config_Loader_File_Error_Fatal
(
'Parameter $file is not file. File: '
.
$file
)
;
}
}
else
{
throw
new
TE_Config_Loader_File_Error_Fatal
(
'Config file does not exists. File: '
.
$file
)
;
}
}
/**
* Loads
*
*
@param
$string
$file
*
@return
boolean
- returns file contents as array one element per row
*
@access
private
*/
private
function
load_file
(
$file
)
{
return
file
(
$file
)
;
}
/**
* Parses array loaded by $this->load_file(). Skips rows that are commets or missformed (i.e. starting with blank space)
*
*
@param
array
$file_arr
*
@return
array
simple associative array that contain just "valid" rows
*
@access
private
*
*/
private
function
parse_file_arr
(
$file_arr
)
{
$ret_arr
=
array
(
)
;
for
(
$i
=
0
;
$i
<
count
(
$file_arr
)
;
$i
++
)
{
if
(
strlen
(
$file_arr
[
$i
]
))
{
$first_char
=
substr
(
$file_arr
[
$i
]
,
0
,
1
)
;
if
(
$first_char
!=
' '
&&
$first_char
!=
"\t"
&&
$first_char
!=
'/'
&&
$first_char
!=
'#'
&&
$first_char
!=
"\n"
)
{
if
(
$first_char
!=
'!'
)
{
array_push
(
$ret_arr
,
$file_arr
[
$i
]
)
;
}
else
{
$this
->
parse_command_line
(
$file_arr
[
$i
]
,
$i
)
;
}
}
}
}
return
$ret_arr
;
}
/**
* Parses array returned by $this->parse_file_arr. Populates $this->pairs.
*
*
@param
unknown_type
$conf_arr
*
@return
array
simple associative array that may be used (and it is used in __construct) for setting $this->pairs
*
@access
private
*/
private
function
parse_conf_arr
(
$conf_arr
)
{
$ret_arr
=
array
(
)
;
foreach
(
$conf_arr
as
$row
)
{
$first_equal_pos
=
strpos
(
$row
,
'='
)
;
if
((
$first_equal_pos
!==
false
)
&&
(
$first_equal_pos
>
0
))
{
$key
=
substr
(
$row
,
0
,
$first_equal_pos
)
;
$value
=
substr
(
$row
,
$first_equal_pos
+
1
)
;
$ret_arr
[
$key
]
=
$value
;
}
}
return
$ret_arr
;
}
/**
* Parses line that contains command, e.g. starting with "!"
*
*
@param
string
$line
*/
private
function
parse_command_line
(
$line
,
$line_no
)
{
$tmp_arr
=
explode
(
' '
,
$line
)
;
if
(
$tmp_arr
==
$line
)
{
$tmp_arr
=
explode
(
"\t"
,
$line
)
;
}
if
(
is_array
(
$tmp_arr
))
{
switch
(
trim
(
$tmp_arr
[
0
]
))
{
case
'!import'
:
if
(
array_key_exists
(
1
,
$tmp_arr
))
{
$this
->
imports
[
]
=
trim
(
$tmp_arr
[
1
]
)
;
}
else
{
throw
new
TE_Config_Loader_File_Error_Fatal
(
"
No file specified for import at line
$line_no
.
"
)
;
}
break
;
default
:
throw
new
TE_Config_Loader_File_Error_Fatal
(
"
Unknown command at line
$line_no
:
"
.
$tmp_arr
[
0
]
)
;
break
;
}
}
else
{
throw
new
TE_Config_Loader_File_Error_Fatal
(
"
Invalid command at line
$line_no
:
"
.
$line
)
;
}
}
private
function
import_file
(
$file
,
$config_dir
)
{
$config_dir
=
tangra_normalize_path
(
$config_dir
)
;
$file_fullpath
=
$config_dir
.
$file
;
if
(
file_exists
(
$file_fullpath
))
{
$tmp_clf
=
new
Config_Loader_File
(
$file_fullpath
)
;
$pairs
=
$tmp_clf
->
get_pairs
(
)
;
foreach
(
$pairs
as
$key
=>
$value
)
{
$this
->
add_pair
(
$key
,
$value
)
;
}
}
else
{
throw
new
TE_Config_Loader_File_Error_Fatal
(
"
Can not find import file
$file
in dir
$config_dir
"
)
;
}
}
}