Rodhos Soft

備忘録を兼ねた技術的なメモです。Rofhos SoftではiOSアプリ開発を中心としてAndroid, Webサービス等の開発を承っております。まずはご相談下さい。

hello world

phpとjsとhtmlでhello worldしてみた。

<?php
  //print_r($argv);

function html($content) {
  return "<!DOCTYPE html>\n".wrap("html", $content);
}

function wrap($title,$content, $options=null) {
  if ($options == null) {
    return "<$title>\n"."       ".$content."</$title>\n";
  } else {
    $res = "";
    foreach($options as $key => $value) {
      $op = "$key"."="."$value";
      $res = $res.$op;
    }
    return "<$title $res>\n"."       ".$content."</$title>\n";
  }
}

function head($content) {
  return wrap("head", $content);
}

function body($content) {
  return wrap("body", $content);
}

function script($content) {
  return wrap("script", $content, array("type" => "text/javascript"));
}

function p($content) {
  return wrap("p", $content);
}

// --------------------------------------------
$head = head("
    <title>title</title>
    <meta charset='utf-8' />
    <style type='text/css'>
    p {
      color: yellow;
      background-color: black;
    }
    </style>
  ");

$script = script("
 document.writeln('<p> Hello rorld</p>');
 ");

$body = body(p("Hello World").$script);

$result = html($head.$body);
echo $result;

?>

実行すると以下が表示される。

<!DOCTYPE html>
<html>
       <head>
       
    <title>title</title>
    <meta charset='utf-8' />
    <style type='text/css'>
    p {
      color: yellow;
      background-color: black;
    }
    </style>
  </head>
<body>
       <p>
       Hello World</p>
<script type=text/javascript>
       
 document.writeln('<p> Hello rorld</p>');
 </script>
</body>
</html>

継続モナドその2

APIをFutureで返す。bindはとりあえず省略。

@interface Future<V> : NSObject
- (void)get:(void(^)(V))cb;
@end


@interface API<I,O> : NSObject
- (Future<O> *)getAPI:(I)input;
@end

typedef void(^CallBack)(id);
typedef void(^CPS)(CallBack);

@interface Future()
@property (nonatomic) CPS cps;
@end

@implementation Future

- (instancetype)init:(CPS)cps {
    self = [super init];
    if (self) {
        self.cps = cps;
    }
    return self;
}

- (void)get:(void (^)(id))cb {
    self.cps(cb);
    self.cps = nil;
}

@end

@implementation API
- (Future *)getAPI:(id)input {
    CPS cps = ^(CallBack cb) {
        return cb(input);
    };
    Future *future = [[Future alloc] init:cps];
    return future;
}
@end


Shell

チュートリアルを参照した。
コンソールツール、シェルとタスク - 3.x

src/Shell直下にHelloShell.phpを作る。

<?php
namespace App\Shell;

use Cake\Console\Shell;

class HelloShell extends Shell
{
    public function main()
    {
        $this->out('Hello world.');
    }

    public function heyThere($name = 'Anonymous')
    {
      $this->out('Hey there ' . $name);
      $this->_hoge();
    }

    // _が先頭にあるものは呼べない。
    public function _hoge()
    {
      $this->out('hoge');
    }
}
?>

実行は

./bin/cake hello

./bin/cake Hello hey_there name

等でできる。

bakeもできる。

 ./bin/cake bake shell User

Composer

依存ライブラリ管理ツール。

ライブラリの依存関係を定義する。

ライブラリの依存関係をcomposer.jsonで定義する。

プロジェクトの依存関係を作るには

composer init

で対話的にcomposer.jsonが作れる。

cakephpのプロジェクトを作りたいときは

composer create-project --prefer-dist cakephp/app myproj2

Migrationの失敗

Exception: There was a problem connecting to the database: SQLSTATE[HY000] [2002] No such file or directory in [/Applications/MAMP/htdocs/hoge/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/MysqlAdapter.php, line 115]

のようなエラーに悩まされた。

結果、app.iniのデータベース設定がlocalhostだとダメで、127.0.0.1でうまくいった。また、MAMPのpreferenceでMySQLのポートを調べて、それをapp.iniのデータベースのポート指定に反映させた。

毎回環境設定で躓くのをなんとかしたい。。あとphpのプリントデバッグはprint_rが使える。

カスタムメニュー

つかったことがないが。。

    override func viewDidLoad() {
        super.viewDidLoad()

        UIMenuController.shared.arrowDirection = .right;
        UIMenuController.shared.menuItems = [UIMenuItem.init(title: "a", action: #selector(hello))]
        let r = CGRect(x: 0, y: 0, width: 40, height: 40)
        UIMenuController.shared.setTargetRect(r, in: self.view)
        
        UIMenuController.shared.setMenuVisible(true, animated: true);
       
        UIMenuController.shared.update()
    }
    
    func hello() {
        print("hello")
    }

    
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if (action == #selector(hello)) {
            return true
        }
        return false
    }