分类 "PHP" 下的文章

相关文章

有使用PHP组件的朋友, 应该会注意到组件里头会有一个文件 composer.json, 它描述了组件的信息: 名称, 描述, 关键词, 作者, GitHub仓库地址...还有它所依赖的子组件, 是 Composer 工作的核心.

拿一个大家都知道的日志组件 monologcomposer.json 为例, 我说下一些比较重要的字段:

{
    "name": "monolog/monolog",
    "description": "Sends your logs to files, sockets, inboxes, databases and various web services",
    "keywords": ["log", "logging", "psr-3"],
    "homepage": "http://github.com/Seldaek/monolog",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "Jordi Boggiano",
            "email": "j.boggiano@seld.be",
            "homepage": "http://seld.be"
        }
    ],
    "require": {
        "php": "^7.0",
        "psr/log": "^1.0.1"
    },
    "require-dev": {
        "phpunit/phpunit": "^5.7",
        "graylog2/gelf-php": "^1.4.2",
        "sentry/sentry": "^0.13",
        "ruflin/elastica": ">=0.90 <3.0",
        "doctrine/couchdb": "~1.0@dev",
        "aws/aws-sdk-php": "^2.4.9 || ^3.0",
        "php-amqplib/php-amqplib": "~2.4",
        "swiftmailer/swiftmailer": "^5.3|^6.0",
        "php-console/php-console": "^3.1.3",
        "jakub-onderka/php-parallel-lint": "^0.9",
        "predis/predis": "^1.1",
        "phpspec/prophecy": "^1.6.1"
    },
    "suggest": {
        "graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server",
        "sentry/sentry": "Allow sending log messages to a Sentry server",
        "doctrine/couchdb": "Allow sending log messages to a CouchDB server",
        "ruflin/elastica": "Allow sending log messages to an Elastic Search server",
        "php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib",
        "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)",
        "ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)",
        "mongodb/mongodb": "Allow sending log messages to a MongoDB server (via library)",
        "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB",
        "rollbar/rollbar": "Allow sending log messages to Rollbar",
        "php-console/php-console": "Allow sending log messages to Google Chrome"
    },
    "autoload": {
        "psr-4": {"Monolog\\": "src/Monolog"}
    },
    "autoload-dev": {
        "psr-4": {"Monolog\\": "tests/Monolog"}
    },
    "provide": {
        "psr/log-implementation": "1.0.0"
    },
    "extra": {
        "branch-alias": {
            "dev-master": "2.0.x-dev"
        }
    },
    "scripts": {
        "test": [
            "parallel-lint . --exclude vendor",
            "phpunit"
        ]
    }
}

安装依赖的时候, 最重要的字段是 name, require 以及 require-dev.

name

该字段标识了组件的名称, 在 所有 的组件中, 它是唯一的;

require

该字段列举出该组件 所需的运行环境 以及 依赖的子组件的版本, 安装该组件时, 会检测运行环境, 并安装该组件的子组件, 以及这些子组件的所有子组件...直到子组件不再依赖任何组件为止;

require-dev

该字段不是必须的, 一般来说不安装里面的依赖, 也是可以用的, 通常都是用来跑单元测试. 依赖的安装同 require 字段.

其他字段对于理解镜像的原理没什么帮助, 有兴趣可以看下 这篇文章.

安装依赖的过程, 其实就是请求服务器, 要求拿到该组件的 composer.json 文件, 然后 JSON 解析, 得到 requirerequire-dev 字段的组件, 一直遍历下去, 根据文件描述的仓库地址 git clone 到本地.

看过我 上一篇文章 的朋友就知道, 文章末尾我们配置了 国内的composer镜像, 用来加速我们安装组件的过程, 它缓存了所有包的 composer.json, 并把仓库的每一个分支源码, 打包为 zip 压缩包, 并结合 cdn 加速.

镜像服务器提供了让我们得到 composer.json 的接口, 我们只需提交一个包名, 还有请求结果的哈希值(是不是很懵逼, 我怎么知道结果的哈希值), 镜像服务器会返回一个 JSON, 它包含了很多 composer.json (至少一个), 这些 composer.json 里面就有我们要找的组件的 composer.json (根据name字段), 也包括了其他包的, 为什么会带有其他包的呢, 我捣鼓了挺多次, 发现是当 require 字段存在时, 它就顺带返回了, 不过也不是绝对的, 可能考虑体积关系, 也不会返回太多.

说了这么多, 看下 psr/log 组件的请求结果吧, 比较长, 它包含了5个包的信息:

hackification/log
mobio/target
notadd/wechat
psr/log
wedeto/log
{
    "packages":{
        "hackification/log":{
            "1.0.2":{
                "name":"hackification/log",
                "description":"Common interface for logging libraries",
                "keywords":[
                    "log",
                    "psr",
                    "psr-3",
                    "hack",
                    "hacklang"
                ],
                "homepage":"https://github.com/hackification/log",
                "version":"1.0.2",
                "version_normalized":"1.0.2.0",
                "license":[
                    "MIT"
                ],
                "authors":[
                    {
                        "name":"PHP-FIG",
                        "homepage":"http://www.php-fig.org/"
                    }
                ],
                "source":{
                    "type":"git",
                    "url":"https://github.com/hackification/log.git",
                    "reference":"75b02e14bd8e5b8ded75de91d7eb9df6046f7fa7"
                },
                "dist":{
                    "type":"zip",
                    "url":"https://files.phpcomposer.com/files/hackification/log/75b02e14bd8e5b8ded75de91d7eb9df6046f7fa7.zip",
                    "reference":"75b02e14bd8e5b8ded75de91d7eb9df6046f7fa7",
                    "shasum":""
                },
                "type":"library",
                "time":"2016-11-08T15:32:34+00:00",
                "autoload":{
                    "psr-4":{
                        "Psr\Log\":"Psr/Log/"
                    }
                },
                "extra":{
                    "branch-alias":{
                        "dev-master":"1.0.x-dev"
                    }
                },
                "require":{
                    "hhvm":">=3.0.0"
                },
                "replace":{
                    "psr/log":"*"
                },
                "uid":1072563
            },
            "dev-master":{
                "name":"hackification/log",
                "description":"Common interface for logging libraries",
                "keywords":[
                    "log",
                    "psr",
                    "psr-3",
                    "hack",
                    "hacklang"
                ],
                "homepage":"https://github.com/hackification/log",
                "version":"dev-master",
                "version_normalized":"9999999-dev",
                "license":[
                    "MIT"
                ],
                "authors":[
                    {
                        "name":"PHP-FIG",
                        "homepage":"http://www.php-fig.org/"
                    }
                ],
                "source":{
                    "type":"git",
                    "url":"https://github.com/hackification/log.git",
                    "reference":"75b02e14bd8e5b8ded75de91d7eb9df6046f7fa7"
                },
                "dist":{
                    "type":"zip",
                    "url":"https://files.phpcomposer.com/files/hackification/log/75b02e14bd8e5b8ded75de91d7eb9df6046f7fa7.zip",
                    "reference":"75b02e14bd8e5b8ded75de91d7eb9df6046f7fa7",
                    "shasum":""
                },
                "type":"library",
                "time":"2016-11-08T15:32:34+00:00",
                "autoload":{
                    "psr-4":{
                        "Psr\Log\":"Psr/Log/"
                    }
                },
                "extra":{
                    "branch-alias":{
                        "dev-master":"1.0.x-dev"
                    }
                },
                "require":{
                    "hhvm":">=3.0.0"
                },
                "replace":{
                    "psr/log":"*"
                },
                "uid":1072564
            }
        },
        "mobio/target":{
            "0.0.5":{
                "name":"mobio/target",
                "description":"PHP library for myTarget API",
                "keywords":[
                    "php",
                    "myTarget"
                ],
                "homepage":"",
                "version":"0.0.5",
                "version_normalized":"0.0.5.0",
                "license":[
                    "MIT"
                ],
                "authors":[

                ],
                "source":{
                    "type":"git",
                    "url":"https://github.com/MobioInc/target.git",
                    "reference":"5baeaae1aa7d85c5b5fd4e33a06608a1de93d73b"
                },
                "dist":{
                    "type":"zip",
                    "url":"https://files.phpcomposer.com/files/MobioInc/target/5baeaae1aa7d85c5b5fd4e33a06608a1de93d73b.zip",
                    "reference":"5baeaae1aa7d85c5b5fd4e33a06608a1de93d73b",
                    "shasum":""
                },
                "type":"library",
                "time":"2016-10-31T08:52:52+00:00",
                "autoload":{
                    "psr-4":{
                        "Mobio\Target\":"src/"
                    }
                },
                "require":{
                    "php":">=5.5.0",
                    "psr/log":"~1.0",
                    "guzzlehttp/guzzle":"^6.1"
                },
                "require-dev":{
                    "phpunit/phpunit":"^5.5"
                },
                "provide":{
                    "psr/log":"1.0.0"
                },
                "uid":1060012
            },
            "dev-master":{
                "name":"mobio/target",
                "description":"PHP library for myTarget API",
                "keywords":[
                    "php",
                    "myTarget"
                ],
                "homepage":"",
                "version":"dev-master",
                "version_normalized":"9999999-dev",
                "license":[
                    "MIT"
                ],
                "authors":[

                ],
                "source":{
                    "type":"git",
                    "url":"https://github.com/MobioInc/target.git",
                    "reference":"70aa382ca6d3ba3b5a834bbe85d3fc2cbfec965f"
                },
                "dist":{
                    "type":"zip",
                    "url":"https://files.phpcomposer.com/files/MobioInc/target/70aa382ca6d3ba3b5a834bbe85d3fc2cbfec965f.zip",
                    "reference":"70aa382ca6d3ba3b5a834bbe85d3fc2cbfec965f",
                    "shasum":""
                },
                "type":"library",
                "time":"2017-02-13T18:53:10+00:00",
                "autoload":{
                    "psr-4":{
                        "Mobio\Target\":"src/"
                    }
                },
                "require":{
                    "php":">=5.5.0",
                    "guzzlehttp/guzzle":"^6.1",
                    "psr/log":"~1.0"
                },
                "require-dev":{
                    "phpunit/phpunit":"^5.5"
                },
                "provide":{
                    "psr/log":"1.0.0"
                },
                "uid":700331
            }
        },
        "notadd/wechat":{
            "dev-master":{
                "name":"notadd/wechat",
                "description":"Notadd's Wechat Module.",
                "keywords":[
                    "framework",
                    "cms",
                    "member",
                    "notadd"
                ],
                "homepage":"https://notadd.com",
                "version":"dev-master",
                "version_normalized":"9999999-dev",
                "license":[
                    "Apache-2.0"
                ],
                "authors":[
                    {
                        "name":"Notadd",
                        "email":"notadd@ibenchu.com"
                    }
                ],
                "source":{
                    "type":"git",
                    "url":"https://github.com/notadd/wechat.git",
                    "reference":"e3f684cd225f3fadf21953c0289cb8426baad0e5"
                },
                "dist":{
                    "type":"zip",
                    "url":"https://files.phpcomposer.com/files/notadd/wechat/e3f684cd225f3fadf21953c0289cb8426baad0e5.zip",
                    "reference":"e3f684cd225f3fadf21953c0289cb8426baad0e5",
                    "shasum":""
                },
                "type":"notadd-module",
                "time":"2017-11-13T04:23:05+00:00",
                "autoload":{
                    "psr-4":{
                        "Notadd\Wechat\":"src/"
                    }
                },
                "require":{
                    "php":">=7.0",
                    "overtrue/wechat":"~3.1"
                },
                "require-dev":{
                    "notadd/installers":"0.14.*",
                    "notadd/testing":"0.4.*",
                    "phpunit/phpunit":"~6.0"
                },
                "replace":{
                    "guzzlehttp/guzzle":"*",
                    "guzzlehttp/promises":"*",
                    "guzzlehttp/psr7":"*",
                    "monolog/monolog":"*",
                    "psr/container":"*",
                    "psr/http-message":"*",
                    "psr/log":"*",
                    "symfony/http-foundation":"*",
                    "symfony/polyfill-mbstring":"*",
                    "symfony/psr-http-message-bridge":"*"
                },
                "uid":1108963
            }
        },
        "psr/log":{
            "1.0.0":{
                "name":"psr/log",
                "description":"Common interface for logging libraries",
                "keywords":[
                    "log",
                    "psr",
                    "psr-3"
                ],
                "homepage":"",
                "version":"1.0.0",
                "version_normalized":"1.0.0.0",
                "license":[
                    "MIT"
                ],
                "authors":[
                    {
                        "name":"PHP-FIG",
                        "homepage":"http://www.php-fig.org/"
                    }
                ],
                "source":{
                    "type":"git",
                    "url":"https://github.com/php-fig/log.git",
                    "reference":"fe0936ee26643249e916849d48e3a51d5f5e278b"
                },
                "dist":{
                    "type":"zip",
                    "url":"https://files.phpcomposer.com/files/php-fig/log/fe0936ee26643249e916849d48e3a51d5f5e278b.zip",
                    "reference":"fe0936ee26643249e916849d48e3a51d5f5e278b",
                    "shasum":""
                },
                "type":"library",
                "time":"2012-12-21T11:40:51+00:00",
                "autoload":{
                    "psr-0":{
                        "Psr\Log\":""
                    }
                },
                "uid":29358
            },
            "1.0.1":{
                "name":"psr/log",
                "description":"Common interface for logging libraries",
                "keywords":[
                    "log",
                    "psr",
                    "psr-3"
                ],
                "homepage":"https://github.com/php-fig/log",
                "version":"1.0.1",
                "version_normalized":"1.0.1.0",
                "license":[
                    "MIT"
                ],
                "authors":[
                    {
                        "name":"PHP-FIG",
                        "homepage":"http://www.php-fig.org/"
                    }
                ],
                "source":{
                    "type":"git",
                    "url":"https://github.com/php-fig/log.git",
                    "reference":"5277094ed527a1c4477177d102fe4c53551953e0"
                },
                "dist":{
                    "type":"zip",
                    "url":"https://files.phpcomposer.com/files/php-fig/log/5277094ed527a1c4477177d102fe4c53551953e0.zip",
                    "reference":"5277094ed527a1c4477177d102fe4c53551953e0",
                    "shasum":""
                },
                "type":"library",
                "time":"2016-09-19T16:02:08+00:00",
                "autoload":{
                    "psr-4":{
                        "Psr\Log\":"Psr/Log/"
                    }
                },
                "extra":{
                    "branch-alias":{
                        "dev-master":"1.0.x-dev"
                    }
                },
                "require":{
                    "php":">=5.3.0"
                },
                "uid":1000789
            },
            "1.0.2":{
                "name":"psr/log",
                "description":"Common interface for logging libraries",
                "keywords":[
                    "log",
                    "psr",
                    "psr-3"
                ],
                "homepage":"https://github.com/php-fig/log",
                "version":"1.0.2",
                "version_normalized":"1.0.2.0",
                "license":[
                    "MIT"
                ],
                "authors":[
                    {
                        "name":"PHP-FIG",
                        "homepage":"http://www.php-fig.org/"
                    }
                ],
                "source":{
                    "type":"git",
                    "url":"https://github.com/php-fig/log.git",
                    "reference":"4ebe3a8bf773a19edfe0a84b6585ba3d401b724d"
                },
                "dist":{
                    "type":"zip",
                    "url":"https://files.phpcomposer.com/files/php-fig/log/4ebe3a8bf773a19edfe0a84b6585ba3d401b724d.zip",
                    "reference":"4ebe3a8bf773a19edfe0a84b6585ba3d401b724d",
                    "shasum":""
                },
                "type":"library",
                "time":"2016-10-10T12:19:37+00:00",
                "autoload":{
                    "psr-4":{
                        "Psr\Log\":"Psr/Log/"
                    }
                },
                "extra":{
                    "branch-alias":{
                        "dev-master":"1.0.x-dev"
                    }
                },
                "require":{
                    "php":">=5.3.0"
                },
                "uid":1029935
            },
            "dev-master":{
                "name":"psr/log",
                "description":"Common interface for logging libraries",
                "keywords":[
                    "log",
                    "psr",
                    "psr-3"
                ],
                "homepage":"https://github.com/php-fig/log",
                "version":"dev-master",
                "version_normalized":"9999999-dev",
                "license":[
                    "MIT"
                ],
                "authors":[
                    {
                        "name":"PHP-FIG",
                        "homepage":"http://www.php-fig.org/"
                    }
                ],
                "source":{
                    "type":"git",
                    "url":"https://github.com/php-fig/log.git",
                    "reference":"4ebe3a8bf773a19edfe0a84b6585ba3d401b724d"
                },
                "dist":{
                    "type":"zip",
                    "url":"https://files.phpcomposer.com/files/php-fig/log/4ebe3a8bf773a19edfe0a84b6585ba3d401b724d.zip",
                    "reference":"4ebe3a8bf773a19edfe0a84b6585ba3d401b724d",
                    "shasum":""
                },
                "type":"library",
                "time":"2016-10-10T12:19:37+00:00",
                "autoload":{
                    "psr-4":{
                        "Psr\Log\":"Psr/Log/"
                    }
                },
                "extra":{
                    "branch-alias":{
                        "dev-master":"1.0.x-dev"
                    }
                },
                "require":{
                    "php":">=5.3.0"
                },
                "uid":29285
            }
        },
        "wedeto/log":{
            "v0.9.1":{
                "name":"wedeto/log",
                "description":"Wedeto Platform - Logger",
                "keywords":[

                ],
                "homepage":"https://wedeto.net/",
                "version":"v0.9.1",
                "version_normalized":"0.9.1.0",
                "license":[
                    "MIT"
                ],
                "authors":[
                    {
                        "name":"Egbert van der Wal",
                        "email":"ewal@pointpro.nl"
                    }
                ],
                "source":{
                    "type":"git",
                    "url":"https://github.com/Wedeto/Log.git",
                    "reference":"acc7f4aa66965dd2627a3886c9ac8b0d77b0a268"
                },
                "dist":{
                    "type":"zip",
                    "url":"https://files.phpcomposer.com/files/Wedeto/Log/acc7f4aa66965dd2627a3886c9ac8b0d77b0a268.zip",
                    "reference":"acc7f4aa66965dd2627a3886c9ac8b0d77b0a268",
                    "shasum":""
                },
                "type":"library",
                "time":"2017-04-10T12:31:55+00:00",
                "autoload":{
                    "psr-4":{
                        "Wedeto\Log\":"src/"
                    }
                },
                "require":{
                    "psr/log":"1.0.*",
                    "wedeto/util":"0.9.*",
                    "php":">=7.0.0"
                },
                "require-dev":{
                    "mikey179/vfsstream":"~1"
                },
                "provide":{
                    "psr/log":"1.0.*"
                },
                "uid":1336127
            },
            "v0.9.2":{
                "name":"wedeto/log",
                "description":"Wedeto Platform - Logger",
                "keywords":[

                ],
                "homepage":"https://wedeto.net/",
                "version":"v0.9.2",
                "version_normalized":"0.9.2.0",
                "license":[
                    "MIT"
                ],
                "authors":[
                    {
                        "name":"Egbert van der Wal",
                        "email":"ewal@pointpro.nl"
                    }
                ],
                "source":{
                    "type":"git",
                    "url":"https://github.com/Wedeto/Log.git",
                    "reference":"55e6b03f0b446b7054078fd8a680ad1499d26264"
                },
                "dist":{
                    "type":"zip",
                    "url":"https://files.phpcomposer.com/files/Wedeto/Log/55e6b03f0b446b7054078fd8a680ad1499d26264.zip",
                    "reference":"55e6b03f0b446b7054078fd8a680ad1499d26264",
                    "shasum":""
                },
                "type":"library",
                "time":"2017-04-10T18:15:36+00:00",
                "autoload":{
                    "psr-4":{
                        "Wedeto\Log\":"src/"
                    }
                },
                "require":{
                    "psr/log":"1.0.*",
                    "wedeto/util":"0.9.*",
                    "php":">=7.0.0"
                },
                "require-dev":{
                    "mikey179/vfsstream":"~1"
                },
                "provide":{
                    "psr/log":"1.0.*"
                },
                "uid":1336697
            }
        }
    }
}

可以看到, packages 字段里面有5个包, 里面的 psr/log 字段就是我们要找的, 而里面有各个分支的 composer.json, 以分支 1.0.0 为例, 里面有两个很关键的字段, sourcedist:

{
    "source":{
        "type":"git",
        "url":"https://github.com/php-fig/log.git",
        "reference":"fe0936ee26643249e916849d48e3a51d5f5e278b"
    },
    "dist":{
        "type":"zip",
        "url":"https://files.phpcomposer.com/files/php-fig/log/fe0936ee26643249e916849d48e3a51d5f5e278b.zip",
        "reference":"fe0936ee26643249e916849d48e3a51d5f5e278b",
        "shasum":""
    }
}

dist

该字段其实就是加速的 zip 压缩包, 无需 git clone, 只需把 zip 下载到本地, 解压完, 分支 1.0.0 就装好了.

source

这个字段的作用, 就是万一 dist 字段的 zip 下载不了, 不会马上中断整个安装流程, 而是接着 git clone. 也就是说, dist 字段失败, 或者压根就没有 dist 字段, 就走 source 字段.


看到这里, 对 Composer 的了解应该多了很多吧? 还记得 请求结果的哈希值 吗? 这个哈希哪里来的, 为什么我可以提前知道这个请求的 JSON 的哈希值? 还有, 接口在哪里? 镜像服务器的官方网站, 并没有提供啊...

下一篇文章再告诉你.

相关文章

相关文章

何为 Composer

Composer 是 PHP 的依赖管理工具, 你可以在项目的 composer.json 文件中声明所依赖的组件, 它将自动为你安装.

安装 Composer

参考链接

命令行窗口下执行 php -v 检查是否已经正确安装 PHP, 如果没有安装, 安装它, 再把路径加到环境变量 PATH 中.

装完 PHP 再依次执行一下3条命令:

# 下载安装脚本 composer-setup.php 到当前目录
php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"

# 安装
php composer-setup.php

# 删除安装脚本
php -r "unlink('composer-setup.php');"

一般来说, Composer 我们使用全局安装, 其实就是放到环境变量 PATH 里面, 根据系统执行下面的步骤:

Windows

  1. 进入 PHP 的安装目录 (即 php.exe 所在位置);
  2. 复制 composer.phar 到上述目录;
  3. 新建 composer.bat 文件, 并将下列代码保存到该文件:
@php "%~dp0composer.phar" %*

Linux / Mac

只需移动或者复制 composer.phar 到目录 /usr/local/bin/ 即可, 即:

sudo mv composer.phar /usr/local/bin/composer

全局安装后, 执行以下命令, 如果输出了版本号, 说明 Composer 已经安装成功:

composer --version

配置 Composer 镜像

注意: 目前中国镜像已经失效, 请使用 Laravel China 社区提供的镜像服务.

中国镜像(已失效)

Laravel China 镜像服务

Composer 镜像我使用的是中国镜像, 因为速度快, 也非常稳定. 镜像的配置分为 全局项目 两种:

全局配置 (推荐)

# 已失效
# composer config -g repo.packagist composer https://packagist.phpcomposer.com
composer config -g repo.packagist composer https://packagist.laravel-china.org

项目配置

composer.json 所在目录, 执行以下命令:

# 已失效
# composer config repo.packagist composer https://packagist.phpcomposer.com
composer config  repo.packagist composer https://packagist.laravel-china.org

此时, composer.json 会多了 repositories 字段 (以 laravel 为例):

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.2.*"
    },
    "config": {
        "preferred-install": "dist"
    },
    "repositories": {
        "packagist": {
            "type": "composer",
            "url": "https://packagist.laravel-china.org"
        }
    }
}

OK, 镜像配置完成, 执行下面的命令, 体验飞一般的速度!

composer install

至于为什么变快了, 我会在后续的文章中解释.

相关文章

我的电脑是 Ubuntu 14.04 LTS, 自己手工编译 php5.6, 打开 ZEND_EXTRA_LIBS='-liconv' 时, 发现没有安装 libiconv, 也就是编码转换的库, 所以百度该库的安装方法, 如下:

下载:

$ wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz

解压:

$ tar -zxvf libiconv-1.14.tar.gz
$ cd libiconv-1.14.1

安装:

$ ./configure --prefix=/usr/local
$ make
# make install

不过我make的时候出现了一个问题:

n file included from progname.c:26:0:
./stdio.h:1010:1: error: ‘gets’ undeclared here (not in a function)
_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
^
make[2]: * [progname.o] Error 1
make[2]: Leaving directory `/home/freeman/Downloads/libiconv-1.14_2/srclib'
make[1]: * [all] Error 2
make[1]: Leaving directory `/home/freeman/Downloads/libiconv-1.14_2/srclib'
make: * [all] Error 2

原因未明, 应该是软件的bug吧, 后来百度找到了 解决方法, 整理如下~

切换到srclib目录下:

$ cd srclib

修改stdio.in.h文件:

$ gedit stdio.in.h

定位到

_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");

这一行, 改成:

#if defined(__GLIBC__) && !defined(__UCLIBC__) && !__GLIBC_PREREQ(2, 16)
 _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
#endif

注意粘贴完下面有两行 #endif, 别少复制了一行 #endif, 改完是这个样子滴~别忘了保存~

#if defined(__GLIBC__) && !defined(__UCLIBC__) && !__GLIBC_PREREQ(2, 16)
 _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
#endif
#endif

保存, make 就可以了, make install 完别忘了

# ldconfig

记下来, 下次就不会忘记了~

本文于 2017-12-05 重新整理.

写了一个可以对 $_GET, $_POST 等输入进行过滤的函数,递归实现如下:

function array_map_recursive($filters, $data)
{
    $result = [];
    foreach ($filters as $filter)
    {
        foreach ($data as $key => $value)
        {
            $result[$key] = is_array($value) 
                ? array_map_recursive($filters, $value)
                : call_user_func($filter, $value);
        }
    }
    return $result;
}

后来想想,以前看书的时候经常说递归函数会浪费堆栈空间,甚至会导致堆栈溢出,于是重新用循环实现了这个函数:

function array_map_all($filters, $data)
{
    foreach ($filters as $filter)
    {
        $stack = [];
        $stack[] = &$data;
        while ( ! empty($stack))
        {
            $node = &$stack[count($stack) - 1];

            array_pop($stack);

            if ( ! is_array($node))
            {
                $node = call_user_func($filter, $node);
            }
            else
            {
                foreach ($node as $key => $value)
                {
                    if ( ! empty($value))
                    {
                        $stack[] = &$node[$key];
                    }
                }
            }
        }
    }
    return $data;
}

写完哇擦~多了这么多行~还是递归简洁明了:)

速度如何呢?写个代码测试一下!

$filters = ['trim', 'htmlspecialchars'];
$data = [
    ' 5 &',
    [' 3 ', ' 7 '],
    [' 2 ', ' 4 '],
    ' 15 ',
    ' 4 ',
    ['12 ', '  ', ' 3 ', ' 4'],
];

$begin = microtime(true);
for ($i = 0; $i < 10000; $i++)
{
    array_map_recursive($filters, $data);
}
$t1 = microtime(true) - $begin;
echo '递归用时: '.sprintf('%.4f', $t1)."\n";

$begin = microtime(true);
for ($i = 0; $i < 10000; $i++)
{
    array_map_all($filters, $data);
}
$t2 = microtime(true) - $begin;
echo '循环用时: '.sprintf('%.4f', $t2)."\n";

运行结果:
递归用时: 0.4985
循环用时: 0.5489

换个复杂的七维数组,如下:

$data = [
    ' 5 &',
    [' 3 ', ' 7 ', [' 55 ', ['    565 ', [' 5445 ', ['da ', [', d '], '3']], 'd d']]],
    [' 2 ', ' 4 '],
    ' 15 ',
    ' 4 ',
    ['12 ', '  ', ' 3 ', ' 4'],
];

运行结果:
递归用时: 5.7484
循环用时: 0.9799

嘿嘿~结果不用我说了吧!还是循环好一些,不过实际应用中,是感觉不到太大差别的,毕竟是10000次循环呐!更何况是一个七维数组呢~

最后更新时间 2017-12-05.

我的环境:

phpMyAdmin:4.0.4.1

PHP:5.6.11

第一步 关闭自动更新

打开 ./libraries 目录下的 vendor_config.php,将 VERSION_CHECK_DEFAULT 的值设置为 FALSE, 意思是关闭自动更新。原因是官方无法访问。

define('VERSION_CHECK_DEFAULT', FALSE);

第二步 修正时间问题

打开 ./libraries 目录下的 Util.class.php,查找,

return strftime($date, $timestamp);

替换成下面的代码:

if (extension_loaded('gettext'))
{
    return strftime($date, $timestamp);
}

如果你在中国,使用北京时间,可以替换成下面的代码:

if (extension_loaded('gettext'))
{
    date_default_timezone_set('UTC');
    return gmdate('Y-m-d H:i:s', $timestamp + 28800);
}

至此,再刷新一下页面,点击“结构”等页面,页面的响应速度是不是大大提升了呢?嘿嘿,我的已经解决了,妈妈再也不用担心我又卡在“结构”页面啦!哈哈!最后感谢这篇文章指点迷津哦:

该链接已失效(2017-12-05).

http://www.fenanr.com/diy/112032.html