Example #1
0
 /**
  * @param string|mixed|\ $dsn
  * @param array $options
  *
  * @return AdapterInterface
  *
  * @link http://docs.doctrine-project.org/en/2.0.x/reference/configuration.html
  * @link http://docs.doctrine-project.org/en/latest/reference/tools.html
  * @link https://github.com/itmcd/StormIgniter/blob/master/app/libraries/DoctrineDb.php
  * @link http://framework.zend.com/manual/2.2/en/user-guide/database-and-models.html
  * @link http://piotrdeszynski.com/zend-framework-2-and-doctrine-2-integration.html
  * @link http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/#toc-update-the-album-controller-to-use-doctrine-instead-of-zend_db
  * @link http://framework.zend.com/manual/2.2/en/user-guide/database-and-models.html
  */
 public function __construct($conn, $options = array())
 {
     $isDev = Environment::isDev();
     // @see Doctrine\ORM\EntityManager#create()
     $config = ArrayUtils::arrayMergeRecursiveRight($options, array('cache' => $isDev ? new Cache\ArrayCache() : new Cache\ApcCache(), 'queryCache' => $isDev ? new Cache\ArrayCache() : new Cache\ApcCache(), 'modelsProxiesPath' => getcwd() . '/cache/ModelProxies'));
     // create Configuration object
     if (is_array($options)) {
         $config = $this->renderConfig($options);
         if (!$config->getMetadataDriverImpl()) {
             throw ORMException::missingMappingDriverImpl();
         }
     }
     // test EventManager object
     $eventManager = empty($options['eventManager']) ? new EventManager() : $options['eventManager'];
     switch (true) {
         case $conn instanceof \PDO:
             $conn = array('driver' => 'pdo_' . $conn->getAttribute(\PDO::ATTR_DRIVER_NAME), 'pdo' => $conn);
             $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
             break;
         case is_string($conn):
             if (($pos = strpos($conn, ':')) !== false) {
                 $driver = substr($conn, 0, $pos);
                 //                    @TODO: must implement like this, but for now throws an error about not knowing the database
                 //                    if ($driver !== 'oci8') {
                 //                        $conn = array(
                 //                            'driver' => 'pdo_' . $driver,
                 //                            'pdo' => new \PDO($conn),
                 //                        );
                 //                    } else {
                 //                        $conn = $this->mapDsnToDoctrine($conn);
                 //                    }
                 //                    ^ by dragosc@itmcd.ro / temporary replacement untill we discover why \PDO doesn't work
                 $dsn = new Dsn($conn);
                 $conn = $this->mapDsnToDoctrine($dsn->parse());
                 if ($driver !== 'oci8') {
                     $conn['driver'] = 'pdo_' . $conn['driver'];
                 }
                 //                    ^ END
             }
             $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
             break;
         case is_array($conn):
             $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
             break;
         case $conn instanceof Connection:
             if ($eventManager !== null && $conn->getEventManager() !== null) {
                 throw ORMException::mismatchedEventManager();
             }
             break;
         default:
             throw new AthemException\InvalidArgument("Invalid argument: " . json_encode($conn));
     }
     // parent constructor
     // @see Doctrine\ORM\EntityManager#__construct()
     parent::__construct($conn, $config, $conn->getEventManager());
 }
Example #2
0
 /**
  * @param MinifyInterface $minifier
  */
 public function __construct(CompilerInterface $minifier, $options = array())
 {
     if ($minifier instanceof CachedCompiler) {
         throw new \Exception(sprintf('Recursion not allowed. Minifier must not be defived from %s', __CLASS__));
     }
     if (!isset($options['cache'])) {
         throw new Exception\InvalidResource('No cache options defined. This is a Cached Compiler class.');
     }
     $this->minifier = $minifier;
     $this->options = ArrayUtils::arrayMergeRecursiveRight($this->options, $options);
 }
Example #3
0
 public function db($path, $prefix = '')
 {
     $files = scandir($path);
     $classes = array();
     foreach ($files as $class) {
         if (strpos($class, '.php') !== false) {
             //                echo "\\NetisTest\\Model\\" . $prefix . str_replace('.php', '', $class) . "\n";
             $classes[] = self::$em->getClassMetadata("\\NetisTest\\Model\\" . $prefix . str_replace('.php', '', $class));
         }
         if (is_dir($path . '/' . $class) && $class[0] != '.' && $class != 'Setup') {
             $classes = ArrayUtils::arrayMergeRecursive($classes, $this->db($path . '/' . $class, $prefix . $class . '\\'));
         }
     }
     return $classes;
 }
Example #4
0
 /**
 *
 *<code>
    $pdf = new \Netis\Pdf\Html2Pdf\Wkhtmltopdf('<h3>This is a test</h3>');
 
    $pdf = new \Netis\Pdf\Html2Pdf\Wkhtmltopdf('http://goo.com/link/to/convert');
 
    $pdf = new \Netis\Pdf\Html2Pdf\Wkhtmltopdf(array(
        'http://goo.com/link/to/convert',
        'http://goo.com/link/to/convert2',
        '<h3>This is a test</h3>'
    ));
 
    $pdf = new \Netis\Pdf\Html2Pdf\Wkhtmltopdf('http://goo.com/link/to/convert', array(
        'size' => \Netis\Pdf\Html2Pdf\Wkhtmltopdf::PAGE_SIZE_A4,
        'margin'    => array(10, 10, 10, 10),
        'encoding'  => \Netis\Pdf\Html2Pdf\Wkhtmltopdf::PAGE_ENCODING_UTF8,
    ));
 * </code>
 *
 * @param array|string $html
 * @param array $page
 */
 public function __construct($html, $page = array())
 {
     if (!defined('NETIS_PDF_MEMORY_LIMIT')) {
         ini_set('memory_limit', '64M');
     } else {
         ini_set('memory_limit', NETIS_PDF_MEMORY_LIMIT);
     }
     $this->_page = ArrayUtils::arrayMergeRecursiveRight($this->_page, $page);
     call_user_func_array(array($this, 'setMargin'), $this->_page['margin']);
     if (is_array($html)) {
         foreach ($html as $page) {
             $this->_html[] = $this->_htmlProcess($page);
         }
     } else {
         $this->_html[] = $this->_htmlProcess($html);
     }
 }
Example #5
0
 /**
  * Obtain a minifier instance.
  *
  * @param array|Traversable $cfg
  * @return MinifyInterface
  */
 public static function factory($cfg = array())
 {
     $options = array('tool' => 'unify', 'options' => array());
     if ($cfg instanceof Traversable) {
         $cfg = ArrayUtils::iteratorToArray($cfg);
     }
     if (!is_array($cfg)) {
         throw new Exception\InvalidArgument('The factory needs an associative array or a Traversable object as an argument');
     }
     $cfg = Stdlib\ArrayUtils::arrayMergeRecursiveRight($options, $cfg);
     if (class_exists($cfg['tool'])) {
         $class = $cfg['tool'];
     } else {
         $class = '\\Athem\\Code';
         if (isset($cfg['code'])) {
             $class .= '\\' . ucfirst($cfg['code']) . '\\Compiler';
         }
         $class .= '\\' . ucfirst($cfg['tool']);
     }
     return isset($cfg['compiler']) ? new $class(self::factory($cfg['compiler']), $cfg['options']) : new $class($cfg['options']);
 }
Example #6
0
 /**
  * Constructor
  *
  * @param array $options
  */
 public function __construct($options = array())
 {
     $this->options = ArrayUtils::arrayMergeRecursiveRight($this->options, $options);
 }
Example #7
0
File: Dsn.php Project: athem/athem
 /**
  * DSN Parser
  *
  * @param string $dsn
  * @return array
  *
  * @TODO: Complete it!
  */
 public function parse($dsn = null)
 {
     if (null === $dsn) {
         $dsn = $this->dsn;
     }
     if (null === $dsn) {
         throw new \Athem\Exception\InvalidArgument('Invalid argument $dsn. Argument must be a string.');
     }
     $parsed = array('phptype' => false, 'dbsyntax' => false, 'username' => false, 'password' => false, 'protocol' => false, 'hostspec' => false, 'port' => false, 'socket' => false, 'database' => false);
     if (($pos = strpos($dsn, '://')) !== false) {
         $str = substr($dsn, 0, $pos);
         $dsn = substr($dsn, $pos + 3);
     } else {
         if (($pos = strpos($dsn, ':')) !== false) {
             $str = substr($dsn, 0, $pos);
             $dsn = substr($dsn, $pos + 1);
         } else {
             $str = $dsn;
             $dsn = null;
         }
     }
     // Get phptype and dbsyntax
     // $str => phptype(dbsyntax)
     if (preg_match('|^(.+?)\\((.*?)\\)$|', $str, $arr)) {
         $parsed['phptype'] = $arr[1];
         $parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2];
     } else {
         $parsed['phptype'] = $str;
         $parsed['dbsyntax'] = $str;
     }
     // still have data in dsn
     if (count($dsn)) {
         if (preg_match('|^(([a-zA-Z0-9_-]+)=([^;]+);?)+$|', $dsn, $match)) {
             $dsn = explode(';', $dsn);
             $parsed2 = array();
             if (!is_array($dsn) && is_string($dsn)) {
                 $dsn = array($dsn);
             }
             foreach ($dsn as $seg) {
                 $seg = explode('=', $seg);
                 $parsed2[$seg[0]] = $seg[1];
             }
             $mapper = '\\Athem\\Db\\Dsn\\Mapper\\' . ucfirst($parsed['phptype']);
             $mapper = new $mapper();
             $parsed = ArrayUtils::arrayMergeRecursiveRight($parsed, $mapper->map($parsed2));
             if ($parsed['protocol'] === false) {
                 $parsed['protocol'] = 'tcp';
             }
         } else {
             // Get (if found): username and password
             // $dsn => username:password@protocol+hostspec/database
             if (($at = strrpos($dsn, '@')) !== false) {
                 $str = substr($dsn, 0, $at);
                 $dsn = substr($dsn, $at + 1);
                 if (($pos = strpos($str, ':')) !== false) {
                     $parsed['username'] = rawurldecode(substr($str, 0, $pos));
                     $parsed['password'] = rawurldecode(substr($str, $pos + 1));
                 } else {
                     $parsed['username'] = rawurldecode($str);
                 }
             }
             // Find protocol and hostspec
             if (preg_match('|^([^(]+)\\((.*?)\\)/?(.*?)$|', $dsn, $match)) {
                 // $dsn => proto(proto_opts)/database
                 $proto = $match[1];
                 $proto_opts = $match[2] ? $match[2] : false;
                 $dsn = $match[3];
             } else {
                 // $dsn => protocol+hostspec:port/database (old format)
                 if (strpos($dsn, '+') !== false) {
                     list($proto, $dsn) = explode('+', $dsn, 2);
                 }
                 if (strpos($dsn, '/') !== false) {
                     list($proto_opts, $dsn) = explode('/', $dsn, 2);
                 } else {
                     $proto_opts = $dsn;
                     $dsn = null;
                 }
             }
             // process the different protocol options
             $parsed['protocol'] = !empty($proto) ? $proto : 'tcp';
             $proto_opts = rawurldecode($proto_opts);
             if ($parsed['protocol'] == 'tcp' || $parsed['protocol'] == 'udp') {
                 if (strpos($proto_opts, ':') !== false) {
                     list($parsed['hostspec'], $parsed['port']) = explode(':', $proto_opts);
                 } else {
                     $parsed['hostspec'] = $proto_opts;
                 }
             } elseif ($parsed['protocol'] == 'unix') {
                 $parsed['socket'] = $proto_opts;
             }
             // Get dabase if any
             // $dsn => database
             if ($dsn) {
                 if (($pos = strpos($dsn, '?')) === false) {
                     // /database
                     $parsed['database'] = rawurldecode($dsn);
                 } else {
                     // /database?param1=value1&param2=value2
                     $parsed['database'] = rawurldecode(substr($dsn, 0, $pos));
                     $dsn = substr($dsn, $pos + 1);
                     if (strpos($dsn, '&') !== false) {
                         $opts = explode('&', $dsn);
                     } else {
                         // database?param1=value1
                         $opts = array($dsn);
                     }
                     foreach ($opts as $opt) {
                         list($key, $value) = explode('=', $opt);
                         if (!isset($parsed[$key])) {
                             // don't allow params overwrite
                             $parsed[$key] = rawurldecode($value);
                         }
                     }
                 }
             }
         }
     }
     return array_filter($parsed, function ($v) {
         return $v !== false;
     });
 }
Example #8
0
 public function testToObject()
 {
     $obj = array('test1' => 1, 'test2' => 2);
     $this->assertEquals(ArrayUtils::toObject($obj), (object) $obj);
 }
Example #9
0
 /**
  *
  */
 protected function preToString()
 {
     $logger = $this->getServiceLocator()->getServiceLocator()->get('Athcore\\Log\\Logger');
     try {
         // obtain Zend\Storage instance
         $cache = $this->getServiceLocator()->getServiceLocator()->get('Athcore\\Cache\\Cache');
         // generate key based on url
         $caheKey = $this->head . '-' . md5(preg_replace('/[^\\d\\w]/', '-', $this->view->url())) . '-js';
         // load the cache if possible, or generate it
         $itemSet = $cache->getItem($caheKey, $success);
         // current resource
         $self = $this;
         // new container
         $container = Placeholder\Container::factoryCloneFromContainer($this->getContainer());
         $container->setCacheKey($caheKey);
         if (!$success) {
             // minify code
             if ($this->config->canMinify()) {
                 $container->organizeForMinify(function ($item) use($self) {
                     return $self->isValid($item);
                 }, function ($o1, $o2) use($self) {
                     return $self->compare($o1, $o2);
                 }, Placeholder\Container::CODE_JS);
             }
             $itemSet = array_filter($container->getArrayCopy(), function ($item) use($self) {
                 return $self->isValid($item);
             });
             $cache->setItem($caheKey, $itemSet);
         }
         // get container
         $container->exchangeArray($itemSet);
         // set the new item urls
         if ($this->config->canMinify()) {
             $container->addaptUrlsForMinify($this->view, $this->config, function ($item) use($self) {
                 return $self->isValid($item);
             }, Placeholder\Container::CODE_JS);
         }
         // add versioning
         if ($this->config->hasVersion()) {
             $container->attachVersionToUrl($this->config->getVersion(), Placeholder\Container::CODE_JS);
         }
         // update container
         $this->setContainer($container);
     } catch (\Exception $e) {
         if (Environment::isDev()) {
             $container = $this->getContainer();
             $item = ArrayUtils::arrayToObject(array('type' => 'text/javascript', 'source' => sprintf('throw new Error(\'(type %s) :: %s. See logs for more details.\')', get_class($e), str_replace('\'', '\\\'', $e->getMessage()))));
             $container[] = $item;
             $this->setContainer($container);
         }
         $logger->exception($e);
     }
 }
Example #10
0
 /**
  * Detect default template path
  *
  * @param array $paths
  * @return string
  */
 public function getTemplatePaths($paths = array())
 {
     return ArrayUtils::arrayMergeRecursiveRight(array($this->getViewPath()), $paths);
 }