<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>sylvan &#187; bat</title>
	<atom:link href="http://www.sylvan-l.net/b/tag/bat/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sylvan-l.net/b</link>
	<description>GTD、ユーザビリティ、iPhone、本、アート</description>
	<lastBuildDate>Sat, 04 Feb 2012 17:44:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://www.sylvan-l.net/b/tag/bat/feed/" />
		<item>
		<title>symfony1.2でバッチ処理を作るにはどうすればいいの？</title>
		<link>http://www.sylvan-l.net/b/2009/01/14/symfony12%e3%81%a7%e3%83%90%e3%83%83%e3%83%81%e5%87%a6%e7%90%86%e3%82%92%e4%bd%9c%e3%82%8b%e3%81%ab%e3%81%af%e3%81%a9%e3%81%86%e3%81%99%e3%82%8c%e3%81%b0%e3%81%84%e3%81%84%e3%81%ae%ef%bc%9f/</link>
		<comments>http://www.sylvan-l.net/b/2009/01/14/symfony12%e3%81%a7%e3%83%90%e3%83%83%e3%83%81%e5%87%a6%e7%90%86%e3%82%92%e4%bd%9c%e3%82%8b%e3%81%ab%e3%81%af%e3%81%a9%e3%81%86%e3%81%99%e3%82%8c%e3%81%b0%e3%81%84%e3%81%84%e3%81%ae%ef%bc%9f/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 15:46:29 +0000</pubDate>
		<dc:creator>sylvan</dc:creator>
				<category><![CDATA[Web開発関連]]></category>
		<category><![CDATA[bat]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://www.sylvan-l.net/b/?p=1183</guid>
		<description><![CDATA[symfony1.2 では generate:task コマンドが用意されているのでそれを利用します。 まず、ジェネレートコマンドでひな形を作る ジェネレートコマンドを実行すると、 % ./symfony generate:task foo:sample fooSampleTask.class.php というファイルが作成される // lib/task/fooSampleTask.class.php &#60;?php class fooSampleTask extends sfBaseTask &#123; &#160; protected function configure&#40;&#41; &#160; &#123; &#160; &#160; // // add your own arguments here &#160; &#160; // $this-&#62;addArguments(array( &#160; &#160; //&#160; &#160;new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'), &#160; &#160; // )); &#160; &#160; $this-&#62;addOptions&#40;array&#40; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>symfony1.2 では generate:task コマンドが用意されているのでそれを利用します。</p>
<ol>
<li><strong>まず、ジェネレートコマンドでひな形を作る</strong></li>
<p>ジェネレートコマンドを実行すると、</p>
<div class="syntax_hilite">
<div id="code-7">
<div class="code">% ./symfony generate:task foo:sample</div>
</div>
</div>
<p></p>
<p>fooSampleTask.class.php というファイルが作成される</p>
<div class="syntax_hilite">
<div id="php-8">
<div class="php"><span style="color:#FF9933; font-style:italic;">// lib/task/fooSampleTask.class.php</span><br />
<span style="color:#000000; font-weight:bold;">&lt;?php</span></p>
<p><span style="color:#000000; font-weight:bold;">class</span> fooSampleTask extends sfBaseTask<br />
<span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; protected <span style="color:#000000; font-weight:bold;">function</span> configure<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// // add your own arguments here</span><br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// $this-&gt;addArguments(array(</span><br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">//&nbsp; &nbsp;new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),</span><br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// ));</span></p>
<p>&nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">addOptions</span><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://jp.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">new</span> sfCommandOption<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'application'</span>, <span style="color:#000000; font-weight:bold;">null</span>, sfCommandOption::<span style="color:#006600;">PARAMETER_REQUIRED</span>, <span style="color:#FF0000;">'The application name'</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">new</span> sfCommandOption<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'env'</span>, <span style="color:#000000; font-weight:bold;">null</span>, sfCommandOption::<span style="color:#006600;">PARAMETER_REQUIRED</span>, <span style="color:#FF0000;">'The environment'</span>, <span style="color:#FF0000;">'dev'</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">new</span> sfCommandOption<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'connection'</span>, <span style="color:#000000; font-weight:bold;">null</span>, sfCommandOption::<span style="color:#006600;">PARAMETER_REQUIRED</span>, <span style="color:#FF0000;">'The connection name'</span>, <span style="color:#FF0000;">'propel'</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// add your own options here</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">namespace</span>&nbsp; &nbsp; &nbsp; &nbsp; = <span style="color:#FF0000;">'foo'</span>;<br />
&nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">name</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= <span style="color:#FF0000;">'sample'</span>;<br />
&nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">briefDescription</span> = <span style="color:#FF0000;">''</span>;<br />
&nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">detailedDescription</span> = &lt;&lt;&lt;EOF<br />
The <span style="color:#006600; font-weight:bold;">&#91;</span>foo:sample|INFO<span style="color:#006600; font-weight:bold;">&#93;</span> task does things.<br />
Call it with:</p>
<p>&nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span>php symfony foo:sample|INFO<span style="color:#006600; font-weight:bold;">&#93;</span><br />
EOF;<br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p>&nbsp; protected <span style="color:#000000; font-weight:bold;">function</span> execute<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$arguments</span> = <a href="http://jp.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#0000FF;">$options</span> = <a href="http://jp.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// initialize the database connection</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$databaseManager</span> = <span style="color:#000000; font-weight:bold;">new</span> sfDatabaseManager<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">configuration</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#0000FF;">$connection</span> = <span style="color:#0000FF;">$databaseManager</span>-&gt;<span style="color:#006600;">getDatabase</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$options</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'connection'</span><span style="color:#006600; font-weight:bold;">&#93;</span> ? <span style="color:#0000FF;">$options</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'connection'</span><span style="color:#006600; font-weight:bold;">&#93;</span> : <span style="color:#000000; font-weight:bold;">null</span><span style="color:#006600; font-weight:bold;">&#41;</span>-&gt;<span style="color:#006600;">getConnection</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// add your code here</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</div>
</div>
<p></p>
<li><strong>execute 関数をカスタマイズする</strong></li>
<p>『add your code here』の下に実際の処理を記述していきます。</p>
<div class="syntax_hilite">
<div id="php-9">
<div class="php">protected <span style="color:#000000; font-weight:bold;">function</span> execute<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$arguments</span> = <a href="http://jp.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#0000FF;">$options</span> = <a href="http://jp.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// initialize the database connection</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$databaseManager</span> = <span style="color:#000000; font-weight:bold;">new</span> sfDatabaseManager<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">configuration</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#0000FF;">$connection</span> = <span style="color:#0000FF;">$databaseManager</span>-&gt;<span style="color:#006600;">getDatabase</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$options</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'connection'</span><span style="color:#006600; font-weight:bold;">&#93;</span> ? <span style="color:#0000FF;">$options</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'connection'</span><span style="color:#006600; font-weight:bold;">&#93;</span> : <span style="color:#000000; font-weight:bold;">null</span><span style="color:#006600; font-weight:bold;">&#41;</span>-&gt;<span style="color:#006600;">getConnection</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// DB アクセスも普通に使える</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$obj</span> = BookPeer::<span style="color:#006600;">retrieveByPK</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://jp.php.net/var_dump"><span style="color:#000066;">var_dump</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$obj</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// ログも便利</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'処理終了'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</div>
</div>
<p></p>
<li><strong>バッチ処理を実行する</strong></li>
<div class="syntax_hilite">
<div id="code-10">
<div class="code">% ./symfony foo:sample</div>
</div>
</div>
<p></p>
<li><strong>引数を使いたいとき</strong></li>
<div class="syntax_hilite">
<div id="php-11">
<div class="php">protected <span style="color:#000000; font-weight:bold;">function</span> configure<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// // add your own arguments here</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">addArguments</span><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://jp.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">new</span> sfCommandArgument<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'my_arg'</span>, sfCommandArgument::<span style="color:#006600;">REQUIRED</span>, <span style="color:#FF0000;">'My argument'</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>configure 関数の先頭のコメントを外すと my_arg 引数が有効になり、execute 関数の引数 $arguments['my_arg'] で利用できるようになります。</p>
<div class="syntax_hilite">
<div id="php-12">
<div class="php">% ./symfony foo:sample aaa<br />
<span style="color:#FF9933; font-style:italic;">// print_r($arguments);</span><br />
<a href="http://jp.php.net/array"><span style="color:#000066;">Array</span></a><br />
<span style="color:#006600; font-weight:bold;">&#40;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span>task<span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; foo:sample<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span>my_arg<span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; aaa<br />
<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
</div>
<p></p>
<li>他にも</li>
<p>symfony/task/project とか<br />
symfony/task/generator とか</p>
<p>symfony のライブラリー内のタスクにサンプルになりそうなファイルがあるので参考にしてください。</p>
<p>参考<br />
<a href="http://symfony.xrea.jp/1.1/book/16-Application-Management-Tools.html#custom.tasks.new.in.symfony.1.1">第16章 - アプリケーションの運用ツール<br />
</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sylvan-l.net/b/2009/01/14/symfony12%e3%81%a7%e3%83%90%e3%83%83%e3%83%81%e5%87%a6%e7%90%86%e3%82%92%e4%bd%9c%e3%82%8b%e3%81%ab%e3%81%af%e3%81%a9%e3%81%86%e3%81%99%e3%82%8c%e3%81%b0%e3%81%84%e3%81%84%e3%81%ae%ef%bc%9f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://www.sylvan-l.net/b/2009/01/14/symfony12%e3%81%a7%e3%83%90%e3%83%83%e3%83%81%e5%87%a6%e7%90%86%e3%82%92%e4%bd%9c%e3%82%8b%e3%81%ab%e3%81%af%e3%81%a9%e3%81%86%e3%81%99%e3%82%8c%e3%81%b0%e3%81%84%e3%81%84%e3%81%ae%ef%bc%9f/" />
	</item>
	</channel>
</rss>

