Composer 小技巧

Composer 是新一代的PHP依赖管理工具。其介绍和基本用法可以看这篇《Composer PHP依赖管理的新时代》。本文介绍使用Composer的五个小技巧,希望能给你的PHP开发带来方便。

1. 仅更新单个库
只想更新某个特定的库,不想更新它的所有依赖,很简单:

composer update foo/bar  

此外,这个技巧还可以用来解决“警告信息问题”。你一定见过这样的警告信息:
继续阅读

Debian升级

Debian 6(Squeeze)升级至Debian 7(Wheezy) (7级8同理)
Debian GNU/Linux支持滚动更新,出于安全方面的考虑,当新版Debian发行后,我们一般会将现有版本的Debian升级至最新的稳定版的Debian。升级除了可以提升系统的安全性以外,同时还可以获得由更新版本的应用软件带来的新功能及其它好处。这就是我们为什么要将Debian 6(Squeeze)升级至Debian 7(Wheezy)的原因。
继续阅读

Html5 canvas画svg

<html>
 <head>
  <title>test</title>
  <style>
  svg, canvas { margin:1em }
  </style>
 </head>
 <body>
  <p>
  <svg xmlns="http://www.w3.org/2000/svg" width="100px" height="100px" id="i">
  FAIL
  <circle cx="50px" cy="50px" r="50px" fill="lime"/>
  </svg>
  <canvas width="100" height="100" id="c">FAIL</canvas>
  </p>
  <script type="text/javascript">
      window.onload = function () {
          var ctx = document.getElementById("c").getContext('2d');
          var svg_xml = (new XMLSerializer()).serializeToString(document.getElementById("i")); 
          var img = new Image();
          img.src = "data:image/svg+xml;base64," + window.btoa(svg_xml);
          img.onload = function () {
              ctx.drawImage(img, 0, 0);
          };
      }
  </script>
 </body>
</html>

JS精度计算

Math.formatFloat = function(f, digit) { 
    var m = Math.pow(10, digit); 
    return parseInt(f * m, 10) / m; 
} 

var numA = 0.1; 
var numB = 0.2;
alert(Math.formatFloat(numA + numB, 1) === 0.3);

这个方法是什么意思呢?为了避免产生精度差异,我们要把需要计算的数字乘以 10 的 n 次幂,换算成计算机能够精确识别的整数,然后再除以 10 的 n 次幂,大部分编程语言都是这样处理精度差异的,我们就借用过来处理一下 JS 中的浮点数精度误差。

ImageMagick Is On Fire — CVE-2016–3714

If you use ImageMagick or an affected library, we recommend you mitigate the known vulnerabilities by doing at least one of these two things (but preferably both!):

Verify that all image files begin with the expected “magic bytes” corresponding to the image file types you support before sending them to ImageMagick for processing. (see FAQ for more info)
Use a policy file to disable the vulnerable ImageMagick coders. The global policy for ImageMagick is usually found in “/etc/ImageMagick”. The below policy.xml example will disable the coders EPHEMERAL, URL, MVG, and MSL.
policy.xml (updated 5/5)

<policymap>
  <policy domain="coder" rights="none" pattern="EPHEMERAL" />
  <policy domain="coder" rights="none" pattern="URL" />
  <policy domain="coder" rights="none" pattern="HTTPS" />
  <policy domain="coder" rights="none" pattern="MVG" />
  <policy domain="coder" rights="none" pattern="MSL" />
  <policy domain="coder" rights="none" pattern="TEXT" />
  <policy domain="coder" rights="none" pattern="SHOW" />
  <policy domain="coder" rights="none" pattern="WIN" />
  <policy domain="coder" rights="none" pattern="PLT" />
</policymap>

Custom Blade Directives in Laravel 5

Step 1: Register a custom Blade directive:

<?php // code in app/Providers/AppServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Blade; // <-- This is important! Without it you'll get an exception.

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
     public function boot()
     {
         // Make a custom blade directive:
         Blade::directive('shout', function ($string) {
             return trim(strtoupper($string), '(\'\')');
         });

         // And another one for good measure:
         Blade::directive('customLink', function () {
             return '<a href="#">Custom Link</a>';
         });
     }
    ...
Step 2: Use your custom Blade directive:

<!-- // code in resources/views/view.blade.php -->

@shout('this is my custom blade directive!!')
<br />
@customLink

Outputs:

THIS IS MY CUSTOM BLADE DIRECTIVE!!
Custom Link

Custom Classes in Laravel 5

This answer is applicable to general custom classes within Laravel. For a more Blade-specific answer, see Custom Blade Directives in Laravel 5.

Step 1: Create your Helpers (or other custom class) file and give it a matching namespace. Write your class and method:

<?php // Code within app\Helpers\Helper.php

namespace App\Helpers;

class Helper
{
    public static function shout(string $string)
    {
        return strtoupper($string);
    }
}

Step 2: Create an alias:

<?php // Code within app/config.php

    'aliases' => [
     ...
        'Helper' => 'App\Helpers\Helper::class',
     ...
</pre >
Step 3: Use it in your Blade template:
<pre>
<!-- Code within resources/views/template.blade.php -->

{!! Helper::shout('this is how to use autoloading correctly!!') !!}

Extra Credit: Use this class anywhere in your Laravel app:

<?php // Code within app/Http/Controllers/SomeController.php

namespace App\Http\Controllers;

use App\Helpers\Helper;

class SomeController extends Controller
{

    public function __construct()
    {
        Helper::shout('now i\'m using my helper class in a controller!!');
    }
    ...

Source: http://www.php-fig.org/psr/psr-4/

Why it works: https://github.com/laravel/framework/blob/master/src/Illuminate/Support/ClassLoader.php

Where autoloading originates from: http://php.net/manual/en/language.oop5.autoload.php