Kaynağa Gözat

Promoted Zend_View_Helper_BaseUrl to trunk

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@16579 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 16 yıl önce
ebeveyn
işleme
cdb489d050

+ 46 - 0
documentation/manual/en/module_specs/Zend_View-Helpers-BaseUrl.xml

@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Reviewed: no -->
+<sect3 id="zend.view.helpers.initial.baseurl">
+    <title>BaseUrl Helper</title>
+
+    <para>
+        While most URLs generated by the framework have the base URL 
+        prepended automatically, developers will need to prepend the
+        base URL to their own URLs in order for paths to resources to 
+        be correct.
+    </para>
+
+    <para>
+        Usage of the BaseUrl helper is very straightforward:
+    </para>
+
+    <programlisting role="php"><![CDATA[
+/*
+ * The following assume that the base URL of the page/application is "/mypage".
+ */
+
+/*
+ * Prints: 
+ * <base href="/mypage/" />
+ */
+<base href="<?php echo $this->baseUrl(); ?>" /> 
+
+/*
+ * Prints:
+ * <link rel="stylesheet" type="text/css" href="/mypage/css/base.css" />
+ */
+<link rel="stylesheet" type="text/css" 
+     href="<?php echo $this->baseUrl('css/base.css'); ?>" /> 
+]]></programlisting>
+
+    <note>
+        <para>
+            For simplicity's sake, we strip out the entry PHP file (e.g.,
+            "index.php") from the base URL that was contained in
+            <classname>Zend_Controller</classname>. However, in some situations
+            this may cause a problem. If one occurs, use
+            <code>$this->getHelper('BaseUrl')->setBaseUrl()</code> to set your
+            own BaseUrl.
+        </para>
+    </note>
+</sect3>

+ 1 - 0
documentation/manual/en/module_specs/Zend_View-Helpers.xml

@@ -352,6 +352,7 @@ echo $this->formCheckbox('foo',
 ]]></programlisting>
 
         <xi:include href="Zend_View-Helpers-Action.xml" />
+        <xi:include href="Zend_View-Helpers-BaseUrl.xml" />
         <xi:include href="Zend_View-Helpers-Cycle.xml" />
         <xi:include href="Zend_View-Helpers-Partial.xml" />
         <xi:include href="Zend_View-Helpers-Placeholder.xml" />

+ 116 - 0
library/Zend/View/Helper/BaseUrl.php

@@ -0,0 +1,116 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_View
+ * @subpackage Helper
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @version    $Id:$
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+/** @see Zend_View_Helper_Abstract */
+require_once 'Zend/View/Helper/Abstract.php';
+
+/**
+ * Helper for retrieving the BaseUrl
+ *
+ * @package    Zend_View
+ * @subpackage Helper
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_View_Helper_BaseUrl extends Zend_View_Helper_Abstract
+{
+    /**
+     * BaseUrl
+     *
+     * @var string
+     */
+    protected $_baseUrl;
+
+    /**
+     * Returns site's base url, or file with base url prepended
+     *
+     * $file is appended to the base url for simplicity
+     *
+     * @param  string|null $file
+     * @return string
+     */
+    public function baseUrl($file = null)
+    {
+        // Get baseUrl
+        $baseUrl = $this->getBaseUrl();
+
+        // Remove trailing slashes
+        if (null !== $file) {
+            $file = '/' . ltrim($file, '/\\');
+        }
+
+        return $baseUrl . $file;
+    }
+
+    /**
+     * Set BaseUrl
+     *
+     * @param  string $base
+     * @return Zend_View_Helper_BaseUrl
+     */
+    public function setBaseUrl($base)
+    {
+        $this->_baseUrl = rtrim($base, '/\\');
+        return $this;
+    }
+
+    /**
+     * Get BaseUrl
+     *
+     * @return string
+     */
+    public function getBaseUrl()
+    {
+        if ($this->_baseUrl === null) {
+            /** @see Zend_Controller_Front */
+            require_once 'Zend/Controller/Front.php';
+            $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
+
+            // Remove scriptname, eg. index.php from baseUrl
+            $baseUrl = $this->_removeScriptName($baseUrl);
+
+            $this->setBaseUrl($baseUrl);
+        }
+
+        return $this->_baseUrl;
+    }
+
+    /**
+     * Remove Script filename from baseurl
+     *
+     * @param  string $url
+     * @return string
+     */
+    protected function _removeScriptName($url)
+    {
+        if (!isset($_SERVER['SCRIPT_NAME'])) {
+            // We can't do much now can we? (Well, we could parse out by ".")
+            return $url;
+        }
+
+        if (($pos = strripos($url, basename($_SERVER['SCRIPT_NAME']))) !== false) {
+            $url = substr($url, 0, $pos);
+        }
+
+        return $url;
+    }
+}

+ 2 - 0
tests/Zend/View/Helper/AllTests.php

@@ -5,6 +5,7 @@ if (!defined('PHPUnit_MAIN_METHOD')) {
 require_once dirname(__FILE__) . '/../../../TestHelper.php';
 
 require_once 'Zend/View/Helper/ActionTest.php';
+require_once 'Zend/View/Helper/BaseUrlTest.php';
 require_once 'Zend/View/Helper/CycleTest.php';
 require_once 'Zend/View/Helper/DeclareVarsTest.php';
 require_once 'Zend/View/Helper/DoctypeTest.php';
@@ -69,6 +70,7 @@ class Zend_View_Helper_AllTests
         $suite = new PHPUnit_Framework_TestSuite('Zend Framework - Zend_View_Helper');
 
         $suite->addTestSuite('Zend_View_Helper_ActionTest');
+        $suite->addTestSuite('Zend_View_Helper_BaseUrlTest');
         $suite->addTestSuite('Zend_View_Helper_CycleTest');
         $suite->addTestSuite('Zend_View_Helper_DeclareVarsTest');
         $suite->addTestSuite('Zend_View_Helper_DoctypeTest');

+ 211 - 0
tests/Zend/View/Helper/BaseUrlTest.php

@@ -0,0 +1,211 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_View
+ * @subpackage Helper
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @version    $Id:$
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+// Call Zend_View_Helper_BaseUrlTest::main() if this source file is executed directly.
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_View_Helper_BaseUrlTest::main');
+}
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+/**
+ * @see Zend_View_Helper_BaseUrl
+ */
+require_once 'Zend/View/Helper/BaseUrl.php';
+
+/**
+ * @see Zend_Controller_Front
+ */
+require_once 'Zend/Controller/Front.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_View
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_View_Helper_BaseUrlTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Previous baseUrl before changing
+     *
+     * @var string
+     */
+    protected $_previousBaseUrl;
+
+    /**
+     * Server backup
+     *
+     * @var array
+     */
+    protected $_server;
+    
+    /**
+     * Main
+     */
+    public static function main()
+    {
+        $suite  = new PHPUnit_Framework_TestSuite("Zend_View_Helper_BaseUrlTest");
+        $result = PHPUnit_TextUI_TestRunner::run($suite);
+    }
+    
+    /**
+     * Prepares the environment before running a test.
+     */
+    protected function setUp()
+    {
+        $this->_previousBaseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
+        $this->_server = $_SERVER;
+    }
+
+    /**
+     * Cleans up the environment after running a test.
+     */
+    protected function tearDown()
+    {
+        Zend_Controller_Front::getInstance()->setBaseUrl($this->_previousBaseUrl);
+        Zend_Controller_Front::getInstance()->resetInstance();
+
+        $_SERVER = $this->_server;
+    }
+
+    /**
+     * Test and make sure base url returned is consistent with the FC
+     *
+     */
+    public function testBaseUrlIsSameAsFrontController()
+    {
+        $baseUrls = array('', '/subdir', '/subdir/', '/sub/sub/dir');
+        foreach ($baseUrls as $baseUrl) {
+            Zend_Controller_Front::getInstance()->setBaseUrl($baseUrl);
+            $helper = new Zend_View_Helper_BaseUrl();
+
+            $this->assertEquals(rtrim($baseUrl, '/\\'), $helper->baseUrl());
+        }
+    }
+
+    /**
+     * Test and make sure if paths given without / prefix are fixed
+     *
+     */
+    public function testBaseUrlIsCorrectingFilePath()
+    {
+        $baseUrls = array(
+            ''             => '/file.js',
+            '/subdir'      => '/subdir/file.js',
+            '/sub/sub/dir' => '/sub/sub/dir/file.js',
+        );
+
+        foreach ($baseUrls as $baseUrl => $val) {
+            Zend_Controller_Front::getInstance()->setBaseUrl($baseUrl);
+            $helper = new Zend_View_Helper_BaseUrl();
+
+            $this->assertEquals($val, $helper->baseUrl('file.js'));
+        }
+    }
+
+    /**
+     * Test and make sure baseUrl appended with file works
+     *
+     */
+    public function testBaseUrlIsAppendedWithFile()
+    {
+        $baseUrls = array(
+            ''             => '/file.js',
+            '/subdir'      => '/subdir/file.js',
+            '/sub/sub/dir' => '/sub/sub/dir/file.js',
+        );
+
+        foreach ($baseUrls as $baseUrl => $val) {
+            Zend_Controller_Front::getInstance()->setBaseUrl($baseUrl);
+            $helper = new Zend_View_Helper_BaseUrl();
+
+            $this->assertEquals($val, $helper->baseUrl('/file.js'));
+        }
+    }
+
+    /**
+     * Test and makes sure that baseUrl appended with path works
+     *
+     */
+    public function testBaseUrlIsAppendedWithPath()
+    {
+        $baseUrls = array(
+            ''             => '/path/bar',
+            '/subdir'      => '/subdir/path/bar',
+            '/sub/sub/dir' => '/sub/sub/dir/path/bar',
+        );
+
+        foreach ($baseUrls as $baseUrl => $val) {
+            Zend_Controller_Front::getInstance()->setBaseUrl($baseUrl);
+            $helper = new Zend_View_Helper_BaseUrl();
+
+            $this->assertEquals($val, $helper->baseUrl('/path/bar'));
+        }
+    }
+
+    /**
+     * Test and makes sure that baseUrl appended with root path
+     *
+     */
+    public function testBaseUrlIsAppendedWithRootPath()
+    {
+        $baseUrls = array(
+            ''     => '/',
+            '/foo' => '/foo/'
+        );
+
+        foreach ($baseUrls as $baseUrl => $val) {
+            Zend_Controller_Front::getInstance()->setBaseUrl($baseUrl);
+            $helper = new Zend_View_Helper_BaseUrl();
+
+            $this->assertEquals($val, $helper->baseUrl('/'));
+        }
+    }
+
+    public function testSetBaseUrlModifiesBaseUrl()
+    {
+        $helper = new Zend_View_Helper_BaseUrl();
+        $helper->setBaseUrl('/myfoo');
+        $this->assertEquals('/myfoo', $helper->getBaseUrl());
+    }
+
+    public function testGetBaseUrlReturnsBaseUrl()
+    {
+        Zend_Controller_Front::getInstance()->setBaseUrl('/mybar');
+        $helper = new Zend_View_Helper_BaseUrl();
+        $this->assertEquals('/mybar', $helper->getBaseUrl());
+    }
+
+    public function testGetBaseUrlReturnsBaseUrlWithoutScriptName()
+    {
+        $_SERVER['SCRIPT_NAME'] = '/foo/bar/bat/mybar/index.php';
+        Zend_Controller_Front::getInstance()->setBaseUrl('/mybar/index.php');
+        $helper = new Zend_View_Helper_BaseUrl();
+        $this->assertEquals('/mybar', $helper->getBaseUrl());
+    }
+}
+
+// Call Zend_View_Helper_BaseUrlTest::main() if this source file is executed directly.
+if (PHPUnit_MAIN_METHOD == 'Zend_View_Helper_BaseUrlTest::main') {
+    Zend_View_Helper_BaseUrlTest::main();
+}