Tidy.php

Go to the documentation of this file.
00001 <?php
00002 
00034 class Robo47_Controller_Plugin_Tidy extends Zend_Controller_Plugin_Abstract
00035 {
00036 
00042     protected $_tidyFilter = null;
00043 
00051     public function __construct(Robo47_Filter_Tidy $tidyFilter = null,
00052         Zend_Log $log = null,
00053         $logPriority = Zend_Log::INFO,
00054         $logCategory = 'tidy')
00055     {
00056         $this->setTidyFilter($tidyFilter);
00057         $this->setLog($log);
00058         $this->setLogPriority($logPriority);
00059         $this->setLogCategory($logCategory);
00060     }
00061 
00068     public function setTidyFilter(Robo47_Filter_Tidy $tidyFilter = null)
00069     {
00070         if (null === $tidyFilter) {
00071             $tidyFilter = new Robo47_Filter_Tidy();
00072         }
00073         $this->_tidyFilter = $tidyFilter;
00074         return $this;
00075     }
00076 
00082     public function getTidyFilter()
00083     {
00084         return $this->_tidyFilter;
00085     }
00086 
00093     public function setLog(Zend_Log $log = null)
00094     {
00095         $this->_log = $log;
00096         return $this;
00097     }
00098 
00104     public function getLog()
00105     {
00106         return $this->_log;
00107     }
00108 
00115     public function setLogCategory($logCategory)
00116     {
00117         $this->_logCategory = $logCategory;
00118         return $this;
00119     }
00120 
00126     public function getLogCategory()
00127     {
00128         return $this->_logCategory;
00129     }
00130 
00137     public function setLogPriority($logPriority)
00138     {
00139         $this->_logPriority = (int) $logPriority;
00140         return $this;
00141     }
00142 
00148     public function getLogPriority()
00149     {
00150         return $this->_logPriority;
00151     }
00152 
00161     public function isHtmlResponse(Zend_Controller_Response_Abstract $response)
00162     {
00163         foreach ($response->getHeaders() as $value) {
00164             if ('content-type' == trim(strtolower($value['name'])) &&
00165                 false !== strpos(strtolower($value['value']), 'text/html')) {
00166                 return true;
00167             }
00168 
00169         }
00170 
00171         foreach ($response->getRawHeaders() as $value) {
00172             $regex = preg_quote('content-type: text/html', '/');
00173             if (preg_match('/' . $regex . '/i', $value)) {
00174                 return true;
00175             }
00176         }
00177 
00178         return false;
00179     }
00180 
00184     public function dispatchLoopShutdown()
00185     {
00186         $response = $this->getResponse();
00187         // only tidy content recognized als text/html
00188         if ($this->isHtmlResponse($response)) {
00189             $tidyFilter = $this->getTidyFilter();
00190             // filter body and set it as new body again
00191             $response->setBody($tidyFilter->filter($response->getBody()));
00192             if (null !== $this->_log) {
00193                 $tidy = $tidyFilter->getTidy();
00194                 $tidy->diagnose();
00195                 // log only if errors / warnings found
00196                 $pos = strpos(
00197                     $tidy->errorBuffer,
00198                     'No warnings or errors were found'
00199                 );
00200                 if (false === $pos) {
00201                     $this->_log->log(
00202                         'Url: ' . $this->getRequest()->getRequestUri() .
00203                         PHP_EOL . $tidy->errorBuffer,
00204                         $this->getLogPriority(),
00205                         array('category' => $this->getLogCategory())
00206                     );
00207                 }
00208             }
00209         }
00210     }
00211 }