Skip to content

Commit 5c934bd

Browse files
committedJan 26, 2015
Started migrating core classes from Core
1 parent be28d70 commit 5c934bd

30 files changed

+2496
-26
lines changed
 

‎LICENSE

-26
Original file line numberDiff line numberDiff line change
@@ -174,29 +174,3 @@ Apache License
174174
of your accepting any such warranty or additional liability.
175175

176176
END OF TERMS AND CONDITIONS
177-
178-
APPENDIX: How to apply the Apache License to your work.
179-
180-
To apply the Apache License to your work, attach the following
181-
boilerplate notice, with the fields enclosed by brackets "{}"
182-
replaced with your own identifying information. (Don't include
183-
the brackets!) The text should be enclosed in the appropriate
184-
comment syntax for the file format. We also recommend that a
185-
file or class name and description of purpose be included on the
186-
same "printed page" as the copyright notice for easier
187-
identification within third-party archives.
188-
189-
Copyright {yyyy} {name of copyright owner}
190-
191-
Licensed under the Apache License, Version 2.0 (the "License");
192-
you may not use this file except in compliance with the License.
193-
You may obtain a copy of the License at
194-
195-
http://www.apache.org/licenses/LICENSE-2.0
196-
197-
Unless required by applicable law or agreed to in writing, software
198-
distributed under the License is distributed on an "AS IS" BASIS,
199-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200-
See the License for the specific language governing permissions and
201-
limitations under the License.
202-

‎platform/boot.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2015 RhubarbPHP
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
use Rhubarb\Crown\Exceptions\Handlers\ExceptionHandler;
20+
use Rhubarb\Crown\Module;
21+
22+
error_reporting(E_ALL | E_STRICT);
23+
24+
// As we preform our own exception handling we need to stop fatal errors from showing stack traces.
25+
ini_set("display_errors", "off");
26+
27+
/**
28+
* Set's up the working environment to provide a consistant and predictable ecosystem for user code
29+
*/
30+
31+
// Change the working directory to the top level folder.
32+
chdir(__DIR__ . "/../../../");
33+
34+
// Initially we don't have an auto loader as this is handled by the modules. We need to load this first
35+
// module 'core' so that we have an auto loader for subsequent modules. There are also some other classes
36+
// that might be needed by this booting script so we load them aswell.
37+
38+
include(__DIR__ . "/../src/Module.php");
39+
include(__DIR__ . "/../src/Exceptions/ImplementationException.php");
40+
include(__DIR__ . "/../src/Exceptions/Handlers/ExceptionHandler.php");
41+
42+
// Register to handle exceptions and PHP errors. However we don't do this if we are unit testing. It's
43+
// best to let the exceptions report unhindered to phpunit.
44+
if (!isset($unitTesting) || !$unitTesting) {
45+
ExceptionHandler::EnableExceptionTrapping();
46+
}
47+
48+
$appName = "app";
49+
50+
// Is there an app environment setting? This allows the same project to serve multiple solutions
51+
// with one code base (e.g. tenant and landlord together). This is very rare in production systems, however
52+
// for the initial project phase this can be very useful.
53+
if ($envAppSetting = getenv("core_app")) {
54+
$appName .= "-" . $envAppSetting;
55+
}
56+
57+
if (file_exists("settings/" . $appName . ".config.php")) {
58+
include("settings/" . $appName . ".config.php");
59+
}
60+
61+
// Now auto loaders are in place we can initialise the modules properly.
62+
Module::InitialiseModules();

‎platform/command-line-bootstrap.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2015 RhubarbPHP
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* A bootstrapper to setup the Rhubarb platform when running scripts from a terminal
21+
*/
22+
23+
include_once( "boot.php" );
24+
25+
// Disable exception trapping as there will be no valid URL handler able to return a sensible
26+
// interpretation of the exception details. CLI scripts are never seen publicly so it is more
27+
// useful to have the real exception text and isn't a security risk.
28+
\Rhubarb\Crown\Exceptions\Handlers\ExceptionHandler::DisableExceptionTrapping();
29+
30+
if ( isset( $argv[1] ) )
31+
{
32+
$script = $argv[1];
33+
include( $script );
34+
}

‎platform/execute.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2015 RhubarbPHP
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
20+
/**
21+
* execute.php is the entry point for all HTTP requests for Rhubarb applications.
22+
* The only exceptions to this are when webserver URL rewriting goes directly to
23+
* a resource for performance reasons, e.g. accessing static content like images
24+
* and CSS files.
25+
*/
26+
27+
use Rhubarb\Crown\Logging\Log;
28+
use Rhubarb\Crown\Module;
29+
30+
// Initiate our bootstrap script to boot all libraries required.
31+
include("boot.php");
32+
33+
$request = \Rhubarb\Crown\Context::currentRequest();
34+
35+
try {
36+
// Pass control to the Module class and ask it to generate a response for the
37+
// incoming request.
38+
$response = Module::generateResponseForRequest($request);
39+
$response->send();
40+
} catch (\Exception $er) {
41+
$context = new \Rhubarb\Crown\Context();
42+
43+
if ($context->DeveloperMode) {
44+
Log::error($er->getMessage(), "ERROR");
45+
46+
print "<pre>Exception: " . get_class($er) . "
47+
Message: " . $er->getMessage() . "
48+
Stack Trace:
49+
" . $er->getTraceAsString();
50+
51+
}
52+
}
53+
54+
Log::debug("Request Complete", "ROUTER");

‎platform/phpunit-bootstrap.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2015 RhubarbPHP
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* A bootstrapper to setup the Rhubarb platform when running unit tests
21+
*/
22+
23+
global $unitTesting;
24+
25+
$unitTesting = true;
26+
27+
include_once( "boot.php" );
28+
29+
if ( isset( $argv[1] ) )
30+
{
31+
$script = $argv[1];
32+
include( $script );
33+
}

‎src/Context.php

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2015 RhubarbPHP
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace Rhubarb\Crown;
20+
21+
require_once __DIR__."/Settings.php";
22+
23+
use \Gcd\Core\Request;
24+
use Gcd\Core\UrlHandlers\UrlHandler;
25+
26+
/**
27+
* A class providing the rest of the platform some contextual information
28+
*
29+
* @property bool $UnitTesting
30+
* @property bool $IsAjaxRequest
31+
* @property bool $IsCliInvocation
32+
* @property bool $Live True to indicate this is a live production server
33+
* @property bool $DeveloperMode True to enable developer only functionality
34+
* @property bool $SimulateNonCli True to pretend that the request is not a CLI request (even if it is - used by unit testing)
35+
* @property mixed $SimulatedRequestBody For unit testing - simulates the request body instead of using php://input
36+
* @property UrlHandler $UrlHandler The URL handler currently generating the response
37+
*
38+
* @author acuthbert
39+
* @copyright GCD Technologies 2012
40+
*/
41+
class Context extends Settings
42+
{
43+
protected function InitialiseDefaultValues()
44+
{
45+
global $unitTesting;
46+
47+
parent::InitialiseDefaultValues();
48+
49+
// $unitTesting is set in phpunit-bootstrap.php
50+
$this->UnitTesting = ( isset( $unitTesting ) && $unitTesting ) ? true : false;
51+
$this->DeveloperMode = false;
52+
$this->Live = false;
53+
}
54+
55+
public function GetIsAjaxRequest()
56+
{
57+
if ( isset( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) && strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) == 'xmlhttprequest')
58+
{
59+
return true;
60+
}
61+
62+
return false;
63+
}
64+
65+
/**
66+
* Check if the script was invoked via PHP's CLI
67+
*
68+
* @return bool
69+
*/
70+
public function GetIsCliInvocation()
71+
{
72+
if ( $this->SimulateNonCli )
73+
{
74+
return false;
75+
}
76+
77+
return 'cli' === php_sapi_name();
78+
}
79+
80+
/**
81+
* A static accessor for the Request property
82+
*
83+
* @return \Gcd\Core\Request\Request The current Request
84+
*/
85+
public static function CurrentRequest()
86+
{
87+
$contextInstance = new static();
88+
89+
return $contextInstance->Request;
90+
}
91+
92+
/**
93+
* Lazily initialise and then return the current Request.
94+
*
95+
* @return \Gcd\Core\Request\Request
96+
*/
97+
public function GetRequest()
98+
{
99+
if ( !isset( $this->modelData[ 'Request'] ) )
100+
{
101+
if ( $this->IsCliInvocation )
102+
{
103+
$request = new Request\CliRequest();
104+
}
105+
else
106+
{
107+
$contentType = ( isset( $_SERVER[ "CONTENT_TYPE"] ) ) ? strtolower( $_SERVER[ "CONTENT_TYPE" ] ) : "";
108+
109+
switch( $contentType )
110+
{
111+
case "application/json":
112+
$request = new Request\JsonRequest();
113+
break;
114+
default:
115+
$request = new Request\WebRequest();
116+
break;
117+
}
118+
}
119+
120+
$this->modelData[ 'Request' ] = $request;
121+
}
122+
123+
return $this->modelData[ 'Request' ];
124+
}
125+
126+
/**
127+
* Returns the body of the request.
128+
*
129+
* This is not automatically passed to the request as this might be an expensive operation
130+
* that may never actually get used (e.g. a File upload)
131+
*
132+
* @return bool|mixed|string
133+
*/
134+
public function GetRequestBody()
135+
{
136+
if ( $this->UnitTesting )
137+
{
138+
return $this->SimulatedRequestBody;
139+
}
140+
141+
$requestBody = file_get_contents( "php://input" );
142+
143+
return $requestBody;
144+
}
145+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2015 RhubarbPHP
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace Rhubarb\Crown\Exceptions;
20+
21+
/**
22+
* Thrown when an attempt is made to modify a read-only property.
23+
*/
24+
class AttemptToModifyReadOnlyPropertyException extends RhubarbException
25+
{
26+
public function __construct($message = "")
27+
{
28+
parent::__construct($message);
29+
}
30+
}

0 commit comments

Comments
 (0)
Please sign in to comment.