Commits

Anonymous committed f11f516

init

Comments (0)

Files changed (11)

doctrine_lucene/library/DoctrineX/Template/Listener/Lucene.php

+<?php
+class DoctrineX_Template_Listener_Lucene extends Doctrine_Record_Listener
+{
+    public function __construct(array $options)
+    {
+        $this->_options = $options;
+    }
+
+    /**
+     *
+     * @param Doctrine_Event $event
+     */
+    public function postInsert(Doctrine_Event $event)
+    {
+        $invoker = $event->getInvoker();
+        $index = $invoker->getLuceneIndex();
+
+        if (method_exists($invoker, 'preLucene')) {
+            $invoker->preLucene();
+        }
+
+        $doc = $invoker->createLuceneDocument();
+        $index->addDocument($doc);
+        $index->commit();
+
+        if (method_exists($invoker, 'postLucene')) {
+            $invoker->postLucene();
+        }
+    }
+
+    public function postUpdate(Doctrine_Event $event)
+    {
+        $invoker = $event->getInvoker();
+        $index = $invoker->getLuceneIndex();
+
+        $hits = $invoker->findLuceneDocument();
+        foreach ($hits as $hit) {
+            $index->delete($hit->id);
+        }
+
+        $doc = $invoker->createLuceneDocument();
+        $index->addDocument($doc);
+        $index->commit();
+    }
+
+    public function postDelete(Doctrine_Event $event)
+    {
+        $invoker = $event->getInvoker();
+        $index = $invoker->getLuceneIndex();
+
+        $hits = $invoker->findLuceneDocument();
+        foreach ($hits as $hit) {
+            $index->delete($hit->id);
+        }
+        $index->commit();
+    }
+}

doctrine_lucene/library/DoctrineX/Template/Lucene.php

+<?php
+class DoctrineX_Template_Lucene extends Doctrine_Template
+{
+    /**
+     *
+     * @var Zend_Search_Lucene_Interface
+     */
+    protected $index = null;
+
+    /**
+     *
+     * @var array
+     */
+    protected $identifierFields = array();
+
+    /**
+     *
+     * @var array
+     */
+    protected $userFields = array();
+
+    /**
+     *
+     * @var array
+     */
+    protected $fields = array();
+
+    /**
+     *
+     * @var string
+     */
+    protected $encoding = '';
+
+    public function __construct(array $options = array())
+    {
+        if (!isset($options['indexesPath']) && !isset($options['indexFile'])) {
+            throw new Doctrine_Record_Exception('Option "indexesPath" or "indexFile" is required');
+        }
+
+        if (!isset($options['fields'])) {
+            throw new Doctrine_Record_Exception('Option "fields" is required');
+        }
+
+        if (isset($options['encoding'])) {
+            $this->encoding = $options['encoding'];
+            Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding($this->encoding);
+        }
+
+        if (isset($options['analyzer'])) {
+            Zend_Search_Lucene_Analysis_Analyzer::setDefault(
+                new $options['analyzer']()
+            );
+        }
+
+        if (isset($options['filters'])) {
+            $filters = $options['filters'];
+        } else {
+            $filters = array();
+        }
+        if (isset($options['stopWords'])) {
+            $filters[] = new Zend_Search_Lucene_Analysis_TokenFilter_StopWords(
+                    $options['stopWords']);
+        }
+        if (isset($options['stopWordsFile'])) {
+            $stopWordsFilter = new Zend_Search_Lucene_Analysis_TokenFilter_StopWords();
+            $stopWordsFilter->loadFromFile($options['stopWordsFile']);
+            $filters[] = $stopWordsFilter;
+        }
+        if (!empty($filters)) {
+            $analyzer = Zend_Search_Lucene_Analysis_Analyzer::getDefault();
+            foreach ($filters as $filter) {
+                $analyzer->addFilter($filter);
+            }
+        }
+
+        parent::__construct($options);
+    }
+
+    public function setTableDefinition()
+    {
+        $this->addListener(new DoctrineX_Template_Listener_Lucene($this->_options));
+    }
+
+    /**
+     *
+     * @return string
+     */
+    public function getLuceneIndexFile()
+    {
+        if (isset($this->_options['indexFile'])) {
+            return $this->_options['indexFile'];
+        } else {
+            return $this->_options['indexesPath'] . '/' . get_class($this->_invoker);
+        }
+    }
+
+    /**
+     *
+     * @return Zend_Search_Lucene_Interface
+     */
+    public function getLuceneIndex()
+    {
+        if (null === $this->index) {
+            if (file_exists($index = $this->getLuceneIndexFile())) {
+                $this->index = Zend_Search_Lucene::open($index);
+            } else {
+                $this->index = Zend_Search_Lucene::create($index);
+            }
+        }
+        return $this->index;
+    }
+
+    public function findLuceneDocument()
+    {
+        $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
+        foreach ($this->getLuceneIdentifierFields() as $field) {
+            $name = $field['name'];
+            $query->addTerm(new Zend_Search_Lucene_Index_Term($this->_invoker->$name,
+                    $name), true);
+        }
+        return $this->getLuceneIndex()->find($query);
+    }
+
+    public function createLuceneDocument()
+    {
+        $doc = new Zend_Search_Lucene_Document();
+        foreach ($this->getLuceneFields() as $field) {
+            $type = $field['type'];
+            $name = $field['name'];
+
+            $docField = Zend_Search_Lucene_Field::$type($name,
+                    $this->_invoker->$name, $this->encoding);
+            if (isset($field['boost'])) {
+                $docField->boost = $field['boost'];
+            }
+            $doc->addField($docField);
+        }
+        return $doc;
+    }
+
+    /**
+     *
+     * @return array
+     */
+    public function getLuceneIdentifierFields()
+    {
+        if (array() === $this->identifierFields) {
+            $identifier = $this->_invoker->getTable()->getIdentifier();
+            foreach ((array) $identifier as $id) {
+                $this->identifierFields[] = array(
+                    'name' => $id,
+                    'type' => 'Keyword',
+                );
+            }
+        }
+        return $this->identifierFields;
+    }
+
+    public function getLuceneUserFields()
+    {
+        if (array() === $this->userFields) {
+            $this->userFields = $this->_options['fields'];
+        }
+        return $this->userFields;
+    }
+
+    /**
+     *
+     * @return array
+     */
+    public function getLuceneFields()
+    {
+        if (array() === $this->fields) {
+            $this->fields = array_merge($this->getLuceneIdentifierFields(),
+                    $this->getLuceneUserFields());
+        }
+        return $this->fields;
+    }
+}

zf_bundles/library/ZendY/View/Helper/BundleAbstract.php

+<?php
+abstract class ZendY_View_Helper_BundleAbstract
+{
+    /**
+     * View object
+     *
+     * @var Zend_View_Interface
+     */
+    public $view = null;
+
+    protected $isEnabled = true;
+    protected $items = array();
+
+    protected $baseUrl = '';
+    protected $documentRoot = '';
+    protected $cacheDir = '';
+    protected $urlPrefix = '';
+
+    protected $bundleId = '';
+    protected $bundleExt = '';
+
+    public function enable()
+    {
+        $this->isEnabled = true;
+        return $this;
+    }
+
+    public function disable()
+    {
+        $this->isEnabled = false;
+        return $this;
+    }
+
+    public function getItems()
+    {
+        return $this->items;
+    }
+
+    public function setDocumentRoot($value)
+    {
+        $this->documentRoot = $value;
+        return $this;
+    }
+
+    public function getDocumentRoot()
+    {
+        return $this->documentRoot;
+    }
+
+    public function setCacheDir($value)
+    {
+        $this->cacheDir = $value;
+        if ('' === $this->urlPrefix) {
+            $this->urlPrefix = $this->cacheDir;
+        }
+        return $this;
+    }
+
+    public function getCacheDir()
+    {
+        return $this->cacheDir;
+    }
+
+    public function setUrlPrefix($value)
+    {
+        $this->urlPrefix = $value;
+        return $this;
+    }
+
+    public function getUrlPrefix()
+    {
+        return $this->urlPrefix;
+    }
+
+    public function setBundleExt($value)
+    {
+        $this->bundleExt = $value;
+        return $this;
+    }
+
+    public function getBundleExt()
+    {
+        return $this->bundleExt;
+    }
+
+    public function __construct()
+    {
+        $this->documentRoot = $_SERVER['DOCUMENT_ROOT'];
+    }
+
+    abstract public function toString();
+
+    protected function _addFile($file)
+    {
+        if (!file_exists($file)) {
+            return;
+        }
+
+        $this->items[] = array(
+            'type' => 'file',
+            'src' => $this->_cutBaseUrl($file),
+        );
+        $this->bundleId = '';
+    }
+
+    protected function _addSource($source)
+    {
+        $this->items[] = array(
+            'type' => 'inline',
+            'src' => $source,
+        );
+        $this->bundleId = '';
+    }
+
+    protected function _cutBaseUrl($url)
+    {
+        $baseUrl = $this->view->baseUrl();
+        if ($baseUrl && strpos($url, $baseUrl) === 0) {
+            $url = substr($url, strlen($baseUrl));
+        }
+        return $url;
+    }
+
+    protected function _getBundleData()
+    {
+        $data = '';
+        foreach ($this->items as $item) {
+            if ($item['type'] == 'file') {
+                $data .= file_get_contents($this->documentRoot . '/'
+                        . $item['src']) . PHP_EOL;
+            } else {
+                $data .= $item['src'] . PHP_EOL;
+            }
+        }
+        return $data;
+    }
+
+    protected function _createBundle($bundleFile)
+    {
+        $data = $this->_getBundleData();
+        file_put_contents($bundleFile, $data);
+    }
+
+    protected function _getBundleId()
+    {
+        if ('' === $this->bundleId) {
+            $id = '';
+            foreach ($this->items as $item) {
+                $id .= $item['src'];
+            }
+            $this->bundleId = md5($id);
+        }
+        return $this->bundleId;
+    }
+
+    protected function _getBundlePath()
+    {
+        $id = $this->_getBundleId();
+        return "{$this->cacheDir}/{$id}{$this->bundleExt}";
+    }
+
+    protected function _getBundleUrl()
+    {
+        $id = $this->_getBundleId();
+        return "{$this->urlPrefix}/{$id}{$this->bundleExt}";
+    }
+
+    public function __toString()
+    {
+        return $this->toString();
+    }
+
+    /**
+     * Set the View object
+     *
+     * @param  Zend_View_Interface $view
+     * @return Zend_View_Helper_Abstract
+     */
+    public function setView(Zend_View_Interface $view)
+    {
+        $this->view = $view;
+        return $this;
+    }
+}

zf_bundles/library/ZendY/View/Helper/BundleLink.php

+<?php
+class ZendY_View_Helper_BundleLink extends ZendY_View_Helper_BundleAbstract
+{
+    public function __construct()
+    {
+        parent::__construct();
+        $this->setBundleExt('.css');
+    }
+
+    public function bundleLink()
+    {
+        if (!$this->isEnabled) {
+            return $this;
+        }
+
+        $args = func_get_args();
+        foreach ($args as $arg) {
+            $this->add($arg);
+        }
+        return $this;
+    }
+
+    public function add($link)
+    {
+        if ($link instanceof Zend_View_Helper_Placeholder_Container_Standalone) {
+            $keys = array();
+            foreach ($link as $key => $item) {
+                if (!$this->_isValidItem($item)) {
+                    continue;
+                }
+
+                if (isset($item->href)) {
+                    $this->_addFile($item->href);
+                    $keys[] = $key;
+                } else if (!empty($item->source)) {
+                    $this->_addSource($item->source);
+                    $keys[] = $key;
+                } else {
+                    throw new UnexpectedValueException('Item has no src attribute nor captured source');
+                }
+            }
+            foreach ($keys as $key) {
+                unset($link[$key]);
+            }
+        } else if ($link instanceof ZendX_JQuery_View_Helper_JQuery_Container) {
+            $renderMode = $link->getRenderMode();
+            if (($link->getRenderMode() & ZendX_JQuery::RENDER_STYLESHEETS) == 0) {
+                continue;
+            }
+
+            foreach ($link->getStylesheets() as $file) {
+                $this->_addFile($file);
+            }
+
+            $link->setRenderMode($renderMode ^ ZendX_JQuery::RENDER_STYLESHEETS);
+        } else {
+            throw new InvalidArgumentException('Unsupported argument type "' . gettype($link) . '"');
+        }
+    }
+
+    public function toString()
+    {
+        if (count($this->items) === 0) {
+            return '';
+        }
+
+        $path = $this->_getBundlePath();
+        $url = $this->_getBundleUrl();
+
+        if (!file_exists($path)) {
+            $this->_createBundle($path);
+        }
+
+        $return = '<link href="' . $url
+                . '" media="screen" rel="stylesheet" type="text/css" />' . PHP_EOL;
+        return $return;
+    }
+
+    protected function _isValidItem($item)
+    {
+        return $item->rel == 'stylesheet' && $item->type == 'text/css';
+    }
+}

zf_bundles/library/ZendY/View/Helper/BundleScript.php

+<?php
+class ZendY_View_Helper_BundleScript extends ZendY_View_Helper_BundleAbstract
+{
+    public function __construct()
+    {
+        parent::__construct();
+        $this->setBundleExt('.js');
+    }
+
+    public function bundleScript()
+    {
+        if (!$this->isEnabled) {
+            return $this;
+        }
+
+        $args = func_get_args();
+        foreach ($args as $arg) {
+            $this->add($arg);
+        }
+        return $this;
+    }
+
+    public function add($script)
+    {
+        if ($script instanceof Zend_View_Helper_Placeholder_Container_Standalone) {
+            $keys = array();
+
+            foreach ($script as $key => $item) {
+                if (isset($item->attributes['src'])) {
+                    $this->_addFile($item->attributes['src']);
+                    $keys[] = $key;
+                } else if (!empty($item->source)) {
+                    $this->_addSource($item->source);
+                    $keys[] = $key;
+                } else {
+                    throw new UnexpectedValueException('Item has no src attribute nor captured source');
+                }
+            }
+
+            foreach ($keys as $key) {
+                unset($script[$key]);
+            }
+        } else if ($script instanceof ZendX_JQuery_View_Helper_JQuery_Container) {
+            $oldMask = $script->getRenderMode();
+
+            $script->setRenderMode($oldMask & (ZendX_JQuery::RENDER_LIBRARY
+                    | ZendX_JQuery::RENDER_SOURCES));
+            $string = $script->__toString();
+
+            $found = preg_match_all(
+                '#<script type="text/javascript" src="(.+?)"></script>#i',
+                $string,
+                $matches
+            );
+            if ($found) {
+                foreach ($matches[1] as $file) {
+                    $this->_addFile($file);
+                }
+            }
+
+            $found = preg_match_all(
+                '#<script type="text/javascript">(.+?)</script>#si',
+                $string,
+                $matches
+            );
+            if ($found) {
+                foreach ($matches[1] as $source) {
+                    $this->_addSource($source);
+                }
+            }
+
+            $script->setRenderMode($oldMask & (ZendX_JQuery::RENDER_JQUERY_ON_LOAD
+                    | ZendX_JQuery::RENDER_JAVASCRIPT));
+            $string = $script->__toString();
+
+            $oDelimiters = $this->pregQuoteArray(array('//<![CDATA[', '//<!--'), '#');
+            $cDelimiters = $this->pregQuoteArray(array('//]]>', '//-->'), '#');
+            $found = preg_match(
+                '#(' . implode('|', $oDelimiters) . ')(.+?)(' . implode('|', $cDelimiters) . ')#si',
+                $string,
+                $matches
+            );
+            if ($found) {
+                $this->_addSource($matches[2]);
+            }
+
+            $script->setRenderMode($oldMask ^ (ZendX_JQuery::RENDER_LIBRARY
+                    | ZendX_JQuery::RENDER_SOURCES
+                    | ZendX_JQuery::RENDER_JQUERY_ON_LOAD
+                    | ZendX_JQuery::RENDER_JAVASCRIPT));
+        } else {
+            throw new InvalidArgumentException('Unsupported argument type "' . gettype($script) . '"');
+        }
+    }
+
+    public function toString()
+    {
+        if (count($this->items) === 0) {
+            return '';
+        }
+
+        $path = $this->_getBundlePath();
+        $url = $this->_getBundleUrl();
+
+        if (!file_exists($path)) {
+            $this->_createBundle($path);
+        }
+
+        $return = '<script type="text/javascript" src="' . $url . '"></script>'
+                . PHP_EOL;
+        return $return;
+    }
+
+    protected function pregQuoteArray($array, $delimiter)
+    {
+        foreach ($array as &$s) {
+            $s = preg_quote($s, $delimiter);
+        }
+        return $array;
+    }
+}

zf_highlight/library/Text/View/Helper/Highlight.php

+<?php
+class Text_View_Helper_Highlight extends Zend_View_Helper_Abstract
+{
+    protected $basePath = '';
+    protected $scriptPath = '';
+    protected $stylePath = '';
+    protected $tabReplace = '';
+    protected $languages = array();
+
+    public function setBasePath($path)
+    {
+        $this->basePath = $path;
+        return $this;
+    }
+
+    public function getBasePath()
+    {
+        return $this->basePath;
+    }
+
+    public function setScriptPath($path)
+    {
+        $this->scriptPath = $this->getBasePath() . $path;
+        return $this;
+    }
+
+    public function getScriptPath()
+    {
+        return $this->scriptPath;
+    }
+
+    public function setStylePath($path)
+    {
+        $this->stylePath = $this->getBasePath() . $path;
+        return $this;
+    }
+
+    public function getStylePath()
+    {
+        return $this->stylePath;
+    }
+
+    public function setTabReplace($tabReplace)
+    {
+        $this->tabReplace = $tabReplace;
+        return $this;
+    }
+
+    public function getTabReplace()
+    {
+        return $this->tabReplace;
+    }
+
+    public function setLanguages(array $languages)
+    {
+        $this->languages = $languages;
+        return $this;
+    }
+
+    public function getLanguages()
+    {
+        return $this->languages;
+    }
+
+    public function setOptions(array $options)
+    {
+        foreach ($options as $key => $value) {
+            $methodName = 'set' . $key;
+            if (method_exists($this, $methodName)) {
+                $this->$methodName($value);
+            } else {
+                throw new Zend_View_Exception('Unknown option "' . $key . '"');
+            }
+        }
+        return $this;
+    }
+
+    public function highlight(array $options = array())
+    {
+        $this->setOptions($options);
+        return $this;
+    }
+
+    protected function _getScript()
+    {
+        $script = '';
+
+        $tabReplace = $this->getTabReplace();
+        if ('' !== $tabReplace) {
+            $tabReplace = str_replace('"', '\"', $tabReplace);
+            $script .= 'hljs.tabReplace = "' . $tabReplace . '";' . PHP_EOL;
+        }
+
+        $languages = $this->getLanguages();
+        if (array() !== $languages) {
+            $languages = '"' . implode('", "', $languages) . '"';
+        } else {
+            $languages = '';
+        }
+        $script .= 'hljs.initHighlightingOnLoad(' . $languages . ');' . PHP_EOL;
+
+        return $script;
+    }
+
+    public function inject()
+    {
+        $localPath = $this->getScriptPath();
+        if (!empty($localPath)) {
+            $this->view->headScript()
+                    ->appendFile($localPath)
+                    ->appendScript($this->_getScript());
+        } else {
+            throw new Zend_View_Exception('Option "scriptPath" must be set');
+        }
+
+        $stylePath = $this->getStylePath();
+        if (!empty($stylePath)) {
+            $this->view->headLink()
+                    ->appendStylesheet($stylePath);
+        }
+    }
+
+    public function __toString()
+    {
+        $this->inject();
+        return '';
+    }
+}

zf_multilingual/library/ZendY/Controller/Router/Route/Multilingual.php

+<?php
+class ZendY_Controller_Router_Route_Multilingual extends
+        Zend_Controller_Router_Route
+{
+    protected static $defaultLang = '';
+    protected static $currentLang = '';
+    protected static $languagePrefixes = array();
+
+    public static function init($langs, $defaultLang = '', $currentLang = '')
+    {
+        self::setLanguagePrefixes($langs);
+        if ($defaultLang) { self::setDefaultLang($defaultLang); }
+        if ($currentLang) { self::setCurrentLang($currentLang); }
+    }
+
+    public static function setDefaultLang($lang)
+    {
+        self::$defaultLang = $lang;
+    }
+
+    public static function setCurrentLang($lang)
+    {
+        self::$currentLang = $lang;
+    }
+
+    public static function setLanguagePrefixes(array $prefixes)
+    {
+        self::$languagePrefixes = $prefixes;
+    }
+
+    public function __construct($route, $defaults = array(), $reqs = array(),
+            Zend_Translate $translator = null, $locale = null)
+    {
+        if (!isset($defaults['lang']) && self::$defaultLang) {
+            $defaults['lang'] = self::$defaultLang;
+        }
+        parent::__construct($route, $defaults, $reqs, $translator, $locale);
+    }
+
+    public static function getInstance(Zend_Config $config)
+    {
+        $reqs = ($config->reqs instanceof Zend_Config)
+                ? $config->reqs->toArray() : array();
+        $defs = ($config->defaults instanceof Zend_Config)
+                ? $config->defaults->toArray() : array();
+        return new self($config->route, $defs, $reqs);
+    }
+
+    public function match($path)
+    {
+        $path = trim($path, $this->_urlDelimiter);
+        $pathParts = explode($this->_urlDelimiter, $path, 2);
+
+        if (in_array($pathParts[0], self::$languagePrefixes)) {
+            $path = (sizeof($pathParts) > 1) ? $pathParts[1] : '';
+            $currentLanguage = $pathParts[0];
+        } else {
+            $currentLanguage = $this->_defaults['lang'];
+        }
+
+        $params = parent::match($path);
+        if ($params) {
+            $params = array_merge($params, array('lang' => $currentLanguage));
+        }
+
+        return $params;
+    }
+
+
+    public function assemble($data = array(), $reset = false, $encode = false)
+    {
+        if (!isset($data['lang']) && self::$currentLang) {
+            $data['lang'] = self::$currentLang;
+        }
+
+        if (isset($data['lang'])) {
+            $lang = $data['lang'];
+            unset($data['lang']);
+
+            $assemble = parent::assemble($data, $reset, $encode);
+            if (in_array($lang, self::$languagePrefixes)) {
+                if ($lang != $this->_defaults['lang']) {
+                    $assemble = implode(
+                        $this->_urlDelimiter,
+                        array($lang, $assemble)
+                    );
+                }
+            }
+            return $assemble;
+        } else {
+            return parent::assemble($data, $reset);
+        }
+    }
+}

zf_translate/library/ZendY/Translate.php

+<?php
+class ZendY_Translate extends Zend_Translate
+{
+    protected $default = array();
+
+    public function setDefault(array $value)
+    {
+        $this->default = $value;
+    }
+
+    public function getDefault()
+    {
+        return $this->default;
+    }
+
+    public function getAdapter()
+    {
+        return $this;
+    }
+
+    public function translate($messageId, $locale = null)
+    {
+        $adapter = parent::getAdapter();
+
+        $locales = array_merge(array($locale), $this->default);
+        foreach ($locales as $currentLocale) {
+            if ($adapter->isTranslated($messageId, false, $currentLocale)) {
+                return $adapter->translate($messageId, $currentLocale);
+            }
+        }
+
+        return $adapter->translate($messageId, $locale);
+    }
+}

zf_translate/tests/ZendY/TranslateTestCase.php

+<?php
+class ZendY_TranslateTestCase extends PHPUnit_Framework_TestCase
+{
+    public function testTranslate()
+    {
+        $translator = new ZendY_Translate(
+            'array',
+            dirname(__FILE__) . '/i18n',
+            'ru' // default locale
+        );
+        // if we are unable to find translation for language "ru",
+        // use translation for language "en"
+        $translator->setDefault(array('en'));
+
+        // translation for language "ru"
+        $this->assertEquals('ru', $translator->translate('lang'));
+
+        // translation for language "en"
+        $this->assertEquals('laughing out loud', $translator->translate('lol'));
+    }
+}

zf_translate/tests/ZendY/i18n/en.php

+<?php
+return array(
+    'lang' => 'en',
+    'lol' => 'laughing out loud',
+);

zf_translate/tests/ZendY/i18n/ru.php

+<?php
+return array(
+    'lang' => 'ru',
+);