Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
question2answer
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
outils
question2answer
Commits
4559829f
Commit
4559829f
authored
Jan 13, 2018
by
Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify Container, move helpers
parent
eacf34fc
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
65 additions
and
64 deletions
+65
-64
page.php
qa-include/app/page.php
+5
-4
qa-base.php
qa-include/qa-base.php
+32
-5
Application.php
qa-src/App/Application.php
+6
-6
Container.php
qa-src/App/Container.php
+10
-10
PageNotFoundException.php
qa-src/Http/Exceptions/PageNotFoundException.php
+1
-1
Route.php
qa-src/Http/Route.php
+10
-10
Router.php
qa-src/Http/Router.php
+1
-1
helpers.php
qa-src/helpers.php
+0
-27
No files found.
qa-include/app/page.php
View file @
4559829f
...
...
@@ -181,9 +181,9 @@ function qa_get_request_content()
$routing
=
qa_page_routing
();
qa_routing_config
();
$route
=
app
(
'router'
)
->
match
(
$requestlower
);
$route
=
qa_service
(
'router'
)
->
match
(
$requestlower
);
if
(
isset
(
$route
)
)
{
if
(
$route
!==
null
)
{
// use new Controller system
qa_set_template
(
$route
->
getId
());
$controllerClass
=
$route
->
getController
();
...
...
@@ -455,12 +455,13 @@ function qa_page_routing()
);
}
/**
*
[qa_routing_config description]
*
Set up routing.
*/
function
qa_routing_config
()
{
$router
=
app
(
'router'
);
$router
=
qa_service
(
'router'
);
$router
->
addRoute
(
'user'
,
'get'
,
'user/{str}'
,
'\Q2A\Controllers\User\UserProfile'
,
'profile'
);
$router
->
addRoute
(
'user'
,
'post'
,
'user/{str}'
,
'\Q2A\Controllers\User\UserProfile'
,
'profile'
);
$router
->
addRoute
(
'user-self'
,
'get'
,
'user'
,
'\Q2A\Controllers\User\UserProfile'
,
'index'
);
...
...
qa-include/qa-base.php
View file @
4559829f
...
...
@@ -46,6 +46,8 @@ function qa_autoload($class)
}
spl_autoload_register
(
'qa_autoload'
);
use
Q2A\App\Application
;
// Execution section of this file - remainder contains function definitions
...
...
@@ -60,11 +62,6 @@ if (defined('QA_WORDPRESS_LOAD_FILE')) {
require_once
QA_JOOMLA_LOAD_FILE
;
}
require
QA_BASE_DIR
.
'qa-src/helpers.php'
;
app
();
qa_initialize_constants_2
();
qa_initialize_modularity
();
qa_register_core_modules
();
...
...
@@ -1843,6 +1840,36 @@ function qa_retrieve_url($url)
/**
* Helper function to access the Application object.
* @return Application
*/
function
qa_app
()
{
return
Application
::
getInstance
();
}
/**
* Helper function to access services in the Container.
* If the $key parameter is set and the $object parameter is null the container is called to resolve the $key.
* If the $key and the $object parameters are null the container is called to bind the $object to the $key.
* @param mixed $key Identifier for the object to get/set.
* @param mixed $object Object to set in the $key (if null, a stored object is returned)
* @return mixed
*/
function
qa_service
(
$key
,
$object
=
null
)
{
$app
=
Application
::
getInstance
();
if
(
$object
===
null
)
{
return
$app
->
getContainer
()
->
get
(
$key
);
}
$app
->
getContainer
()
->
set
(
$key
,
$object
);
}
/**
* Shortcut to get or set an option value without specifying database
* @param $name
* @param mixed $value
...
...
qa-src/App/Application.php
View file @
4559829f
...
...
@@ -28,11 +28,11 @@ class Application
/** @var static */
protected
static
$instance
;
p
ublic
function
__construct
()
p
rotected
function
__construct
()
{
$this
->
container
=
new
Container
();
$this
->
registerCore
Binding
s
();
$this
->
registerCore
Service
s
();
}
/**
...
...
@@ -41,7 +41,7 @@ class Application
*/
public
static
function
getInstance
()
{
if
(
is_null
(
static
::
$instance
)
)
{
if
(
static
::
$instance
===
null
)
{
static
::
$instance
=
new
static
();
}
...
...
@@ -49,11 +49,11 @@ class Application
}
/**
* Register the
binding
s used by the core.
* Register the
service
s used by the core.
*/
private
function
registerCore
Binding
s
()
private
function
registerCore
Service
s
()
{
$this
->
container
->
bind
(
'router'
,
new
Router
());
$this
->
container
->
set
(
'router'
,
new
Router
());
}
/**
...
...
qa-src/App/Container.php
View file @
4559829f
...
...
@@ -23,29 +23,29 @@ use Q2A\Exceptions\FatalErrorException;
class
Container
{
/** @var array */
protected
$
binding
s
=
array
();
protected
$
service
s
=
array
();
/**
* Bind an object to a key.
* @param
mixed
$key The key to bind the object to
* @param
string
$key The key to bind the object to
* @param mixed $object The object to bind to the key
*/
public
function
bind
(
$key
,
$object
)
public
function
set
(
$key
,
$object
)
{
$this
->
binding
s
[
$key
]
=
$object
;
$this
->
service
s
[
$key
]
=
$object
;
}
/**
* Return an object
boun
d to the given key. If the key is not found an exception is thrown.
* @param
mixed
$key The key to look for
* Return an object
assigne
d to the given key. If the key is not found an exception is thrown.
* @param
string
$key The key to look for
* @return mixed
*/
public
function
resolve
(
$key
)
public
function
get
(
$key
)
{
if
(
isset
(
$this
->
binding
s
[
$key
]))
{
return
$this
->
binding
s
[
$key
];
if
(
isset
(
$this
->
service
s
[
$key
]))
{
return
$this
->
service
s
[
$key
];
}
throw
new
FatalErrorException
(
sprintf
(
'Key "%s" not
b
ound in container'
,
$key
));
throw
new
FatalErrorException
(
sprintf
(
'Key "%s" not
f
ound in container'
,
$key
));
}
}
qa-src/Http/Exceptions/PageNotFoundException.php
View file @
4559829f
...
...
@@ -29,7 +29,7 @@ class PageNotFoundException extends ErrorMessageException
*/
public
function
__construct
(
$message
=
null
)
{
if
(
is_null
(
$message
)
)
{
if
(
$message
===
null
)
{
$message
=
qa_lang_html
(
'main/page_not_found'
);
}
...
...
qa-src/Http/Route.php
View file @
4559829f
...
...
@@ -50,16 +50,6 @@ class Route
}
/**
* Bind actual request parameters to the route, replacing any existing ones.
*
* @param array $parameters
*/
public
function
bindParameters
(
$parameters
)
{
$this
->
parameters
=
$parameters
;
}
/**
* @return string
*/
public
function
getId
()
...
...
@@ -106,4 +96,14 @@ class Route
{
return
$this
->
parameters
;
}
/**
* Bind actual request parameters to the route, replacing any existing ones.
*
* @param array $parameters
*/
public
function
setParameters
(
$parameters
)
{
$this
->
parameters
=
$parameters
;
}
}
qa-src/Http/Router.php
View file @
4559829f
...
...
@@ -61,7 +61,7 @@ class Router
$pathRegex
=
$this
->
buildPathRegex
(
$route
->
getRoutePath
());
if
(
preg_match
(
$pathRegex
,
$request
,
$matches
))
{
$route
->
bind
Parameters
(
array_slice
(
$matches
,
1
));
$route
->
set
Parameters
(
array_slice
(
$matches
,
1
));
return
$route
;
}
...
...
qa-src/helpers.php
deleted
100644 → 0
View file @
eacf34fc
<?php
use
Q2A\App\Application
;
if
(
!
function_exists
(
'app'
))
{
/**
* Helper function to access the application object.
* If the $key parameter is null the application instance is returned.
* If the $key parameter is set and the $object parameter is null the container is called to resolve the $key.
* If the $key and the $object parameters are null the container is called to bind the $object to the $key.
* @param mixed $key
* @param mixed $object
* @return mixed
*/
function
app
(
$key
=
null
,
$object
=
null
)
{
if
(
is_null
(
$key
))
{
return
Application
::
getInstance
();
}
if
(
is_null
(
$object
))
{
return
Application
::
getInstance
()
->
getContainer
()
->
resolve
(
$key
);
}
Application
::
getInstance
()
->
getContainer
()
->
bind
(
$key
,
$object
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment