Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change some properties to protected in CTheme. #1759

Closed
metayii opened this issue Nov 26, 2012 · 9 comments
Closed

Change some properties to protected in CTheme. #1759

metayii opened this issue Nov 26, 2012 · 9 comments

Comments

@metayii
Copy link

metayii commented Nov 26, 2012

Currently CTheme.php has this properties with private access:

private $_name;
private $_basePath;
private $_baseUrl;

I request the Yii team to modify them so they are protected:

protected $_name;
protected $_basePath;
protected $_baseUrl;

In order to use them in any class which extends CTheme and needs access to the properties mentioned above. For instance, the theme name could be changed at any time based on dinamic input or certain conditions.

@metayii
Copy link
Author

metayii commented Nov 26, 2012

@samdark
Copy link
Member

samdark commented Nov 26, 2012

What exactly are you trying to achieve? What this "my own theme handler" would change?

@metayii
Copy link
Author

metayii commented Nov 26, 2012

This is my ATheme class. This is what I try to achieve:

class ATheme extends CTheme
{
   public function setName($value)
   {
      $this->_name = $value;
   }

   /**
   * Finds the view file for the specified controller's view.
   * @param CController $controller the controller
   * @param string $viewName the view name
   * @return string the view file path. False if the file does not exist.
   */
   public function getViewFile($controller, $viewName)
   {
      $moduleViewPath = $this->getViewPath();
      if (($module = $controller->getModule()) !== null) {
         $moduleViewPath = $module->getBasePath() . '/themes/' . $this->getName() . '/views/' . $controller->getId();
      }
      return $controller->resolveViewFile($viewName, $this->getViewPath().'/'.$controller->getUniqueId(), $this->getViewPath(), $moduleViewPath);
   }

    /**
     * Finds the layout file for the specified controller's layout.
     * @param CController $controller the controller
     * @param string $layoutName the layout name
     * @return string the layout file path. False if the file does not exist.
     */
    public function getLayoutFile($controller, $layoutName)
    {
        $moduleViewPath = $basePath=$this->getViewPath();
        $module = $controller->getModule();
        if (empty($layoutName)) {
            while ($module !== null) {
                if($module->layout === false)
                    return false;
                if(!empty($module->layout))
                    break;
                $module = $module->getParentModule();
            }
            if($module === null) {
                $layoutName = Yii::app()->layout;
         }
            else {
                $layoutName = $module->layout;
            $moduleViewPath = $module->getBasePath() . '/themes/' . $this->getName() . '/views';
            }
        }
        else if($module !== null) {
         $moduleViewPath = $module->getBasePath() . '/themes/' . $this->getName() . '/views';
      }

        return $controller->resolveViewFile($layoutName, $moduleViewPath.'/layouts', $basePath, $moduleViewPath);
    }
}

@fiesh
Copy link

fiesh commented Nov 26, 2012

I would like to extend this request to all private properties from CDbCommand. If they were private, I could easily circumvent http://www.yiiframework.com/forum/index.php/topic/37911-call-of-yiilog-in-cdbcommandexecute/

Like it is right now, it's next to impossible.

@metayii
Copy link
Author

metayii commented Nov 26, 2012

flesh: you mean "if they were protected" I think :)

@fiesh
Copy link

fiesh commented Nov 26, 2012

Oh, haha, thanks yes ;)

@cebe
Copy link
Member

cebe commented Nov 28, 2012

There is a good reason for many framework properties to be private. This allows to change code behind the api without breaking behavior of a class. In most cases there are getters and setters that allow you to access the private properties.
We will not accept a request for making all properties protected. When there is a special case and you have a good reason for making one property protected we might accept it.

You might also want to read this: http://fabien.potencier.org/article/47/pragmatism-over-theory-protected-vs-private

@cebe cebe closed this as completed Nov 28, 2012
@samdark samdark reopened this Nov 28, 2012
@samdark
Copy link
Member

samdark commented Nov 28, 2012

@cebe this time it's a specific request.

@qiangxue
Copy link
Member

If you want to make the theme name writable, you can overwrite getName() which returns your own $theme variable, and you then add setName() which sets your $theme variable.

Yes, it looks cumbersome, but it is because the name property is read-only by design and you are trying to break this limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants