Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch '3.1/develop' into 3.1/release/3.1.0

  • Loading branch information...
commit 506a8c3986aa232682a0c560d5ab731059e921b5 2 parents a132fd6 + a85fc0a
@zombor zombor authored
View
5 classes/kohana/http/exception.php
@@ -8,11 +8,6 @@ class Kohana_Http_Exception extends Kohana_Exception {
protected $_code = 0;
/**
- * @var string file of view to use for http exeception
- */
- protected $_http_view = 'kohana/http/error';
-
- /**
* Creates a new translated exception.
*
* throw new Kohana_Exception('Something went terrible wrong, :user',
View
49 classes/kohana/request/client/external.php
@@ -146,6 +146,35 @@ public function execute(Request $request)
}
/**
+ * Set and get options for this request.
+ *
+ * @param mixed $key Option name, or array of options
+ * @param mixed $value Option value
+ * @return mixed
+ * @return Request_Client_External
+ */
+ public function options($key = NULL, $value = NULL)
+ {
+ if ($key === NULL)
+ return $this->_options;
+
+ if (is_array($key))
+ {
+ $this->_options = $key;
+ }
+ elseif ( ! $value)
+ {
+ return Arr::get($this->_options, $key);
+ }
+ else
+ {
+ $this->_options[$key] = $value;
+ }
+
+ return $this;
+ }
+
+ /**
* Execute the request using the PECL HTTP extension. (recommended)
*
* @param Request $request Request to execute
@@ -167,6 +196,9 @@ protected function _http_execute(Request $request)
// Create an http request object
$http_request = new HttpRequest($request->uri(), $http_method_mapping[$request->method()]);
+ // Set custom options
+ $http_request->setOptions($this->_options);
+
// Set headers
$http_request->setHeaders($request->headers()->getArrayCopy());
@@ -216,19 +248,8 @@ protected function _curl_execute(Request $request)
// Reset the headers
Request_Client_External::$_processed_headers = array();
- // Load the default remote settings
- $defaults = Kohana::config('remote')->as_array();
-
- if ( ! $this->_options)
- {
- // Use default options
- $options = $defaults;
- }
- else
- {
- // Add default options
- $options = $options + $defaults;
- }
+ // Allow custom options to be set
+ $options = $this->_options;
// Set the request method
$options[CURLOPT_CUSTOMREQUEST] = $request->method();
@@ -318,6 +339,8 @@ protected function _native_execute(Request $request)
// Create the context stream
$context = stream_context_create($options);
+ stream_context_set_option($context, $this->_options);
+
$stream = fopen($request->uri(), $mode, FALSE, $context);
$meta_data = stream_get_meta_data($stream);
View
18 classes/kohana/validation.php
@@ -287,9 +287,19 @@ public function check()
}
}
- if (is_array($rule) OR ! is_string($rule))
+ // Default the error name to be the rule (except array and lambda rules)
+ $error_name = $rule;
+
+ if (is_array($rule))
+ {
+ // This is an array callback, the method name is the error name
+ $error_name = $rule[1];
+ $passed = call_user_func_array($rule, $params);
+ }
+ elseif ( ! is_string($rule))
{
- // This is either a callback as an array or a lambda
+ // This is a lambda function, there is no error name (errors must be added manually)
+ $error_name = FALSE;
$passed = call_user_func_array($rule, $params);
}
elseif (method_exists('Valid', $rule))
@@ -324,10 +334,10 @@ public function check()
if ( ! in_array($rule, $this->_empty_rules) AND ! Valid::not_empty($value))
continue;
- if ($passed === FALSE)
+ if ($passed === FALSE AND $error_name !== FALSE)
{
// Add the rule to the errors
- $this->error($field, $rule, $params);
+ $this->error($field, $error_name, $params);
// This field has an error, stop executing rules
break;
View
61 guide/kohana/tutorials/error-pages.md
@@ -1,11 +1,11 @@
# Friendly Error Pages
-By default Kohana 3 doesn't have a method to display friendly error pages like that
+By default Kohana 3 doesn't have a method to display friendly error pages like that
seen in Kohana 2; In this short guide you will learn how it is done.
-## Prerequisites
+## Prerequisites
-You will need `'errors' => TRUE` passed to `Kohana::init`. This will convert PHP
+You will need `'errors' => TRUE` passed to `Kohana::init`. This will convert PHP
errors into exceptions which are easier to handle.
## 1. An Improved Exception Handler
@@ -13,32 +13,47 @@ errors into exceptions which are easier to handle.
Our custom exception handler is self explanatory.
class Kohana_Exception extends Kohana_Kohana_Exception {
+
public static function handler(Exception $e)
{
if (Kohana::DEVELOPMENT === Kohana::$environment)
{
- Kohana_Core::exception_handler($e);
+ parent::handler($e);
}
else
{
- Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));
+ try
+ {
+ Kohana::$log->add(Log::ERROR, parent::text($e));
- $attributes = array
- (
- 'action' => 500,
- 'message' => rawurlencode($e->getMessage())
- );
+ $attributes = array
+ (
+ 'action' => 500,
+ 'message' => rawurlencode($e->getMessage())
+ );
- if ($e instanceof Http_Exception)
- {
- $attributes['action'] = $e->getCode();
- }
+ if ($e instanceof Http_Exception)
+ {
+ $attributes['action'] = $e->getCode();
+ }
- // Error sub-request.
- echo Request::factory(Route::url('error', $attributes))
+ // Error sub-request.
+ echo Request::factory(Route::url('error', $attributes))
->execute()
->send_headers()
- ->response;
+ ->body();
+ }
+ catch (Exception $e)
+ {
+ // Clean the output buffer if one exists
+ ob_get_level() and ob_clean();
+
+ // Display the exception text
+ echo parent::text($e);
+
+ // Exit with an error status
+ exit(1);
+ }
}
}
}
@@ -91,13 +106,13 @@ would display an error 500 page.
{
$this->request->action(404);
}
-
+
$this->response->status((int) $this->request->action());
}
-1. Set a template variable "page" so the user can see what they requested. This
+1. Set a template variable "page" so the user can see what they requested. This
is for display purposes only.
-2. If an internal request, then set a template variable "message" to be shown to
+2. If an internal request, then set a template variable "message" to be shown to
the user.
3. Otherwise use the 404 action. Users could otherwise craft their own error messages, eg:
`error/404/email%20your%20login%20information%20to%20hacker%40google.com`
@@ -106,7 +121,7 @@ would display an error 500 page.
public function action_404()
{
$this->template->title = '404 Not Found';
-
+
// Here we check to see if a 404 came from our website. This allows the
// webmaster to find broken links and update them in a shorter amount of time.
if (isset ($_SERVER['HTTP_REFERER']) AND strstr($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME']) !== FALSE)
@@ -114,7 +129,7 @@ would display an error 500 page.
// Set a local flag so we can display different messages in our template.
$this->template->local = TRUE;
}
-
+
// HTTP Status code.
$this->response->status(404);
}
@@ -129,7 +144,7 @@ would display an error 500 page.
$this->template->title = 'Internal Server Error';
}
-You will notice that each example method is named after the HTTP response code
+You will notice that each example method is named after the HTTP response code
and sets the request response code.
## 4. Conclusion
View
8 tests/kohana/ValidationTest.php
@@ -235,6 +235,14 @@ public function provider_check()
FALSE,
array('foo' => '1.foo.is_string'),
),
+ // Test array rules use method as error name
+ array(
+ array('foo' => 'test'),
+ array('foo' => array(array(array('Valid', 'min_length'), array(':value', 10)))),
+ array(),
+ FALSE,
+ array('foo' => 'foo must be at least 10 characters long'),
+ ),
);
}
Please sign in to comment.
Something went wrong with that request. Please try again.