Skip to content

Instantly share code, notes, and snippets.

@k-holy
k-holy / built-in-server.php
Created December 13, 2011 23:56
PHP5.4ビルトインサーバのルータースクリプト
<?php
// ポート8080の場合、ショートカットのリンク先 C:\php5.4\php.exe -S 127.0.0.1:8080 built-in-server.php
// 作業フォルダにドキュメントルートを入力して起動
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if (strcmp('/', $path) !== 0) {
if (file_exists(realpath(__DIR__ . $path))) {
return false;
}
}
require __DIR__ . DIRECTORY_SEPARATOR . 'index.php';
@k-holy
k-holy / _nya
Created December 19, 2011 05:49
NYAOS用Luaスクリプト
# 履歴保存
option savehist C:\Applications\NYAOS\.history
# %USERPROFILE%\_nya に以下を追記して読み込む
source %USERPROFILE%\Dropbox\AppData\NYAOS\_nya_prompt.lua
# Gitコマンド
alias git C:\Applications\Git\bin\git.exe
@k-holy
k-holy / googlenews.txt
Created December 19, 2011 10:18
BEAR.Sunday(apps/04-rest)動かしてみて発生したエラー(Windows7+NYAOS+PHP5.4.0RC3 CLI)
$ php apps/04-rest/htdocs/dev.php get /http/googlenews
[1;32m[CODE][0m500 Internal Server Error
[1;32m[HEADER][1;33m X-EXCEPTION [0mException
[1;32m[HEADER][1;33m X-EXCEPTION-CODE [0m0
[1;32m[HEADER][1;33m X-EXCEPTION-MESSAGE [0mView C:\Users\k_horii\Documents\Projects\BEAR\BEAR.Sunday\apps\04-rest\Page\http/view/GoogleNews.hng doesn't exists
[1;32m[HEADER][1;33m 0 [0mX-Request-Per-Second: 0
[1;32m[HEADER][1;33m 1 [0mX-Memory-Peak-Usage: 3,145,728
[1;32m[BODY][0m
<div class="trace">#0 C:\Users\k_horii\Documents\Projects\BEAR\BEAR.Sunday\packages\BEAR.Framework\src\Link\View\Haanga.php(48): Haanga::Load('C:\Users\k_hori...', Array, true)
#1 [internal function]: restWorld\Page\Http\GoogleNews->onLinkView(Object(restWorld\Page\Http\GoogleNews))
@k-holy
k-holy / error_handler.php
Created December 20, 2011 00:55
callableタイプヒントの追加と配列から文字列への暗黙の変換時の警告 (PHP5.4 Advent Calendar 2011 Day 21)
<?php
set_error_handler(function($errno, $errstr, $errfile, $errline){
$titles = array (
E_ERROR => 'Fatal error',
E_WARNING => 'Warning',
E_NOTICE => 'Notice',
E_STRICT => 'Strict standards',
E_RECOVERABLE_ERROR => 'Catchable fatal error',
E_DEPRECATED => 'Depricated',
);
@k-holy
k-holy / include_function.items.php
Created January 5, 2012 09:39
無名関数をinclude
<?php
return array(
array('name' => 'Abe' , 'score' => 800),
array('name' => 'Oda' , 'score' => 1000),
array('name' => 'Hosokawa', 'score' => 600),
);
@k-holy
k-holy / memcache_add.php
Created January 13, 2012 03:38
PHP Memcacheのテスト
<?php
session_start();
// コネクションプールにサーバ追加。必要な時に自動接続、スクリプト終了時に自動切断される。
$memcache = new Memcache();
$memcache->addServer('localhost', 11211);
$keys = range(0, 10000);
$items = array_combine($keys,
@k-holy
k-holy / memcache_get.php
Created January 17, 2012 02:59
PHP MemcacheとSQLiteインメモリDBの比較
<?php
set_time_limit(60);
session_start();
$keys = range(0, 100000);
//-----------------------------------------------------------------------------
$host = 'localhost';
@k-holy
k-holy / str_getcsv.php
Created January 19, 2012 06:16
str_getcsv()の第4引数($escape)の挙動
<?php
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_CALLBACK, function ($file, $line) {
echo '<pre>' . htmlspecialchars(sprintf("Assertion Failed: at %s[%d]\n", $file, $line)) . '</pre>';
});
assert(array('1', 'foo"') == str_getcsv("1\t\"foo\"\"", "\t", '"')); // OK
assert(array('1', 'foo"') == str_getcsv("1\t\"foo\\\"", "\t", '"', '\\')); // NG
// なぜなの…
@k-holy
k-holy / autoload.php
Created January 25, 2012 10:19
include_path前提のnamespace対応autoload実装
<?php
namespace Acme;
set_include_path(implode(PATH_SEPARATOR, array(
'/path/to/pear',
'/path/to/vendor',
get_include_path(),
)));
spl_autoload_register(function($className) {
@k-holy
k-holy / autoload.function.php
Created January 26, 2012 02:54
include_path前提のnamespace対応autoload実装(namespaceとpearの併設エラー検証)
<?php
return function($className) {
$className = ltrim($className, '\\');
if (false !== ($pos = strrpos($className, '\\'))) {
$namespace = substr($className, 0, $pos);
$className = substr($className, $pos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace)
. DIRECTORY_SEPARATOR
. str_replace('_', DIRECTORY_SEPARATOR, $className);
} else {