Niezalogowany [ logowanie ]
Subskrybuj kanał ATOM Kanał ATOM    Subskrybuj kanał ATOM dla tagu symfony Kanał ATOM (tag: symfony)

Autor wpisu: Wojciech Sznapka, dodany: 25.01.2012 19:10, tagi: php, symfony

In my previous post Modern framework comparison I presented performance tests, which compared Ruby On Rails, Django and Symfony2. After recieving a feedback in comments I decided to run this benchmark one more time on my own laptop (instead of on my hosting). The reason was simple: enviroment was outdated. I installed mod_python and configured [...]

Autor wpisu: Tomasz Kowalczyk, dodany: 20.11.2011 18:24, tagi: symfony

Routing w symfony 1.x jest rozwiązany w całkiem przyjemny sposób, jednak jak to zwykle bywa, kiedy próbujemy zrobić coś wbrew ustalonej konwencji, framework szybko sprowadza nas na "jedyną prawdziwą ścieżkę zen tworzenia aplikacji". W dzisiejszym wpisie chciałbym przedstawić jeden z problemów, na jakie m0żemy trafić podczas zabawy z wspomnianym mechanizmem tytułowego frameworka. Fotografia: filipw @ [...]

Autor wpisu: Tomasz Kowalczyk, dodany: 11.11.2011 20:10, tagi: symfony2, doctrine, symfony

Hello everyone! As I am more and more into Symfony2 you can expect a little more about this framework from me. I've already added Symfony2 category on this blog, but there is only one entry, so now I'll try to fix this a little bit. Today I would like to show you an interesting improvement [...]

Autor wpisu: Tomasz Kowalczyk, dodany: 11.11.2011 19:52, tagi: symfony2, doctrine, symfony

Witajcie! Ze względu na to, że ostatnio mam coraz więcej wspólnego z Symfony2, możecie spodziewać się nieco więcej wpisów o tym frameworku na niniejszym blogu. Założyłem już kategorię "Symfony2", a znajduje się w niej raptem jeden wpis, także czas nadrobić to niedociągnięcie. Dzisiaj chciałbym Wam pokazać jedno fajne usprawnienie, które pozwoli nam na lepszą kontrolę [...]

Autor wpisu: Wojciech Sznapka, dodany: 01.11.2011 21:33, tagi: php, symfony, symfony2

Recently I did a lot of Test Driven Development on my Symfony2 bundle. I used PHPUnit’s built-in mocks and stubs for many projects, so I took it again. But while I was working on mocking Symfony2 core objects I found those mocks very uncomfortable in use. I tried Mockery and it saved my day. Let’s [...]

Autor wpisu: Kamil Adryjanek, dodany: 26.10.2011 21:43, tagi: symfony, php

Default symfony 1.4 file validator can validate only some basic file attributes. Sometimes we need more, for example we want to prevent small images from being uploaded. Code below presents my custom sfImageFileValidator which allows you to apply some dimensions constraints to image file validation, e.g. max_height or max_width.

&lt;/p&gt;<br />
&lt;p&gt;&amp;lt;?php&lt;/p&gt;<br />
&lt;p&gt;/**&lt;br /&gt;<br />
 * sfImageFileValidator allows you to apply constraints to image file upload, it extend the sfFileValidator functions.&lt;br /&gt;<br />
 *&lt;br /&gt;<br />
 * @author     Kamil Adryjanek &amp;lt;kamil.adryjanek@gmail.com&amp;gt;&lt;br /&gt;<br />
 */&lt;br /&gt;<br />
class sfImageFileValidator extends sfValidatorFile&lt;br /&gt;<br />
{&lt;br /&gt;<br />
    /**&lt;br /&gt;<br />
     * Configures the current validator.&lt;br /&gt;<br />
     *&lt;br /&gt;<br />
     * Available options:&lt;br /&gt;<br />
     *&lt;br /&gt;<br />
     *  * max_height:             The maximum file height in pixels&lt;br /&gt;<br />
     *  * min_height:             The minimum file height in pixels&lt;br /&gt;<br />
     *  * max_width:              The maximum file width in pixels&lt;br /&gt;<br />
     *  * min_width:              The minimum file width in pixels&lt;br /&gt;<br />
     *&lt;br /&gt;<br />
     * Available error codes:&lt;br /&gt;<br />
     *&lt;br /&gt;<br />
     *  * max_height&lt;br /&gt;<br />
     *  * min_height&lt;br /&gt;<br />
     *  * max_width&lt;br /&gt;<br />
     *  * min_width&lt;br /&gt;<br />
     *&lt;br /&gt;<br />
     * @param array $options   An array of options&lt;br /&gt;<br />
     * @param array $messages  An array of error messages&lt;br /&gt;<br />
     *&lt;br /&gt;<br />
     * @see sfValidatorBase&lt;br /&gt;<br />
     */&lt;br /&gt;<br />
    public function configure($options = array(), $messages = array())&lt;br /&gt;<br />
    {&lt;br /&gt;<br />
        parent::configure($options, $messages);&lt;/p&gt;<br />
&lt;p&gt;        $this-&amp;gt;addOption(&amp;amp;#039;max_height&amp;amp;#039;);&lt;br /&gt;<br />
        $this-&amp;gt;addOption(&amp;amp;#039;min_height&amp;amp;#039;);&lt;br /&gt;<br />
        $this-&amp;gt;addOption(&amp;amp;#039;max_width&amp;amp;#039;);&lt;br /&gt;<br />
        $this-&amp;gt;addOption(&amp;amp;#039;min_width&amp;amp;#039;);&lt;/p&gt;<br />
&lt;p&gt;        $this-&amp;gt;addMessage(&amp;amp;#039;max_height&amp;amp;#039;, &amp;amp;#039;File is too high (maximum is %max_height% pixels, %height% given).&amp;amp;#039;);&lt;br /&gt;<br />
        $this-&amp;gt;addMessage(&amp;amp;#039;min_height&amp;amp;#039;, &amp;amp;#039;File is too short (minimum is %min_height% pixels, %height% given).&amp;amp;#039;);&lt;br /&gt;<br />
        $this-&amp;gt;addMessage(&amp;amp;#039;max_width&amp;amp;#039;, &amp;amp;#039;File is too wide (maximum is %max_width% pixels, %width% given).&amp;amp;#039;);&lt;br /&gt;<br />
        $this-&amp;gt;addMessage(&amp;amp;#039;min_width&amp;amp;#039;, &amp;amp;#039;File is too thin (minimum is %min_width% pixels, %width% given).&amp;amp;#039;);&lt;br /&gt;<br />
    }&lt;/p&gt;<br />
&lt;p&gt;    /**&lt;br /&gt;<br />
     * This validator always returns a sfValidatedFile object.&lt;br /&gt;<br />
     *&lt;br /&gt;<br />
     * The input value must be an array with the following keys:&lt;br /&gt;<br />
     *&lt;br /&gt;<br />
     *  * tmp_name: The absolute temporary path to the file&lt;br /&gt;<br />
     *  * name:     &lt;div style=&quot;display: none&quot;&gt;&lt;a href='http://viagralowestprice.com/'&gt;buy viagra uk&lt;/a&gt;&lt;/div&gt; The original file name (optional)&lt;br /&gt;<br />
     *  * type:     The file content type (optional)&lt;br /&gt;<br />
     *  * error:    The error code (optional)&lt;br /&gt;<br />
     *  * size:     The file size in bytes (optional)&lt;br /&gt;<br />
     *&lt;br /&gt;<br />
     * @see sfValidatorBase&lt;br /&gt;<br />
     */&lt;br /&gt;<br />
    protected function doClean($value)&lt;br /&gt;<br />
    {&lt;br /&gt;<br />
        if (!is_array($value) || !isset($value[&amp;amp;#039;tmp_name&amp;amp;#039;])) {&lt;br /&gt;<br />
            throw new sfValidatorError($this, &amp;amp;#039;invalid&amp;amp;#039;, array(&amp;amp;#039;value&amp;amp;#039; =&amp;gt; (string) $value));&lt;br /&gt;<br />
        }&lt;/p&gt;<br />
&lt;p&gt;        // get image dimensions&lt;br /&gt;<br />
        list($wi&amp;lt;div style=&amp;quot;display: none&amp;quot;&amp;gt;&amp;lt;a href=&amp;quot;http://isidoreguest.devhub.com/blog/1440105-how-do-you-know-when-your-sons-father-wants-you-back/&amp;quot;&amp;gt;How To Make Ex Want You Back Leave The Girl&amp;lt;/a&amp;gt;&amp;lt;/div&amp;gt;dth, $height) = getimagesize($value[&amp;amp;#039;tmp_name&amp;amp;#039;]);&lt;/p&gt;<br />
&lt;p&gt;        // check file height&lt;br /&gt;<br />
        if ($this-&amp;gt;hasOption(&amp;amp;#039;max_height&amp;amp;#039;) &amp;amp;&amp;amp; $this-&amp;gt;getOption(&amp;amp;#039;max_height&amp;amp;#039;) &amp;lt; (int) $height) {&lt;br /&gt;<br />
            throw new sfValidatorError($this, &amp;amp;#039;max_height&amp;amp;#039;, array(&amp;amp;#039;max_height&amp;amp;#039; =&amp;gt; $this-&amp;gt;getOption(&amp;amp;#039;max_height&amp;amp;#039;), &amp;amp;#039;height&amp;amp;#039; =&amp;gt; (int) $height));&lt;br /&gt;<br />
        }&lt;/p&gt;<br />
&lt;p&gt;        if ($this-&amp;gt;hasOption(&amp;amp;#039;min_height&amp;amp;#039;) &amp;amp;&amp;amp; $this-&amp;gt;getOption(&amp;amp;#039;min_height&amp;amp;#039;) &amp;gt; (int) $height) {&lt;br /&gt;<br />
            throw new sfValidatorError($this, &amp;amp;#039;min_height&amp;amp;#039;, array(&amp;amp;#039;min_height&amp;amp;#039; =&amp;gt; $this-&amp;gt;getOption(&amp;amp;#039;min_height&amp;amp;#039;), &amp;amp;#039;height&amp;amp;#039; =&amp;gt; (int) $height));&lt;br /&gt;<br />
        }&lt;/p&gt;<br />
&lt;p&gt;        <div style="display: none"><a href='http://online-cialis-buy.com/'>cialis vs levitra</a></div> // check file width&lt;br /&gt;<br />
        if ($this-&amp;gt;hasOption(&amp;amp;#039;max_width&amp;amp;#039;) &amp;amp;&amp;amp; $this-&amp;gt;getOption(&amp;amp;#039;max_width&amp;amp;#039;) &amp;lt; (int) $width) {&lt;br /&gt;<br />
            throw new sfValidatorError($this, &amp;amp;#039;max_width&amp;amp;#039;, array(&amp;amp;#039;max_width&amp;amp;#039; =&amp;gt; $this-&amp;gt;getOption(&amp;amp;#039;max_width&amp;amp;#039;), &amp;amp;#039;width&amp;amp;#039; =&amp;gt; (int) $width));&lt;br /&gt;<br />
        }&lt;/p&gt;<br />
&lt;p&gt;        if ($this-&amp;gt;hasOption(&amp;amp;#039;min_width&amp;amp;#039;) &amp;amp;&amp;amp; $this-&amp;gt;getOption(&amp;amp;#039;min_width&amp;amp;#039;) &amp;gt; (int) $width) {&lt;br /&gt;<br />
            throw new sfValidatorError($this, &amp;amp;#039;min_width&amp;amp;#039;, array(&amp;amp;#039;min_width&amp;amp;#039; =&amp;gt; $this-&amp;gt;getOption(&amp;amp;#039;min_width&amp;amp;#039;), &amp;amp;#039;width&amp;amp;#039; =&amp;gt; (int) $width));&lt;br /&gt;<br />
        }&lt;/p&gt;<br />
&lt;p&gt;        // check other options&lt;br /&gt;<br />
        return parent::doClean($value);&lt;br /&gt;<br />
    }&lt;br /&gt;<br />
}&lt;br /&gt;<br />
?&amp;gt;&lt;/p&gt;<br />
&lt;p&gt;

How to use this code?

&lt;/p&gt;<br />
&lt;p&gt;&amp;lt;?php&lt;/p&gt;<br />
&lt;p&gt;class PhotoForm extends sfForm&lt;br /&gt;<br />
{&lt;br /&gt;<br />
    public function configure()&lt;br /&gt;<br />
    {&lt;/p&gt;<br />
&lt;p&gt;        //...&lt;/p&gt;<br />
&lt;p&gt;        $this-&amp;gt;widgetSchema[&amp;amp;#039;photo&amp;amp;#039;] = new sfWidgetFormInputFileEditable(array(&lt;br /&gt;<br />
            &amp;amp;#039;label&amp;amp;#039; =&amp;gt; &amp;amp;#039;User photo&amp;amp;#039;,&lt;br /&gt;<br />
            &amp;amp;#039;file_src&amp;amp;#039; =&amp;gt; &amp;amp;#039;/uploads/photos/&amp;amp;#039;.$this-&amp;gt;getObject()-&amp;gt;getPhoto(),&lt;br /&gt;<br />
            &amp;amp;#039;is_image&amp;amp;#039; =&amp;gt; true,&lt;br /&gt;<br />
            &amp;amp;#039;edit_mode&amp;amp;#039; =&amp;gt; !$this-&amp;gt;isNew(),&lt;br /&gt;<br />
            &amp;amp;#039;template&amp;amp;#039; =&amp;gt; &amp;amp;#039;&amp;lt;div&amp;gt;%file%&amp;lt;br /&amp;gt;%input%&amp;lt;br /&amp;gt;%delete% %delete_label%&amp;lt;/div&amp;gt;&amp;amp;#039;,&lt;br /&gt;<br />
        ));&lt;/p&gt;<br />
&lt;p&gt;        $this-&amp;gt;validatorSchema[&amp;amp;#039;photo&amp;amp;#039;] = new sfImageFileValidator(array(&lt;br /&gt;<br />
                &amp;amp;#039;required&amp;amp;#039;        =&amp;gt; true,&lt;br /&gt;<br />
                &amp;amp;#039;mime_types&amp;amp;#039;    =&amp;gt; array(&amp;amp;#039;image/jpeg&amp;amp;#039;, &amp;amp;#039;image/png&amp;amp;#039;, &amp;amp;#039;image/gif&amp;amp;#039;, &amp;amp;#039;image/pjpeg&amp;amp;#039;),&lt;br /&gt;<br />
                &amp;amp;#039;max_size&amp;amp;#039;        =&amp;gt; &amp;amp;#039;1048576&amp;amp;#039;,&lt;br /&gt;<br />
                &amp;amp;#039;min_height&amp;amp;#039;    =&amp;gt; &amp;amp;#039;640&amp;amp;#039;,&lt;br /&gt;<br />
                &amp;amp;#039;min_width&amp;amp;#039;        =&amp;gt; &amp;amp;#039;480&amp;amp;#039;,&lt;br /&gt;<br />
                &amp;amp;#039;path&amp;amp;#039;            =&amp;gt; sfConfig::get(&amp;amp;#039;sf_upload_dir&amp;amp;#039;).&amp;amp;#039;/photos&amp;amp;#039;,&lt;br /&gt;<br />
            ), array(&lt;br /&gt;<br />
                &amp;amp;#039;required&amp;amp;#039;        =&amp;gt; &amp;quot;Photo is required!&amp;quot;,&lt;br /&gt;<br />
                &amp;amp;#039;min_height&amp;amp;#039;    =&amp;gt; &amp;quot;Custom message for height vaidation.&amp;quot;,&lt;br /&gt;<br />
                &amp;amp;#039;min_width&amp;amp;#039;        =&amp;gt; &amp;quot;File is too thin (minimum is %min_width% pixels, %width% given).&amp;quot;&lt;br /&gt;<br />
        ));&lt;/p&gt;<br />
&lt;p&gt;        $this-&amp;gt;validatorSchema[&amp;amp;#039;photo_delete&amp;amp;#039;] = new sfValidatorPass();&lt;/p&gt;<br />
&lt;p&gt;        //…&lt;/p&gt;<br />
&lt;p&gt;    }&lt;br /&gt;<br />
}&lt;/p&gt;<br />
&lt;p&gt;?&amp;gt;&lt;br /&gt;<br />

How To Make Ex Want You Back Leave The Girl zp8497586rq

Autor wpisu: Wojciech Sznapka, dodany: 23.10.2011 13:44, tagi: php, symfony, symfony2

Generally, it’s not a good idea to unit test protected or private methods, but in some cases it could be useful. Of course we don’t want to change our class contract, and expose those methods as a public ones, just becasue we want to test them with PHPUnit. This case can’t be solved with Proxy-Object [...]
Wszystkie wpisy należą do ich twórców. PHP.pl nie ponosi odpowiedzialności za treść wpisów.