zoukankan      html  css  js  c++  java
  • yii2框架随笔9

    今天我们继续来挖Application.php

     /**
         * Sets the directory that stores vendor files.
       * 设置存储厂商文件的目录。 * @param string $path the directory that stores vendor files.
    */ public function setVendorPath($path) { // 获取vendor的路径,并存到_vendorPath中 $this->_vendorPath = Yii::getAlias($path); // 设置@vendor的alias Yii::setAlias('@vendor', $this->_vendorPath); // 设置@bower的alias Yii::setAlias('@bower', $this->_vendorPath . DIRECTORY_SEPARATOR . 'bower'); // 设置@npm的alias Yii::setAlias('@npm', $this->_vendorPath . DIRECTORY_SEPARATOR . 'npm'); } /** * Returns the time zone used by this application.
       *返回此应用程序使用的时间。 * This is a simple wrapper of PHP function date_default_timezone_get().
       *这是PHP函数date_default_timezone_set()的一个简单的包装。 * If time zone is not configured in php.ini or application config,
       * 如果时间时区不在php.ini或者application confg里面配置, * it will be set to UTC by default.
       *它将被默认设置为UTC。 * @return string the time zone used by this application. * @see
    http://php.net/manual/en/function.date-default-timezone-get.php */ public function getTimeZone() { // date_default_timezone_get — 取得一个脚本中所有日期时间函数所使用的默认时区 return date_default_timezone_get(); } /** * Sets the time zone used by this application.
       *设置此应用程序使用的时区。 * This is a simple wrapper of PHP function date_default_timezone_set().
       *这是PHP函数date_default_timezone_set()的一个简单的包装。 * Refer to the [php manual](
    http://www.php.net/manual/en/timezones.php) for available timezones. * @param string $value the time zone used by this application. * @see http://php.net/manual/en/function.date-default-timezone-set.php */ public function setTimeZone($value) { // date_default_timezone_set — 设定用于一个脚本中所有日期时间函数的默认时区 date_default_timezone_set($value); }

    下面我们来看一下ArrayableTrait.php

    <?php
    /**
     * @link http://www.yiiframework.com/
     * @copyright Copyright (c) 2008 Yii Software LLC
     * @license http://www.yiiframework.com/license/
     */
    namespace yiibase;
    use Yii;
    use yiihelpersArrayHelper;
    use yiiwebLink;
    use yiiwebLinkable;
    /**
     * ArrayableTrait provides a common implementation of the [[Arrayable]] interface.
     *ArrayableTrait提供[Arrayable]接口的实现。
     * ArrayableTrait implements [[toArray()]] by respecting the field definitions as declared
     * in [[fields()]] and [[extraFields()]].
     *
     * @author Qiang Xue <qiang.xue@gmail.com>
     * @since 2.0
     */
    trait ArrayableTrait
    {
    
        /** In this method, you may also want to return different lists of fields based on some context
         * information. For example, depending on the privilege of the current application user,
         * you may return different sets of visible fields or filter out some fields.
         *在这个方法中,你可能还需要返回基于一些背景场不同的列表信息。例如,根据当前的应用程序的用户的特权
       *您可能会返回不同组可见字段或过滤掉某些字段。
    * The default implementation of this method returns the public object member variables indexed by themselves. *此方法的默认实现返回自己索引的公众对象的成员变量 * @return array the list of field names or field definitions. * @see toArray() */ public function fields() { // 获取该对象的 public 成员变量的名列表,赋给 $fields $fields = array_keys(Yii::getObjectVars($this)); // array_combine — 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值 // 返回数组, keys 和 values 都是 $fields return array_combine($fields, $fields); } /** * Converts the model into an array. *该模型转换成一个数组。 * * If the model implements the [[Linkable]] interface, the resulting array will also have a `_link` element * which refers to a list of links as specified by the interface. *如果模型实现了[可链接]界面,由此产生的阵列也将有一个`link`元素,它由接口指定指链接列表。 * @param array $fields the fields being requested. If empty, all fields as specified by [[fields()]] will be returned. * @param array $expand the additional fields being requested for exporting. Only fields declared in [[extraFields()]] * will be considered. * @param boolean $recursive whether to recursively return array representation of embedded objects. * @return array the array representation of the object */ public function toArray(array $fields = [], array $expand = [], $recursive = true) { $data = []; foreach ($this->resolveFields($fields, $expand) as $field => $definition) { // 如果是 string, 就返回当前对象的该属性, 否则调用 call_user_func 去执行 $definition 函数 $data[$field] = is_string($definition) ? $this->$definition : call_user_func($definition, $this, $field); } if ($this instanceof Linkable) { $data['_links'] = Link::serialize($this->getLinks()); } return $recursive ? ArrayHelper::toArray($data) : $data; } /** * Determines which fields can be returned by [[toArray()]]. * 决定哪些 fields 会通过 toArray() 返回 * This method will check the requested fields against those declared in [[fields()]] and [[extraFields()]] * to determine which fields can be returned.
       * 此方法将检查对那些在[[fields()],并宣布必填字段[extraFields()]] 以确定哪些领域可以退换。 * @param array $fields the fields being requested for exporting * @param array $expand the additional fields being requested for exporting * @return array the list of fields to be exported. The array keys are the field names, and the array values * are the corresponding object property names or PHP callables returning the field values.
    */ protected function resolveFields(array $fields, array $expand) { $result = []; // 循环 $this->fields() 中取得的 fields foreach ($this->fields() as $field => $definition) { if (is_integer($field)) { // 如果 $field 是 int, 就将 $definition 赋值给 $field $field = $definition; } if (empty($fields) || in_array($field, $fields, true)) { // 如果 $fields 为空, 或者 $field 在 $fields 中, 就将 $definition 赋到 $result 中 // 即 $fields 为空,就将所有的对象的属性都放入到结果中 // 不为空时,如果当前对象的属性在 $fields 中存在, 就将对象中定义的该属性的值放入到结果中 $result[$field] = $definition; } } if (empty($expand)) { return $result; } // 循环 $this->extraFields() 中取得的 fields foreach ($this->extraFields() as $field => $definition) { if (is_integer($field)) { // 如果 $field 是 int, 就将 $definition 赋值给 $field $field = $definition; } if (in_array($field, $expand, true)) { // 如果$field 在 $expand 中, 就将 $definition 赋到 $result 中 // 即当前对象的扩展属性在 $fields 中存在, 就将对象中定义的该扩展属性的值放入到结果中 $result[$field] = $definition; } } return $result; } }
  • 相关阅读:
    自定义Filter服务
    filter 以及 orderBy的使用
    ng-repeat-start ng-repeat-end 的使用
    ng-repeat 与ng-switch的简单应用
    ng-bind-html 的使用
    Oracle instr用法
    Oracle left查询案例
    Oracle case用法
    mysql 导出导入sql
    Gson解析复杂JSON对象
  • 原文地址:https://www.cnblogs.com/taokai/p/5410305.html
Copyright © 2011-2022 走看看