Slide 1

Slide 1 text

Learning Swift2 with PHP7 PHP BLT #1 2015-11-24 @suzuki

Slide 2

Slide 2 text

Lightning Talks Driven Learning This talk for my study.
 Perhaps it is not interesting for you☻

Slide 3

Slide 3 text

Language Specifications PHP7 : Swift2

Slide 4

Slide 4 text

language version % php --version PHP 7.0.0RC6 (cli) (built: Oct 29 2015 13:49:53) ( NTS ) Copyright (c) 1997-2015 The PHP Group Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies % swift --version Apple Swift version 2.1 (swiftlang-700.1.101.6 clang-700.1.76) Target: x86_64-apple-darwin14.5.0

Slide 5

Slide 5 text

comment

Slide 6

Slide 6 text

type 1, 'b' => 2]; echo $hashVar['a']; var intVar1 = 123 var intVar2:Int = 123 var floatVar1 = 1.23 var floatVar2:Float = 1.23 var doubleQuote1 = "string" var doubleQuote2:String = "string" // single quote is not work //var singleQuote1 = 'string' //var singleQuote2:String = 'string' var arrayVar = [1, 2, 3] var dic:Dictionary = ["a":1, "b":2] // "!" is cast for Optional tyep print(dic["a"]!)

Slide 7

Slide 7 text

access

Slide 8

Slide 8 text

operator

Slide 9

Slide 9 text

if $b) { } elseif ($a < $b) { } else { } var a = 123 var b = 456 // Parens can be ommitted. if a > b { } else if a < b { } else { }

Slide 10

Slide 10 text

switch

Slide 11

Slide 11 text

for

Slide 12

Slide 12 text

while

Slide 13

Slide 13 text

function Array { var result:[String] = [] for i in 0..<10 { result.append(str + String(i)) } return result } let list = foo("x") for l in list { print(l) }

Slide 14

Slide 14 text

class bar; } } $example = new Example(); echo $example->foo(); protocol ExampleProtocol { func foo() -> String } class Base { internal let bar = "bar" } class Example: Base, ExampleProtocol { override init() { print("initialize") } func foo() -> String { return self.bar } } let example = Example() // no need 'new' print(example.foo())

Slide 15

Slide 15 text

Swift Application

Slide 16

Slide 16 text

CLI with Swift2 http://techlife.cookpad.com/entry/2015/11/09/150248

Slide 17

Slide 17 text

CLI with Swift2 http://techlife.cookpad.com/entry/2015/11/09/150248

Slide 18

Slide 18 text

#!/usr/bin/env swift

Slide 19

Slide 19 text

So…

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Web Application PHP7 : Swift2

Slide 22

Slide 22 text

system structure nginx php-fpm php7 index.php nginx fcgiwrap swift2 index.swift

Slide 23

Slide 23 text

form

Slide 24

Slide 24 text

confirm

Slide 25

Slide 25 text

complete

Slide 26

Slide 26 text

execute run(); #!/usr/bin/env swift let view = View(); let app = App(view: view); app.run()

Slide 27

Slide 27 text

App::run() public function run() { $params = $this->parseGetVars(); $html = ''; $next = $params['next'] ?? ''; switch ($next) { case 'confirm': $html = $this->view->getConfirmHtml($params); break; case 'complete': $html = $this->view->getCompleteHtml($params); break; default: $html = $this->view->getFormHtml($params); } $this->response($html); } func run() { let params = self.parseGetVars() var html = "" let next = params["next"] ?? "" switch next { case "confirm": html = self.view.getConfirmHtml(params) case "complete": html = self.view.getCompleteHtml(params) default: html = self.view.getFormHtml(params) } self.response(html); }

Slide 28

Slide 28 text

htmlspecialchars private function renderTemplate( string $html, array $params ): string { $replaceKey = []; $replaceValue = []; foreach ($params as $key => $value) { $replaceKey[] = '/{{ ' . $key . ' }}/'; $replaceValue[] = htmlspecialchars( $value, ENT_QUOTES , 'UTF-8' ); } $replaceKey[] = '/{{ action }}/'; $replaceValue[] = 'index.php'; return preg_replace( $replaceKey, $replaceValue, $html); } private func renderTemplate(html: String, params: Dictionary) -> String { let pattern = "\\{\\{ action \\}\\}" let replace = "index.swift" var html = html.stringByReplacingOccurrencesOfString(pattern, withString: replace, options: NSStringCompareOptions.RegularExpressionSearch, range: nil) for (key, value) in params { let pattern = "\\{\\{ " + key + " \\}\\}" let replace = self.htmlspecialchars(value) html = html.stringByReplacingOccurrencesOfString(pattern, withString: replace, options: NSStringCompareOptions.RegularExpressionSearch, range: nil) } return html; } private func htmlspecialchars(var html: String) -> String { let replaceDef: Dictionary = [ "\"": """, "'": "'", "<": "<", ">": ">", ] for (key, value) in replaceDef { let pattern = key; let replace = value; html = html.stringByReplacingOccurrencesOfString(pattern, withString: replace, options: NSStringCompareOptions.RegularExpressionSearch, range: nil) } return html }

Slide 29

Slide 29 text

other code https://github.com/suzuki/learning-swift2-with-php7

Slide 30

Slide 30 text

Conclusion

Slide 31

Slide 31 text

• Swift2 can use in web applications, maybe.

Slide 32

Slide 32 text

perfect http://perfect.org/

Slide 33

Slide 33 text

• Let's use the new syntax of PHP7. • It would be helpful to learn other programing languages.

Slide 34

Slide 34 text

Thank you

Slide 35

Slide 35 text

Reference • Swift2Ͱ࡞ΔίϚϯυϥΠϯπʔϧ • http://techlife.cookpad.com/entry/2015/11/09/150248 • Swift 2ඪ४ΨΠυϒοΫ • http://tatsu-zine.com/books/swift2-hyojyun-guide • PHP7ͰมΘΔ͜ͱ ——ݴޠ࢓༷ͱΤϯδϯͷվળϙΠϯτ • http://www.slideshare.net/hnw/phpcon-kansai20150530 • Apple͕SwiftΛΦʔϓϯιʔεԽ • http://www.infoq.com/jp/news/2015/06/swift-opensourced