Advertisement
Guest User

EntityManagementGuesser

a guest
May 11th, 2012
1,460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.35 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Acme\HelloBundle\Service;
  4.  
  5. class EntityManagementGuesser
  6. {
  7.  
  8.     /**
  9.      * @var \ReflectionClass
  10.      */
  11.     protected $reflector;
  12.  
  13.     public function inizialize(\Symfony\Bundle\FrameworkBundle\Controller\Controller $controller)
  14.     {
  15.  
  16.         $this->reflector = new \ReflectionClass(get_class($controller));
  17.         return $this;
  18.  
  19.     }
  20.  
  21.     /**
  22.      *
  23.      * @return string
  24.      */
  25.     public function getBundleName()
  26.     {
  27.         return ($p1 = strpos($ns = $this->getNamespace(), '\\')) === false ? $ns :
  28.             substr($ns, 0, ($p2 = strpos($ns, '\\', $p1 + 1)) === false ? strlen($ns) : $p2);
  29.     }
  30.  
  31.     /**
  32.      *
  33.      * @return string
  34.      */
  35.     public function getBundleShortName()
  36.     {
  37.         return str_replace('\\', '', $this->getBundleName());
  38.     }
  39.  
  40.     /**
  41.      *
  42.      * @return string
  43.      */
  44.     public function getNamespace() { return $this->reflector->getNamespaceName(); }
  45.  
  46.     /**
  47.      *
  48.      * @return string
  49.      */
  50.     public function getShortName() { return $this->reflector->getShortName(); }
  51.  
  52.     /**
  53.      *
  54.      * @return string
  55.      */
  56.     public function getName() { return $this->reflector->getName(); }
  57.  
  58.     /**
  59.      *
  60.      * @return string
  61.      */
  62.     public function guessEntityNamespace()
  63.     {
  64.         return ($pos = strrpos($ns = $this->getNamespace(), '\\')) === false ? $ns
  65.             : sprintf("%s\%s", substr($ns, 0, $pos), 'Entity');
  66.     }
  67.  
  68.     /**
  69.      *
  70.      * @return string
  71.      */
  72.     public function guessEntityShortName()
  73.     {
  74.         return ($pos = strpos($short = $this->getShortName(), 'Controller')) === false ? $short :
  75.             substr($short, 0, $pos);
  76.     }
  77.  
  78.     /**
  79.      *
  80.      * @return string
  81.      */
  82.     public function guessEntityName()
  83.     {
  84.         return sprintf('%s\\%s', $this->guessEntityNamespace(), $this->guessEntityShortName());
  85.     }
  86.  
  87.     /**
  88.      *
  89.      * @return string
  90.      */
  91.     public function guessRepositoryNamespace()
  92.     {
  93.         return ($p = strrpos($ns = $this->getNamespace(), '\\')) === false ? $ns
  94.             : sprintf("%s\Repository", substr($ns, 0, $p));
  95.     }
  96.  
  97.     /**
  98.      *
  99.      * @return string
  100.      */
  101.     public function guessRepositoryShortName()
  102.     {
  103.         return sprintf(
  104.             '%sRepository',
  105.             ($pos = strpos($s = $this->getShortName(), 'Controller')) === false ? $s : substr($s, 0, $pos)
  106.         );
  107.     }
  108.  
  109.     /**
  110.      *
  111.      * @return string
  112.      */
  113.     public function guessRepositoryName()
  114.     {
  115.         return sprintf('%s\\%s', $this->guessRepositoryNamespace(), $this->guessRepositoryShortName());
  116.     }
  117.  
  118.     /**
  119.      *
  120.      * @return string
  121.      */
  122.     public function guessFormTypeNamespace()
  123.     {
  124.         return ($p = strrpos($ns = $this->getNamespace(), '\\')) === false ? $ns
  125.             : sprintf("%s\Form\Type", substr($ns, 0, $p));
  126.     }
  127.  
  128.     /**
  129.      *
  130.      * @return string
  131.      */
  132.     public function guessFormTypeShortName()
  133.     {
  134.         return sprintf(
  135.             '%sFormType',
  136.             ($pos = strpos($s = $this->getShortName(), 'Controller')) === false ? $s : substr($s, 0, $pos)
  137.         );
  138.     }
  139.  
  140.     /**
  141.      *
  142.      * @return string
  143.      */
  144.     public function guessFormTypeName()
  145.     {
  146.         return sprintf('%s\%s', $this->guessFormTypeNamespace(), $this->guessFormTypeShortName());
  147.     }
  148.  
  149. }
  150.  
  151. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement