MacのPHPをHomebrewで7.1に変更する
Symfony4を利用するにはPHP7.1.3以上でなければならない。
https://symfony.com/doc/current/reference/requirements.html
試してみたところ、PHPのバージョンが要件に満たない場合は composer create-project
するとSymfony3.4のプロジェクトが作成されるぽい。
https://symfony.com/doc/current/setup.html
ということで、HomebrewでMacのPHPのバージョンをあげたときのメモ。
自分の環境ではHomebrewを使ってPHP7.0を入れていたので、以下のような手順でPHP7.1を入れ直した。
$ brew update $ brew upgrade $ brew unlink php70 $ brew install php71
特にエラーなどが発生しなければこれでスムーズにいけるはず。
が、自分の環境だと brew upgrade
したときに以下のようなエラーが発生した。
brew permission denied @ dir_s_mkdir
権限がなく、ディレクトリが作れないっぽい。
ので手動でディレクトリを作って対応した。
$ sudo mkdir /usr/local/Frameworks
これで無事upgradeできてPHP7.1インストールまでできた。
PHPで言語処理100本ノック 2015(第1章)
00. 文字列の逆順
<?php $str = 'stressed'; echo strrev($str);
01. 「パタトクカシーー」
<?php $str = 'パタトクカシーー'; $result = ''; for ($i = 0; $i < mb_strlen($str); ++$i) { $result .= ($i % 2 === 0) ? mb_substr($str, $i, 1) : ''; } echo $result;
02. 「パトカー」+「タクシー」=「パタトクカシーー」
<?php $s1 = 'パトカー'; $s2 = 'タクシー'; $len = mb_strlen($s1) > mb_strlen($s2) ? mb_strlen($s1) : mb_strlen($s2); $result = ''; for ($i = 0; $i < $len; ++$i) { $result .= (mb_substr($s1, $i, 1) . mb_substr($s2, $i, 1)); } echo $result;
03. 円周率
<?php $str = 'Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.'; $arr = explode(' ', str_replace([',', '.'], '', $str)); $result = []; foreach ($arr as $v) { $result[] = mb_strlen($v); } print_r($result);
04. 元素記号
<?php $str = 'Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.'; $arr = explode(' ', str_replace([',', '.'], '', $str)); $targetIndex = [1,5,6,7,8,9,15,16,19]; $result = []; foreach ($arr as $k => $v) { $result[in_array($k + 1, $targetIndex, true) ? mb_substr($v, 0, 1) : mb_substr($v, 0, 2)] = $k + 1; } print_r($result);
05. n-gram
<?php function nGram(string $str, int $n, string $type = 'char'): string { $result = ''; if ($type === 'char') { for ($i = 0; $i < mb_strlen($str); ++$i) { $result .= mb_substr($str, $i, $n) . "\n"; } return $result; } if ($type === 'word') { $arr = explode(' ', $str); for ($i = 0; $i < count($arr); ++$i) { $result .= implode(' ', array_slice($arr, $i, $n)) . "\n"; } return $result; } return "引数には'char'もしくは'word'を指定してください\n"; } $str = 'I am an NLPer'; echo nGram($str, 2); echo "\n"; echo nGram($str, 2, 'word');
06. 集合
<?php function nGram(string $str, int $n = 2): array { $result = []; for ($i = 0; $i < mb_strlen($str); ++$i) { $result[] = mb_substr($str, $i, $n); } return array_values(array_unique($result)); } function union(array $x, array $y): array { return array_values(array_unique(array_merge($x, $y))); } function intersection(array $x, array $y): array { return array_intersect($x, $y); } function difference(array $x, array $y): array { return array_diff($x, $y); } function inBigram(array $arr, string $str): bool { return in_array($str, $arr, true); } $x = nGram('paraparaparadise'); $y = nGram('paragraph'); echo "X is \n"; print_r($x); echo "\n"; echo "Y is \n"; print_r($y); echo "\n"; echo "Union is \n"; print_r(union($x, $y)); echo "\n"; echo "Intersection is \n"; print_r(intersection($x, $y)); echo "\n"; echo "Difference(x-y) is \n"; print_r(difference($x, $y)); echo "\n"; echo "Difference(y-x) is \n"; print_r(difference($y, $x)); echo "\n"; echo "'se' in X \n"; var_export(inBigram($x, 'se')); echo "\n"; echo "'se' in Y \n"; var_export(inBigram($y, 'se')); echo "\n";
07. テンプレートによる文生成
<?php function template($x, $y, $z): string { return printf('%s時の%sは%s', $x, $y, $z); } $x = 12; $y = '気温'; $z = 22.4; template($x, $y, $z);
08. 暗号文
<?php function cipher(string $str): string { $result = ''; for ($i = 0; $i < mb_strlen($str); ++$i) { $char = mb_substr($str, $i, 1); $result .= ctype_alpha($char) && ctype_lower($char) ? chr(219 - ord($char)) : $char; } return $result; } echo cipher("Hello, Peter. I'm Ken. Nice to meet you.");
09. Typoglycemia
<?php function typoglycemia(string $sentence): string { $result = ''; $wordList = explode(' ', $sentence); foreach ($wordList as $word) { if (mb_strlen($word) < 4) { $result .= $word; } else { $result .= mb_substr($word, 0, 1); $result .= str_shuffle(mb_substr($word, 1, (mb_strlen($word) - 2))); $result .= mb_substr($word, (mb_strlen($word) - 1), 1); } $result .= ' '; } return $result; } $sentence = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ."; echo typoglycemia($sentence);
Symfony4でVue.jsを使う
必要なものをインストールして、
$ yarn add --dev vue vue-loader vue-template-compiler
webpack.config.js
の修正。
Encore
// ...
.enableVueLoader()
;
これだけで使えるようになる。
Hello world的なところまでやってみる。
エントリーポイントは assets/js/app.js
。
Symfony4のフロントエンド環境構築 - Tomcky's blog
assets/js/app.js
の作成。
import Vue from 'vue'; import MyComponent from './MyComponent'; new Vue({ el: '#example', components: { MyComponent }, template: '<MyComponent/>' });
assets/js/MyComponent.vue
の作成。
<template> <div class="example">{{ msg }}</div> </template> <script> export default { data () { return { msg: 'Hello world!' } } } </script> <style> .example { color: red; } </style>
表示させたいTwigテンプレートを修正。
{# 下の一行を任意の箇所に追加 #} <div id="example"></div>
yarn run
する。
$ yarn run encore dev
赤字で"Hello world"が表示されるはず。
このあたりは最低でもコンポーネントに対する理解がないとツライ。
ので、この辺りを読んどくといい。
コンポーネント — Vue.js
単一ファイルコンポーネント — Vue.js
Vue Component の仕様 · vue-loader
vue.jsのcomponentをwebpackで.vueにして単一ファイルコンポーネントにする - Qiita