Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Feeds
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Feeds
Commits
ab4dafa4
Commit
ab4dafa4
authored
4 years ago
by
Andrew Dolgov
Browse files
Options
Downloads
Patches
Plain Diff
config: add a type hint system
parent
9e2e12df
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
classes/config.php
+73
-54
73 additions, 54 deletions
classes/config.php
with
73 additions
and
54 deletions
classes/config.php
+
73
−
54
View file @
ab4dafa4
...
@@ -2,6 +2,10 @@
...
@@ -2,6 +2,10 @@
class
Config
{
class
Config
{
private
const
_ENVVAR_PREFIX
=
"TTRSS_"
;
private
const
_ENVVAR_PREFIX
=
"TTRSS_"
;
const
T_BOOL
=
1
;
const
T_STRING
=
2
;
const
T_INT
=
3
;
// override defaults, defined below in _DEFAULTS[], via environment: DB_TYPE becomes TTRSS_DB_TYPE, etc
// override defaults, defined below in _DEFAULTS[], via environment: DB_TYPE becomes TTRSS_DB_TYPE, etc
const
DB_TYPE
=
"DB_TYPE"
;
const
DB_TYPE
=
"DB_TYPE"
;
...
@@ -22,7 +26,6 @@ class Config {
...
@@ -22,7 +26,6 @@ class Config {
const
AUTH_AUTO_CREATE
=
"AUTH_AUTO_CREATE"
;
const
AUTH_AUTO_CREATE
=
"AUTH_AUTO_CREATE"
;
const
AUTH_AUTO_LOGIN
=
"AUTH_AUTO_LOGIN"
;
const
AUTH_AUTO_LOGIN
=
"AUTH_AUTO_LOGIN"
;
const
FORCE_ARTICLE_PURGE
=
"FORCE_ARTICLE_PURGE"
;
const
FORCE_ARTICLE_PURGE
=
"FORCE_ARTICLE_PURGE"
;
const
ENABLE_REGISTRATION
=
"ENABLE_REGISTRATION"
;
const
SESSION_COOKIE_LIFETIME
=
"SESSION_COOKIE_LIFETIME"
;
const
SESSION_COOKIE_LIFETIME
=
"SESSION_COOKIE_LIFETIME"
;
const
SMTP_FROM_NAME
=
"SMTP_FROM_NAME"
;
const
SMTP_FROM_NAME
=
"SMTP_FROM_NAME"
;
const
SMTP_FROM_ADDRESS
=
"SMTP_FROM_ADDRESS"
;
const
SMTP_FROM_ADDRESS
=
"SMTP_FROM_ADDRESS"
;
...
@@ -52,52 +55,53 @@ class Config {
...
@@ -52,52 +55,53 @@ class Config {
const
TTRSS_SESSION_NAME
=
"TTRSS_SESSION_NAME"
;
const
TTRSS_SESSION_NAME
=
"TTRSS_SESSION_NAME"
;
private
const
_DEFAULTS
=
[
private
const
_DEFAULTS
=
[
Config
::
DB_TYPE
=>
"pgsql"
,
Config
::
DB_TYPE
=>
[
"pgsql"
,
Config
::
T_STRING
],
Config
::
DB_HOST
=>
"db"
,
Config
::
DB_HOST
=>
[
"db"
,
Config
::
T_STRING
],
Config
::
DB_USER
=>
""
,
Config
::
DB_USER
=>
[
""
,
Config
::
T_STRING
],
Config
::
DB_NAME
=>
""
,
Config
::
DB_NAME
=>
[
""
,
Config
::
T_STRING
],
Config
::
DB_PASS
=>
""
,
Config
::
DB_PASS
=>
[
""
,
Config
::
T_STRING
],
Config
::
DB_PORT
=>
"5432"
,
Config
::
DB_PORT
=>
[
"5432"
,
Config
::
T_STRING
],
Config
::
MYSQL_CHARSET
=>
"UTF8"
,
Config
::
MYSQL_CHARSET
=>
[
"UTF8"
,
Config
::
T_STRING
],
Config
::
SELF_URL_PATH
=>
""
,
Config
::
SELF_URL_PATH
=>
[
""
,
Config
::
T_STRING
],
Config
::
SINGLE_USER_MODE
=>
""
,
Config
::
SINGLE_USER_MODE
=>
[
""
,
Config
::
T_BOOL
],
Config
::
SIMPLE_UPDATE_MODE
=>
""
,
Config
::
SIMPLE_UPDATE_MODE
=>
[
""
,
Config
::
T_BOOL
],
Config
::
PHP_EXECUTABLE
=>
"/usr/bin/php"
,
Config
::
PHP_EXECUTABLE
=>
[
"/usr/bin/php"
,
Config
::
T_STRING
],
Config
::
LOCK_DIRECTORY
=>
"lock"
,
Config
::
LOCK_DIRECTORY
=>
[
"lock"
,
Config
::
T_STRING
],
Config
::
CACHE_DIR
=>
"cache"
,
Config
::
CACHE_DIR
=>
[
"cache"
,
Config
::
T_STRING
],
Config
::
ICONS_DIR
=>
"feed-icons"
,
Config
::
ICONS_DIR
=>
[
"feed-icons"
,
Config
::
T_STRING
],
Config
::
ICONS_URL
=>
"feed-icons"
,
Config
::
ICONS_URL
=>
[
"feed-icons"
,
Config
::
T_STRING
],
Config
::
AUTH_AUTO_CREATE
=>
"true"
,
Config
::
AUTH_AUTO_CREATE
=>
[
"true"
,
Config
::
T_BOOL
],
Config
::
AUTH_AUTO_LOGIN
=>
"true"
,
Config
::
AUTH_AUTO_LOGIN
=>
[
"true"
,
Config
::
T_BOOL
],
Config
::
FORCE_ARTICLE_PURGE
=>
0
,
Config
::
FORCE_ARTICLE_PURGE
=>
[
0
,
Config
::
T_INT
],
Config
::
ENABLE_REGISTRATION
=>
""
,
Config
::
SESSION_COOKIE_LIFETIME
=>
[
86400
,
Config
::
T_INT
],
Config
::
SESSION_COOKIE_LIFETIME
=>
86400
,
Config
::
SMTP_FROM_NAME
=>
[
"Tiny Tiny RSS"
,
Config
::
T_STRING
],
Config
::
SMTP_FROM_NAME
=>
"Tiny Tiny RSS"
,
Config
::
SMTP_FROM_ADDRESS
=>
[
"noreply@localhost"
,
Config
::
T_STRING
],
Config
::
SMTP_FROM_ADDRESS
=>
"noreply@localhost"
,
Config
::
DIGEST_SUBJECT
=>
[
"[tt-rss] New headlines for last 24 hours"
,
Config
::
DIGEST_SUBJECT
=>
"[tt-rss] New headlines for last 24 hours"
,
Config
::
T_STRING
],
Config
::
CHECK_FOR_UPDATES
=>
"true"
,
Config
::
CHECK_FOR_UPDATES
=>
[
"true"
,
Config
::
T_BOOL
],
Config
::
PLUGINS
=>
"auth_internal"
,
Config
::
PLUGINS
=>
[
"auth_internal"
,
Config
::
T_STRING
],
Config
::
LOG_DESTINATION
=>
"sql"
,
Config
::
LOG_DESTINATION
=>
[
"sql"
,
Config
::
T_STRING
],
Config
::
LOCAL_OVERRIDE_STYLESHEET
=>
"local-overrides.css"
,
Config
::
LOCAL_OVERRIDE_STYLESHEET
=>
[
"local-overrides.css"
,
Config
::
DAEMON_MAX_CHILD_RUNTIME
=>
1800
,
Config
::
T_STRING
],
Config
::
DAEMON_MAX_JOBS
=>
2
,
Config
::
DAEMON_MAX_CHILD_RUNTIME
=>
[
1800
,
Config
::
T_STRING
],
Config
::
FEED_FETCH_TIMEOUT
=>
45
,
Config
::
DAEMON_MAX_JOBS
=>
[
2
,
Config
::
T_INT
],
Config
::
FEED_FETCH_NO_CACHE_TIMEOUT
=>
15
,
Config
::
FEED_FETCH_TIMEOUT
=>
[
45
,
Config
::
T_INT
],
Config
::
FILE_FETCH_TIMEOUT
=>
45
,
Config
::
FEED_FETCH_NO_CACHE_TIMEOUT
=>
[
15
,
Config
::
T_INT
],
Config
::
FILE_FETCH_CONNECT_TIMEOUT
=>
15
,
Config
::
FILE_FETCH_TIMEOUT
=>
[
45
,
Config
::
T_INT
],
Config
::
DAEMON_UPDATE_LOGIN_LIMIT
=>
30
,
Config
::
FILE_FETCH_CONNECT_TIMEOUT
=>
[
15
,
Config
::
T_INT
],
Config
::
DAEMON_FEED_LIMIT
=>
500
,
Config
::
DAEMON_UPDATE_LOGIN_LIMIT
=>
[
30
,
Config
::
T_INT
],
Config
::
DAEMON_SLEEP_INTERVAL
=>
120
,
Config
::
DAEMON_FEED_LIMIT
=>
[
500
,
Config
::
T_INT
],
Config
::
MAX_CACHE_FILE_SIZE
=>
64
*
1024
*
1024
,
Config
::
DAEMON_SLEEP_INTERVAL
=>
[
120
,
Config
::
T_INT
],
Config
::
MAX_DOWNLOAD_FILE_SIZE
=>
16
*
1024
*
1024
,
Config
::
MAX_CACHE_FILE_SIZE
=>
[
64
*
1024
*
1024
,
Config
::
T_INT
],
Config
::
MAX_FAVICON_FILE_SIZE
=>
1
*
1024
*
1024
,
Config
::
MAX_DOWNLOAD_FILE_SIZE
=>
[
16
*
1024
*
1024
,
Config
::
T_INT
],
Config
::
CACHE_MAX_DAYS
=>
7
,
Config
::
MAX_FAVICON_FILE_SIZE
=>
[
1
*
1024
*
1024
,
Config
::
T_INT
],
Config
::
MAX_CONDITIONAL_INTERVAL
=>
3600
*
12
,
Config
::
CACHE_MAX_DAYS
=>
[
7
,
Config
::
T_INT
],
Config
::
DAEMON_UNSUCCESSFUL_DAYS_LIMIT
=>
30
,
Config
::
MAX_CONDITIONAL_INTERVAL
=>
[
3600
*
12
,
Config
::
T_INT
],
Config
::
LOG_SENT_MAIL
=>
""
,
Config
::
DAEMON_UNSUCCESSFUL_DAYS_LIMIT
=>
[
30
,
Config
::
T_INT
],
Config
::
HTTP_PROXY
=>
""
,
Config
::
LOG_SENT_MAIL
=>
[
""
,
Config
::
T_BOOL
],
Config
::
FORBID_PASSWORD_CHANGES
=>
""
,
Config
::
HTTP_PROXY
=>
[
""
,
Config
::
T_STRING
],
Config
::
TTRSS_SESSION_NAME
=>
"ttrss_sid"
,
Config
::
FORBID_PASSWORD_CHANGES
=>
[
""
,
Config
::
T_BOOL
],
Config
::
TTRSS_SESSION_NAME
=>
[
"ttrss_sid"
,
Config
::
T_STRING
],
];
];
private
static
$instance
;
private
static
$instance
;
...
@@ -115,28 +119,43 @@ class Config {
...
@@ -115,28 +119,43 @@ class Config {
$ref
=
new
ReflectionClass
(
get_class
(
$this
));
$ref
=
new
ReflectionClass
(
get_class
(
$this
));
foreach
(
$ref
->
getConstants
()
as
$const
=>
$cvalue
)
{
foreach
(
$ref
->
getConstants
()
as
$const
=>
$cvalue
)
{
if
(
strpos
(
$const
,
"_"
)
!==
0
)
{
if
(
isset
(
$this
::
_DEFAULTS
[
$const
])
)
{
$override
=
getenv
(
$this
::
_ENVVAR_PREFIX
.
$const
);
$override
=
getenv
(
$this
::
_ENVVAR_PREFIX
.
$const
);
$this
->
params
[
$cvalue
]
=
!
empty
(
$override
)
?
$override
:
$this
::
_DEFAULTS
[
$const
];
list
(
$defval
,
$deftype
)
=
$this
::
_DEFAULTS
[
$const
];
$this
->
params
[
$cvalue
]
=
[
$this
->
cast_to
(
!
empty
(
$override
)
?
$override
:
$defval
,
$deftype
),
$deftype
];
}
}
}
}
}
}
private
function
cast_to
(
string
$value
,
int
$type_hint
)
{
switch
(
$type_hint
)
{
case
self
::
T_BOOL
:
return
sql_bool_to_bool
(
$value
);
case
self
::
T_INT
:
return
(
int
)
$value
;
default
:
return
$value
;
}
}
private
function
_get
(
string
$param
)
{
private
function
_get
(
string
$param
)
{
return
$this
->
params
[
$param
];
list
(
$value
,
$type_hint
)
=
$this
->
params
[
$param
];
return
$this
->
cast_to
(
$value
,
$type_hint
);
}
}
private
function
_add
(
string
$param
,
string
$default
)
{
private
function
_add
(
string
$param
,
string
$default
,
int
$type_hint
)
{
$override
=
getenv
(
$this
::
_ENVVAR_PREFIX
.
$param
);
$override
=
getenv
(
$this
::
_ENVVAR_PREFIX
.
$param
);
$this
->
params
[
$param
]
=
!
empty
(
$override
)
?
$override
:
$default
;
$this
->
params
[
$param
]
=
[
$this
->
cast_to
(
!
empty
(
$override
)
?
$override
:
$default
,
$type_hint
),
$type_hint
]
;
}
}
static
function
add
(
string
$param
,
string
$default
)
{
static
function
add
(
string
$param
,
string
$default
,
int
$type_hint
=
Config
::
T_STRING
)
{
$instance
=
self
::
get_instance
();
$instance
=
self
::
get_instance
();
return
$instance
->
_add
(
$param
,
$default
);
return
$instance
->
_add
(
$param
,
$default
,
$type_hint
);
}
}
static
function
get
(
string
$param
)
{
static
function
get
(
string
$param
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment