2011年11月1日火曜日

symfony2でキャリアの判別

キャリアというかuser_agentというかですけれど、とにかくリクエストからユーザーエージェントをを調べて利用している端末を調べてガラパゴス携帯なら文字のエンコードをshift-jisで吐き出す処理。

関連するphpファイル

#\src\Acme\PagesBundle\DependencyInjection\Carrier.php

namespace Acme\PagesBundle\DependencyInjection;

use Symfony\Component\HttpFoundation\Request;

class Carrier
{
 protected $agent;
 protected $carrier_type;

 public function __construct(Request $request)
 {
  $this->agent = $request->server->get('HTTP_USER_AGENT');
  $this->initialize();
 }

 public function initialize()
 {
  $this->carrier_type = "pc";

  if(preg_match("/^DoCoMo/i", $this->agent)){
   $this->carrier_type = "docomo";
  }else if(preg_match("/^(J\-PHONE|Vodafone|MOT\-[CV]|SoftBank)/i", $this->agent)){
   $this->carrier_type = "softbank";
  }else if(preg_match("/^KDDI\-/i", $this->agent) || preg_match("/UP\.Browser/i", $this->agent)){
   $this->carrier_type = "au";
  }else if(preg_match("/iPad/i",$this->agent)){
   $this->carrier_type = "ipad";
  }else if(preg_match("/iPhone/i",$this->agent)){
   $this->carrier_type = "iphone";
  }else if(preg_match("/Galaxy/i",$this->agent)){
   $this->carrier_type = "iphone";
  }else if(preg_match("/Android/i",$this->agent)){
   $this->carrier_type = "android";
  }
 }

 public function setCarrierType($type)
 {
  $this->carrier_type = $type;
 }

 public function isDocomo()
 {
  if($this->carrier_type === "docomo"){
   return true;
  }else{
   return false;
  }
 }

 public function isSoftbank()
 {
  if($this->carrier_type === "softbank"){
   return true;
  }else{
   return false;
  }
 }

 public function isAu()
 {
  if($this->carrier_type === "au"){
   return true;
  }else{
   return false;
  }
 }

 public function isIpad()
 {
  if($this->carrier_type === "ipad"){
   return true;
  }else{
   return false;
  }
 }

 public function isIphone()
 {
  if($this->carrier_type === "iphone"){
   return true;
  }else{
   return false;
  }
 }

 public function isAndroid()
 {
  if($this->carrier_type === "android"){
   return true;
  }else{
   return false;
  }
 }

 public function isPc()
 {
  if($this->carrier_type === "pc"){
   return true;
  }else{
   return false;
  }
 }

 public function isLegacy()
 {
  if($this->isDocomo() || $this->isSoftbank() || $this->isAu()){
   return true;
  }else{
   return false;
  }
 }

 public function isSmartphone()
 {
  if($this->isIphone() || $this->isAndroid()){
   return true;
  }else{
   return false;
  }
 }

 public function isSmartpad()
 {
  if($this->isIpad()){
   return true;
  }else{
   return false;
  }
 }
}


#\src\Acme\PagesBundle\Controller\PagesContller.php

namespace Acme\PagesBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Acme\PagesBundle\DependencyInjection\Carrier;


class PagesController extends Controller
{

 public function indexAction()
 {
  //リクエストを取得
  $request = $this->getRequest();
  //キャリアのインスタンスを生成
  $carrier = new Carrier($request);
  //ガラパゴス携帯か判断する
  if($carrier->isLegacy()){
   $content = $this->renderView('AcmePagesBundle:Pages:legacy_index.html.twig');
   $content = mb_convert_encoding($content, "SJIS", "auto");
   return new Response($content);
  }
  return $this->render('AcmePagesBundle:Pages:pc_index.html.twig');


あと当たり前ですが、これとは別にビューを用意します。

...っていうか本当にshift-jisに変わってるのかな?今更ながら不安になってきた。

0 件のコメント:

コメントを投稿