<?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/"
	>

<channel>
	<title>Flexmaniak.pl &#187; Zend_Amf</title>
	<atom:link href="http://blog.flexmaniak.pl/tag/zend_amf/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.flexmaniak.pl</link>
	<description>Flex, Papervision3D, PHP, Symfony</description>
	<lastBuildDate>Sun, 02 Oct 2011 08:36:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>[Zend_Amf + Symfony + Flex] Server with Logger</title>
		<link>http://blog.flexmaniak.pl/flex-and-php/zend_amf-symfony-flex-server-with-logger</link>
		<comments>http://blog.flexmaniak.pl/flex-and-php/zend_amf-symfony-flex-server-with-logger#comments</comments>
		<pubDate>Sun, 18 Jul 2010 19:50:57 +0000</pubDate>
		<dc:creator>damian</dc:creator>
				<category><![CDATA[Flex and PHP]]></category>
		<category><![CDATA[amf server]]></category>
		<category><![CDATA[Flex Halo]]></category>
		<category><![CDATA[logger]]></category>
		<category><![CDATA[logs]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[symfony]]></category>
		<category><![CDATA[Zend_Amf]]></category>
		<category><![CDATA[zend_amf_server]]></category>

		<guid isPermaLink="false">http://blog.flexmaniak.pl/?p=878</guid>
		<description><![CDATA[After integrating Symfony with Flex through Zend_Amf, I had to debug it and I needed some logs on server side. Below I present my LoggedAmfServer extending Zend_Amf_Server, which logs all methods called by Flex and its&#8217; responses. You need to pass instance of sfLogger (ie. sfFileLogger) in the constructor. private $logger = null; public function [...]]]></description>
			<content:encoded><![CDATA[<p>After integrating Symfony with Flex through Zend_Amf, I had to debug it and I needed some logs on server side.<br />
Below I present my <strong>LoggedAmfServer</strong> extending Zend_Amf_Server, which logs all methods called by Flex and its&#8217; responses. You need to pass instance of sfLogger (ie. sfFileLogger) in the constructor.</p>
<p><span id="more-878"></span></p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$logger</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span>sfLogger <span style="color: #000088;">$l</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; logger <span style="color: #339933;">=</span> <span style="color: #000088;">$l</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _handle<span style="color: #009900;">&#40;</span>Zend_Amf_Request <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$responseBody</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAmfBodies</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/current"><span style="color: #990000;">current</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$responseBody</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$body</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> instanceof Zend_Amf_Value_Messaging_RemotingMessage<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">logger</span><span style="color: #339933;">-&gt;</span><a href="http://www.php.net/log"><span style="color: #990000;">log</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/sprintf"><span style="color: #990000;">sprintf</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;[Amf Request] CLIENT: [<span style="color: #009933; font-weight: bold;">%s</span>] OPERATION: [<span style="color: #009933; font-weight: bold;">%s</span>]. PARAMETERS: [<span style="color: #009933; font-weight: bold;">%s</span>].&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clientId</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">operation</span><span style="color: #339933;">,</span> <a href="http://www.php.net/var_export"><span style="color: #990000;">var_export</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; parent<span style="color: #339933;">::</span>_handle<span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handle<span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">handle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/current"><span style="color: #990000;">current</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAmfBodies</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$body</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">logger</span><span style="color: #339933;">-&gt;</span><a href="http://www.php.net/log"><span style="color: #990000;">log</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/sprintf"><span style="color: #990000;">sprintf</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;[Amf Response] CLIENT: [<span style="color: #009933; font-weight: bold;">%s</span>] RESPONSE: [<span style="color: #009933; font-weight: bold;">%s</span>]&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clientId</span><span style="color: #339933;">,</span> <a href="http://www.php.net/var_export"><span style="color: #990000;">var_export</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$response</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexmaniak.pl/flex-and-php/zend_amf-symfony-flex-server-with-logger/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Symfony i Flex przez Zend_Amf</title>
		<link>http://blog.flexmaniak.pl/flex-and-php/symfony-i-flex-przez-zend_amf</link>
		<comments>http://blog.flexmaniak.pl/flex-and-php/symfony-i-flex-przez-zend_amf#comments</comments>
		<pubDate>Mon, 31 May 2010 12:05:27 +0000</pubDate>
		<dc:creator>damian</dc:creator>
				<category><![CDATA[Flex and PHP]]></category>
		<category><![CDATA[Amf]]></category>
		<category><![CDATA[Flex Halo]]></category>
		<category><![CDATA[symfony]]></category>
		<category><![CDATA[Zend_Amf]]></category>

		<guid isPermaLink="false">http://blog.flexmaniak.pl/?p=389</guid>
		<description><![CDATA[Szeroki wybór możliwości połączenia PHP i Flexa możemy uważać za coś pozytywnego, ponieważ każdy może sobie odnaleźć swój sposób integracji server-side z client-side. Jednakże na długą metę okazuje się to zmorą, bo człowiek (przynajmniej ja tak mam) zaczyna się zastanawiać, czy to drugie rozwiązanie nie jest lepsze. Dlatego postanowiłem znaleźć jakiś sposób integracji, który będzie [...]]]></description>
			<content:encoded><![CDATA[<p>Szeroki wybór możliwości połączenia PHP i Flexa możemy uważać za coś pozytywnego, ponieważ każdy może sobie odnaleźć swój sposób integracji server-side z client-side. Jednakże na długą metę okazuje się to zmorą, bo człowiek (przynajmniej ja tak mam) zaczyna się zastanawiać, czy to drugie rozwiązanie nie jest lepsze.</p>
<p>Dlatego postanowiłem znaleźć jakiś sposób integracji, który będzie szybki i dobry :). Przeglądając różne stronki o Flexie, odnalazłem prezentację (niestety nie pamiętam adresu), na której ładnie przedstawiony był fakt, że <strong>AMF</strong> to idealny protokół komunikacji z Flexem. Nic dziwnego, w końcu to twór Adobe.</p>
<p><span id="more-389"></span></p>
<p>Ok, protokół mamy. Teraz wypada wybrać implementację. Jako, że jestem użytkownikiem frameworka Symfony, to właśnie z nim chciałbym zintegrować Flexa.</p>
<p>Wiem, że istnieje plugin do symfony <strong>sfAmfPlugin</strong> (<a href="http://www.symfony-project.org/plugins/sfAmfPlugin">strona głowna</a>). Nie będe się o nim rozpisywał. Jak ktoś jest ciekawy, to niech śmiga na jego stronę główną. Powiem tylko, że jest on oparty na bibliotece <strong>SabreAMF</strong>, której szczerze mówiąc nie znam. Wiem natomiast, że biblioteka Zend&#8217;a jest dobra do wsyztskiego :), dlatego właśnie z niej korzystam.</p>
<p>Poniżej przedstawiam opis jak zrobić szybko HelloWorld we Flexie, z wykorzystaniem Symfony i AMF oraz bibliotek Zenda. We wszystkich HelloWorld&#8217;ach w necie brakowało mi tego połączenia.</p>
<p><strong>Zaczynamy</strong></p>
<p>Na początku trzeba postawić zwykły projekt Symfony i dodać jakąś aplikację &#8211; ja dodałem aplikacje o nazwie <strong>frontend</strong>. Zakładam, że ludzie czytający wpis o symfony wiedzą jak to zrobić :)</p>
<p>Następnie należy zintegrować odpowiednie biblioteki Zenda. Integracja została opisana we wpisie <a title="Integracja Symfony + Zend" href="http://blog.flexmaniak.pl/flex_i_php/integracja-symfony-zend">Integracja Symfony + Zend</a>.</p>
<p>Oto lista plików jakie należy umieścić w katalogu /lib/vendor/Zend:</p>
<ul>
<li>Amf/</li>
<li>Loader/</li>
<li>Server/</li>
<li>Auth.php</li>
<li>Loader.php</li>
<li>Exception.php</li>
</ul>
<p>Następnie tworzymy moduł <strong>amf</strong>, w którym będzie nasz Zend_Amf_Endpoint, czyli miejsce do którego będziemy się odwoływać z Flexa.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">symfony generate:module frontend amf</div></div>
<p>Ja umieściłem swój endpoint w akcji server (tak dla niepoznaki ;)). Oto kod akcji:</p>
<pre class="brush:php">public function executeServer(sfWebRequest $request) {

$server = new Zend_Amf_Server();

$server-&gt;setClass('helloService');

$this-&gt;renderText($server-&gt;handle());
return sfView::NONE;
}</pre>
<p>Nic innego jak stworzenie instancji serwera, przypisanie mu klas, które są service&#8217;ami oraz wyświetlenie wyniku metody handle(). Istotne jest też wyłączenie renderowania template&#8217;u.</p>
<p><strong>Services</strong></p>
<p>Jak pisałem w opisie akcji, serwer Zend_Amf_Server składa się z klas (lub funkcji). Najwygodniejszym sposobem jest stworzenie oddzielnych klas, które będziemy traktować jako przestrzenie nazw. Poniżej nasza klasa dla helloWorld.</p>
<pre class="brush:php">class helloService {

/**
* Method returns hello mesage
*
* @return string
*/
public function helloWorld() {
return 'hello world';
}

}</pre>
<p>Jak widać service <strong>hello</strong> posiada tylko jedną metodę bezparametrową <strong>helloWorld()</strong>, która zwraca string. AMF to potęga bo mapuje typy :), więc we Flexie nie trzeba będzie parsować odpowiedzi, jak by to miało miejsce w przypadku np. XML czy JSON. Należy jednak określic w PHPDoc (nad metodą) wszystkie parametry oraz zwracany typ. Dobrą praktyką jest też krótkie opisanie działania metody.</p>
<p><strong>Flex</strong></p>
<p>Przed rozpoczęciem implementowania trzeba dodać do projektu (do folderu src) plik services-config.xml, który jest konfiguracją połączenie Flexa z endpointem. Założmy, że nasz serwer amf znajduje się pod adresem:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">http://localhost/helloServer/amf/server</div></div>
<p>Wtedy przykładowy plik services-config.xml wygląda następująco</p>
<pre class="brush:xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;services-config&gt;
&lt;services&gt;
&lt;service id="amfphp-flashremoting-service" class="flex.messaging.services.RemotingService" messageTypes="flex.messaging.messages.RemotingMessage"&gt;
&lt;destination id="amfphp"&gt;
&lt;channels&gt;
&lt;channel ref="my-amfphp"/&gt;
&lt;/channels&gt;
&lt;properties&gt;
&lt;source&gt;*&lt;/source&gt;
&lt;/properties&gt;
&lt;/destination&gt;
&lt;/service&gt;
&lt;/services&gt;

&lt;channels&gt;
&lt;channel-definition id="my-amfphp"  class="mx.messaging.channels.AMFChannel"&gt;
&lt;endpoint  uri="http://localhost/helloServer/amf/server"  class="flex.messaging.endpoints.AMFEndpoint"/&gt;
&lt;/channel-definition&gt;
&lt;/channels&gt;
&lt;/services-config&gt;</pre>
<p>Nastepnie należy wejść do ustawień projektu. Przejść do &#8220;Flex compiler&#8221; i w polu &#8220;Additonal compiler arguments&#8221; dopisać:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-services &quot;sciezka-do-folderu/services-config.xml&quot;</div></div>
<p><strong>Komunikacja</strong></p>
<p>Z poziomu flexa do komunikacji będziemy wykorzystywać klasę <strong>RemoteObject</strong>. Poniżej przedstawiam przykład klasy fasady do komunikacji z serwerem PHP oraz jej wykorzystanie w przykładowej aplikacji. Fasada posiada obiekt RemoteObject, do którego mamy dostęp zabroniony. Wszelkie zapytania do serwera AMF będą przechodziły przez nasza fasadę.</p>
<pre class="brush:js">public class ServerFacade {

private var helloService:RemoteObject = null;

public function ServerFacade() {
//połączenie z endpointem oznaczonym w pliku services-config.xml
helloService = new RemoteObject("amfphp");
helloService.showBusyCursor = true;
//przypisanie nazwy klasy do źródła
helloService.source = "helloService";
//określenie co ma sie dziać jak wystąpi błąd
helloService.addEventListener(FaultEvent.FAULT, globalFault);

//przypisanie Listenerów, które są wywoływaja jak zostanie zwrócona wartość z serwera
addApiListeners();
}

//METHODS
public function hello_helloWorld():void {
helloService.helloWorld();
}

//HELLO_WORLD
public var hello_helloWorld_resultHandler:Function = null;
private function hello_helloWorld_internalResultHandler(event:ResultEvent):void {
if (hello_helloWorld_resultHandler != null) {
hello_helloWorld_resultHandler(event);
}
}

//PRIVATE

private function addApiListeners():void {
helloService.helloWorld.addEventListener(ResultEvent.RESULT, hello_helloWorld_internalResultHandler);
}

private function globalFault(event:FaultEvent):void {
Alert.show(event.fault.message, "Fault: " + event.type);
}

}</pre>
<p>Jak widać funkcja helloWorld została połączona z dwoma resultHandlerami. Jest to mój sposób implementacji asynchronicznych zapytań do serwera. Oczywiście asynchroniczność powoduje, że nie otrzymujemu wyniku od razu, tylko musimy dodać Listener&#8217;a, który obsłuży zwracany wynik (w bliżej nieokreślonym czasie).</p>
<p>Wewnętrzny handler (<em>internalHandler</em>)<em> </em>nasłuchuje na zdarzenie. Gdy je przechwyci:</p>
<ol>
<li>Wykonuje wewnętrzne akcję (przykład podam poniżej)</li>
<li>Wywołuje funkcję zewnętrzną &#8216;<em>hello_helloWorld_resultHandler</em>&#8216;, jeśli została zdefiniowana przez użytkownika.</li>
</ol>
<p>Przykład zastosowania fasady jest następujący:</p>
<pre class="brush:js">var sF:ServerFacade = new ServerFacade();
sF.hello_helloWorld_resultHandler = function(event:ResultEvent) {
Alert.show(event.result);
}
sF.hello_helloWorld();</pre>
<p>Przed wywołaniem funkcji określamy, co ma zostać wykonane z jej wynikiem &#8211; wyświetlenie w boxie Alert. Następnie wywołujemy funkcję.</p>
<p><strong>Przykładem wykorzystania wewnętrznych handlerów</strong> jest zachowanie stanu sesji uzytkownika. Na przykład w wewnętrznym handlerze akcji logowania można zapisać rolę użytkownika i wynik innych metod odpowiednio modyfikować w zależności od roli zalogowanego użytkownika.</p>
<p>Niestety nie mam tego przykładu jeszcze popartego kodem, ale jak tylko będę mógł pokazać kod, który wykorzystuje taki mechanizm, to umieszczę go na blogu.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexmaniak.pl/flex-and-php/symfony-i-flex-przez-zend_amf/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

