ComposerでCakephpを入れてみた

phpのパッケージマネージャーのComposerでCakephpとPHPUnitを入れてみました
Ubuntu 14.04で試しました

システム全体で使えるようにしました
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer

参考)https://getcomposer.org/doc/00-intro.md

これで
$ composer
だけで呼び出せます

インストールするものをcomposer.jsonに記載します
まず、cakephpを入れてみました

$ vim composer.json

{
    "name": "example-app",
    "repositories": [
        {
            "type": "pear",
            "url": "http://pear.cakephp.org"
        }
    ],
    "require": {
        "cakephp/cakephp": ">=2.4.9"
    },
    "config": {
        "vendor-dir": "Vendor/"
    }
}

参照) http://book.cakephp.org/2.0/ja/installation/advanced-installation.html

インストール
$ composer install
Loading composer repositories with package information
Initializing PEAR repository http://pear.cakephp.org
Installing dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - cakephp/cakephp 2.6.1 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.6.0 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.8 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.7 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.6 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.5 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.4 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.3 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.2 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.1 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.0 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.4.9 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.4.10 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - Installation request for cakephp/cakephp >=2.4.9 -> satisfiable by cakephp/cakephp[2.4.10, 2.4.9, 2.5.0, 2.5.1, 2.5.2, 2.5.3, 2.5.4, 2.5.5, 2.5.6, 2.5.7, 2.5.8, 2.6.0, 2.6.1].

エラー発生..mcryptがない..ということで

$ sudo apt-get install php5-mcrypt
再度
$ composer install

Loading composer repositories with package information
Initializing PEAR repository http://pear.cakephp.org
Installing dependencies (including require-dev)
  - Installing cakephp/cakephp (2.6.1)
    Downloading: 100%         

Writing lock file
Generating autoload files

cakephpが入りました
こんなディレクトリで入るみたいです

$ tree -d -L 5

.
└── Vendor
    ├── bin
    ├── cakephp
    │   └── cakephp
    │       ├── app
    │       │   ├── Config
    │       │   ├── Console
    │       │   ├── Controller
    │       │   ├── Lib
    │       │   ├── Locale
    │       │   ├── Model
    │       │   ├── Plugin
    │       │   ├── Test
    │       │   ├── Vendor
    │       │   ├── View
    │       │   ├── tmp
    │       │   └── webroot
    │       ├── lib
    │       │   └── Cake
    │       ├── plugins
    │       └── vendors
    └── composer

エラーが出たら
http://book.cakephp.org/2.0/ja/getting-started.html
http://book.cakephp.org/2.0/ja/installation/url-rewriting.html
を参考に

PHPExcelでセル内改行をする

PHPExcelを使ってセル内改行ではまったのでメモ

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$excel = $objReader->load($templatepath);
$excel->setActiveSheetIndex(0);
$sheet = $excel->getActiveSheet();
$sheet->setTitle($sheetName);
$value = '123'."\r".'';
$sheet->setCellValue('A8', $value);

と改行コードを \r を入れてもしても改行されない..
(Excelは \r らしい)

$obj->getActiveSheet()->getStyle(‘A8’)->getAlignment()->setWrapText(true);

が必要なようで、これをいれると改行されました

excel_eol

phpでファイルの更新時間を取る

phpでファイルの更新時間を取るには

があります

filectimeはinode変更時間をとるので、権限の変更とかも変更時間が変わります
filemtimeはファイルが書き換えられたら、変更時間が変わります

こんなコードで試してみました

<?php
$unix = filectime('./sample');
var_dump(date('Y-m-d H:i:s', $unix));
$unix = filemtime('./sample');
var_dump(date('Y-m-d H:i:s', $unix));

CakephpのCakeResponseで日本語名ファイルをダウンロードできない

cakephp 2.5.3での出来事です

public function download($path)
{
    // レイアウトの無効
    $this->autoRender = false;
    if( file_exists($fileName) === TRUE )
    {
        $this->response->file($path);
        $this->response->download(basename($path));
        return $this->response;
    }
}

なんてすれば、ファイルダウンロードできるはずが
404エラーになる..

ソースコードを追うと

//lib/Cake/Network/CakeResponse.php L1347
    $file = new File($path);
if (!$file->exists() || !$file->readable()) {
    if (Configure::read('debug')) {
        throw new NotFoundException(__d('cake_dev', 'The requested file %s was not found or not readable', $path));
    }
    throw new NotFoundException(__d('cake', 'The requested file was not found'));
}

if (!$file->exists() || !$file->readable()) {
でエラーに

Fileモデルを追っていると

//lib/Cake/Utility/File.php L86
public function __construct($path, $create = false, $mode = 0755) {
    $this->Folder = new Folder(dirname($path), $create, $mode);
    if (!is_dir($path)) {
        $this->name = basename($path);
    }
    $this->pwd();
    $create && !$this->exists() && $this->safe($path) && $this->create();
}

で、$this->name = basename($path);

.txtになってました
ロケールの問題でした..

以前公開したこちらで解決です
phpのbasenameはロケール次第で日本語が扱えない