Skip to content

Commit

Permalink
Added reponse object as property of View class View::$response. Refac…
Browse files Browse the repository at this point in the history
…tored HtmlHelper::media() a bit.
  • Loading branch information
ADmad committed Feb 6, 2012
1 parent c572756 commit c0690a3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 35 deletions.
1 change: 1 addition & 0 deletions lib/Cake/Test/Case/Utility/DebuggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ public function testExportVar() {
hasRendered => false
uuids => array()
request => null
response => object(CakeResponse) {}
elementCache => 'default'
int => (int) 2
float => (float) 1.333
Expand Down
51 changes: 28 additions & 23 deletions lib/Cake/View/Helper/HtmlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
*/
class HtmlHelper extends AppHelper {

/**
* Reference to the Response object
*
* @var CakeResponse
*/
public $response;

/**
* html tags used by this helper.
*
Expand Down Expand Up @@ -174,6 +181,11 @@ class HtmlHelper extends AppHelper {
*/
public function __construct(View $View, $settings = array()) {
parent::__construct($View, $settings);
if (is_object($this->_View->response)) {
$this->response = $this->_View->response;
} else {
$this->response = new CakeResponse(array('charset' => Configure::read('App.encoding')));
}
if (!empty($settings['configFile'])) {
$this->loadConfig($settings['configFile']);
}
Expand Down Expand Up @@ -1029,45 +1041,38 @@ public function media($path, $options = array()) {
}

if (is_array($path)) {
$response = null;
$sourceTags = '';
foreach ($path as $source) {
foreach ($path as &$source) {
if (is_string($source)) {
$source = array(
'src' => $source,
);
}
if (!isset($source['type'])) {
if ($response === null) {
$response = new CakeResponse();
}
$ext = pathinfo($source['src'], PATHINFO_EXTENSION);
$source['type'] = $response->getMimeType($ext);
}
if ($type === null) {
if (preg_match('#^video/#', $source['type'])) {
$type = 'video';
} else {
$type = 'audio';
}
$source['type'] = $this->response->getMimeType($ext);
}
$source['src'] = $this->assetUrl($source['src'], $options);
$sourceTags .= $this->useTag('tagselfclosing', 'source', $source);
}
unset($source);
$options['text'] = $sourceTags . $options['text'];
unset($options['fullBase']);
} else {
if ($type === null) {
$response = new CakeResponse();
$mimeType = $response->getMimeType(pathinfo($path, PATHINFO_EXTENSION));
if (preg_match('#^video/#', $mimeType)) {
$type = 'video';
} else {
$type = 'audio';
}
$options['src'] = $this->assetUrl($path, $options);
}

if ($type === null) {
if (is_array($path)) {
$mimeType = $path[0]['type'];
} else {
$mimeType = $this->response->getMimeType(pathinfo($path, PATHINFO_EXTENSION));
}
if (preg_match('#^video/#', $mimeType)) {
$type = 'video';
} else {
$type = 'audio';
}
$path = $this->assetUrl($path, $options);
$options['src'] = $path;
}

if (isset($options['poster'])) {
Expand Down
12 changes: 0 additions & 12 deletions lib/Cake/View/MediaView.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,13 @@ class MediaView extends View {
*/
protected $_compressionEnabled = false;

/**
* Reference to the Response object responsible for sending the headers
*
* @var CakeResponse
*/
public $response = null;

/**
* Constructor
*
* @param Controller $controller The controller with viewVars
*/
public function __construct($controller = null) {
parent::__construct($controller);
if (is_object($controller) && isset($controller->response)) {
$this->response = $controller->response;
} else {
$this->response = new CakeResponse;
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions lib/Cake/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
App::uses('ViewBlock', 'View');
App::uses('CakeEvent', 'Event');
App::uses('CakeEventManager', 'Event');
App::uses('CakeResponse', 'Network');

/**
* View, the V in the MVC triad. View interacts with Helpers and view variables passed
Expand Down Expand Up @@ -201,6 +202,13 @@ class View extends Object {
*/
public $request;

/**
* Reference to the Response object
*
* @var CakeResponse
*/
public $response;

/**
* The Cache configuration View will use to store cached elements. Changing this will change
* the default configuration elements are stored under. You can also choose a cache config
Expand Down Expand Up @@ -306,6 +314,11 @@ public function __construct($controller) {
}
$this->_eventManager = $controller->getEventManager();
}
if (is_object($controller) && isset($controller->response)) {
$this->response = $controller->response;
} else {
$this->response = new CakeResponse(array('charset' => Configure::read('App.encoding')));
}
$this->Helpers = new HelperCollection($this);
$this->Blocks = new ViewBlock();
parent::__construct();
Expand Down

0 comments on commit c0690a3

Please sign in to comment.