sean hess

  • Twitter
  • LinkedIn
  • i.TV
    • 0
      11 Aug 2009

      Client-Side Flex Logging with Shared Object

      • Edit
      • Delete
      • Tags
      • Autopost

      Having your app keep a detailed log can be indispensable for figuring out tricky user-reported bugs. This is easy on the server-side, where you can use any logging framework or just write to a file. Flash includes the trace function, which is useful while developing, but nearly impossible to get from an end-user. Flex, however, has a good logging api that lets you put log statements anywhere you want. I’ve always wanted the interface to be as easy as trace, so I write a couple of scripts to make it easier.

       

      Flog (Flex Logging) on Github
      log.as
      SharedObjectTarget.as

       

      There are two files in there now. The first is log.as. I didn’t realize before that it was possible to define your own package-level functions. Some might disagree with me, but I think this syntax is a lot nicer than the default. It looks just like trace. All it does is create an easy way to access the flex logging api.

      import net.seanhess.flog.log;
      
      function stuff():void
      {
          log("Executing stuff");
          // do stuff 
          log("Finished!");
      }

      You can replace all your trace statements with these. It’s easy enough to add a trace target to the logging api, so all log messages are traced. See the Flex Logging API for more details, but here’s how you would do it.

      import mx.logging.Log;
      import mx.logging.targets.TraceTarget;
      
      // Only once in your app
      function addLoggingTargets():void
      {
          Log.addTarget(new TraceTarget());
      }

      The second little tool is a new logging target. I wanted a way to log to a local file in flex, so the support guys could get an end-user to email it. The only way to do this without hacks is to write to a shared object. It turns out if you write an array of strings to a shared object, it’s easily human-readable. You attach SharedObjectTarget like any other target and away you go.

      import mx.logging.Log;
      import net.seanhess.flog.SharedObjectTarget;
      
      // Only once in your app
      function addLoggingTargets():void
      {
          Log.addTarget(new TraceTarget());
          Log.addTarget(new SharedObjectTarget("MyCustomAppLogging"));        
      }

      The file is located in the application data folder, which is in~/Library/Preferences/Macromedia/Flash Player/#SharedObjects on the mac. (Google it for windows). The target does some fancy stuff like automatically saving if it hasn’t received a log message for one second. It also automatically rotates messages once the size reaches about 50k (The default allowed size for shared objects is 100k).

      There’s more that can be done here. I might add a debug and an error function. You could easily write an app to send the contents to a server too.

      For alternatives and more information look at the approaches of Tom Barker, Virgil Cristea, and John Wilker.


      Tomasz
      Hi!

      Your library seems to be just what I've been looking for. The only question remaining is the license under which it has been published. Could you please add some licensing information together with the sources?

      • views
      • Tweet
      • Tweet
    • 3
      3 Aug 2009

      How to write your own PHP templating engine

      • Edit
      • Delete
      • Tags
      • Autopost

      MVC is all the rage. Choosing a framework, however, is not so easy. The view part of a standard php MVC framework usually involves grabbing one or more model objects and printing out some text (html) about them. There are several frameworks that do this, including the famous smarty.There are even more frameworks, like Zend and CakePHP that include templating as a subset of what they do.

      You may not have realized that PHP is itself a templating engine. It allows you to intersperse logic and text, and has some handy features to make it easy. I’m going to show you how to build a php-based templating framework in one file.

      Please read Template Engines and Beyond Template Engine for background and other approaches to the same thing. They specifically address why you would want to use PHP instead of another layer for templating.

      The final product

       

      SimpleTemplate.php

       

      post.php?id=2

      <?php
      
          require_once('SimpleTemplate.php');
      
          $post = new BlogPost($_GET["id"]); // pretend we're fetching a post from a db
          $main = new SimpleTemplate("templating/post_full.php");
          $main->post = $post;
          $main->comments = $post->comments;
      
          $layout = new SimpleTemplate("templating/main_layout.php");
          $layout->title = "Post - $post->title";
          $layout->content = $main->run();
      
          echo $layout;

      templating/post_full.php

      <h2><?= $post->title ?></h2>
      <p><?= $post->body ?></p>
      <hr>
      <? foreach ($comments as $comment): ?>
      <p>Comment - <?= "$comment->name - $comment-text" ?></p>
      <? endforeach; ?>
      <!-- Add a comment form here -->

      templating/main_layout.php

      <html>
      <head>
          <!-- some script tags, css, etc -->
          <title><?= $title ?></title>
      </head>
      <body>
          <h1>This is my site</h1>
          <?= $content ?>
      </body>
      </html>

      It’s simple to use, and requires nothing but basic php skills (maybe with the addition of knowing you can do that colon trick with loops).

      How to make one?

      It’s simple, really. First let’s create the class, and add 3 fields. source will store the variables, pathis the path to the template, and result contains the generated result.

      class SimpleTemplate
      {
          public $source;
          public $path;
          public $result;
      }

      The main essential feature is that the engine include another php script and capture its output. You can easily do that with php’s output buffer functions. Let’s create a “run” function that will return the template’s content. Anything printed or echoed between ob_start() andob_get_contents() will be stored in $this->result.

      public function run()
      {
          ob_start();                         
          extract ($this->source); 
          include $this->path;
          $this->result = ob_get_contents();
          ob_end_clean();
          return $this->result;
      }

      Next we need a way to store some variables so that extract call works. extract takes the variables available in a certain scope and empties them into the current scope. So, we can set$template->title = "a title"; and that variable will be available in our template as $title. We’ll implement php’s magic __set() and __get() to do this.

      public function __set($name, $value)
      {
          $this->source[$name] = $value;
      }
      
      public function __get($name)
      {
          return isset($this->source[$name]) ? $this->source[$name] : "";
      }

      Now that extract call will work as explained above. Finally, let’s add a __toString() method so we can just print $template; and use string concatenation and go nuts. Let’s also add a constructor that stores the $path variable.

      <?php
      
      class SimpleTemplate
      {
          public $source;
          public $path;
          public $result;
      
          public function SimpleTemplate($path=false)
          {
              $this->source = array();
              $this->path($path);
          }
      
          public function __toString()
          {
              return $this->run();
          }
      
          public function __set($name, $value)
          {
              $this->source[$name] = $value;
          }
      
          public function __get($name)
          {
              return isset($this->source[$name]) ? $this->source[$name] : "";
          }
      
          public function run()
          {
              ob_start();
              extract ($this->source);
              include $this->path;
              $this->result = ob_get_contents();
              ob_end_clean();
              return $this->result;
          }
      }

      You can get more fancy if you like. In the full example I added parents for templates, meaning they can access variables from their parent’s scope. I also added the extract method, which copies all the variables in an object into the scope (it saves a few characters).

       

       

       

      Andy
      Thanks a lot! This helped me develop the view component for a MVC framework I'm working on.

      • views
      • Tweet
      • Tweet
    • 0
      18 Jun 2009

      RedBean - the simplest (php) ORM in the world

      • Edit
      • Delete
      • Tags
      • Autopost

      I’ve used ActiveRecord and similar ORMs in a variety of languages, but I have never seen a framework as elegant as Gabor de Mooij’s RedBean. It not only makes manipulating and reading records from a database easy, it makes new changes to the database completely invisible.

      http://redbeanphp.com/

      The real innovation was to allow the database to run in development mode, during which it alters the database any time your model structure changes. It generates the classes for you, so you don’t even have to write the actual models.

      R::gen("BlogPost");
      
      $post = new BlogPost();
      $post->title = "Title";
      $post->description = "description";
      $post->save();

      This will generate a blogpost table, and save your record for you, without having to run any migrations or anything. We’ve been using RedBean with ZendAMF on our projects and I feel very productive. Here is a more complete example.

      <?php
      
      require_once('libs/redbean/oodb.php');
      
      R::gen("BlogPost","Comment");
      
      $post = new BlogPost();
      $post->title = "New Blog Title";
      $post->description = "This is a sweet message and stuff";
      
      $comment = new Comment();
      $comment->user = "sean";
      $comment->message = "I think this post is great";
      
      $post->add($comment);
      
      // we're just loading the 1st on the db, but you could
      // find a particular one if you wanted. 
      $currentPost = new BlogPost(1);  
      $comments = $currentPost->getRelatedComment();
      
      // Print them out in a view

      Original Comments

      billeater.com
      Just found this myself. I had started using Propel, largely because it seemed simpler than Doctrine.

      I got frustrated with the amount of config files, and the semi-manual effort to reverse engineer my existing schema.

      So, even apart from the unique 'dev mode', it's a winner. No config files, and no overly verbose getters/setters.

      Very, very cool. 

      Should have been easier to find though. Searches for "php orm" should place it higher...I suppose word of mouth will get around soon enough.

       

      • views
      • Tweet
      • Tweet
    • Search

    • Sites I Like

      • Lessons Learned
      • Both Sides of the Table 2x Entrepreneur turned VC
    • Tags

      • tutorial
      • flex
      • php
      • ruby
      • component
      • sinatra
      • agile
      • code
      • git
      • iphone
      • sequel
      • spark
    • Archive

      • 2012 (1)
        • February (1)
      • 2011 (8)
        • December (1)
        • October (2)
        • June (2)
        • May (2)
        • January (1)
      • 2010 (4)
        • December (2)
        • May (2)
      • 2009 (7)
        • November (1)
        • August (2)
        • July (4)
    • Obox Design
  • sean hess

    Sean is cofounder and CTO of i.TV.

    62454 Views
  • Get Updates

    Follow this Space »
    You're following this Space (Edit)
    You're a contributor here (Edit)
    This is your Space (Edit)
    Follow by email »
    Get the latest updates in your email box automatically.
    Loading...
    Subscribe via RSS
    TwitterLinkedIn