Exemplo n.º 1
0
 public function testexpand()
 {
     $paths = FileUtils::expand_path('/path/{to,to2,to3,foo,bar}/end');
     is(count($paths), 5);
     $paths = FileUtils::expand_path('/path/foo/end');
     is(count($paths), 1);
 }
Exemplo n.º 2
0
 public function run()
 {
     $kernel = kernel();
     $frameworkId = $kernel::FRAMEWORK_ID;
     $appId = $kernel->config->framework->ApplicationID;
     /* merge/update framework locale into app locale dir */
     $finder = Finder::create()->files()->name('*.po')->in(PH_ROOT . DIRECTORY_SEPARATOR . 'locale');
     $itr = $finder->getIterator();
     foreach ($itr as $item) {
         # echo $item->getPathname(). "\n";
         $sourceDir = dirname($item->getPathname());
         $sourceRelPath = FileUtils::remove_base($item->getPathname(), PH_ROOT);
         $sourceRelDir = dirname($sourceRelPath);
         $targetDir = PH_APP_ROOT . DIRECTORY_SEPARATOR . $sourceRelDir;
         FileUtils::mkpath($targetDir);
         $sourcePo = $sourceDir . DIRECTORY_SEPARATOR . $frameworkId . '.po';
         $targetPo = $targetDir . DIRECTORY_SEPARATOR . $appId . '.po';
         # var_dump( $sourcePo , $targetPo );
         if (file_exists($targetPo)) {
             $this->log("Msgcat " . basename($sourcePo) . ' => ' . basename($targetPo));
             $merged = '';
             $h = popen("msgcat {$sourcePo} {$targetPo}", 'r');
             while (!feof($h)) {
                 // send the current file part to the browser
                 $merged .= fread($h, 1024);
             }
             pclose($h);
             $this->log("Writing back to ");
             file_put_contents($targetPo, $merged);
         } else {
             $this->log("Copying files..");
             copy($sourcePo, $targetPo);
         }
     }
 }
Exemplo n.º 3
0
    public function execute()
    {
        $kernel = kernel();
        $dirs = array();
        $dirs[] = FileUtils::path_join(PH_APP_ROOT, 'cache', 'view');
        $dirs[] = FileUtils::path_join(PH_APP_ROOT, 'cache', 'config');
        $dirs[] = 'locale';
        $dirs[] = 'applications';
        $dirs[] = 'bin';
        $dirs[] = 'bundles';
        $dirs[] = 'config';
        $dirs[] = 'webroot';
        /* for hard links */
        $dirs[] = 'webroot' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . 'images';
        $dirs[] = 'webroot' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . 'css';
        $dirs[] = 'webroot' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . 'js';
        $dirs[] = 'webroot' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . 'upload';
        FileUtils::mkpath($dirs, true);
        // TODO: create .htaccess file
        $this->logger->info("Changing permissions...");
        $chmods = array();
        $chmods[] = array("og+rw", "cache");
        $chmods[] = array("og+rw", $kernel->webroot . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . 'upload');
        foreach ($chmods as $mod) {
            $this->logger->info("{$mod[0]} {$mod[1]}", 1);
            system("chmod -R {$mod[0]} {$mod[1]}");
        }
        $this->logger->info("Linking bin/phifty");
        if (!file_exists('bin/phifty')) {
            symlink('../phifty/bin/phifty', 'bin/phifty');
        }
        # init config
        $this->logger->info("Copying config files...");
        copy_if_not_exists(FileUtils::path_join(PH_ROOT, 'config', 'framework.app.yml'), FileUtils::path_join(PH_APP_ROOT, 'config', 'framework.yml'));
        // copy_if_not_exists(FileUtils::path_join(PH_ROOT,'config','application.dev.yml'), FileUtils::path_join(PH_APP_ROOT,'config','application.yml') );
        copy_if_not_exists(FileUtils::path_join(PH_ROOT, 'config', 'database.app.yml'), FileUtils::path_join(PH_APP_ROOT, 'config', 'database.yml'));
        copy_if_not_exists(FileUtils::path_join(PH_ROOT, 'webroot', 'index.php'), FileUtils::path_join(PH_APP_ROOT, 'webroot', 'index.php'));
        copy_if_not_exists(FileUtils::path_join(PH_ROOT, 'webroot', '.htaccess'), FileUtils::path_join(PH_APP_ROOT, 'webroot', '.htaccess'));
        $this->logger->info('Application is initialized, please edit your config files and run:');
        echo <<<DOC

    \$ bin/phifty build-conf
    \$ bin/phifty asset

    \$ lazy build-conf config/database.yml

DOC;
    }
Exemplo n.º 4
0
 public function execute()
 {
     $kernel = kernel();
     $dirs = array();
     $dirs[] = FileUtils::path_join(PH_APP_ROOT, 'cache', 'view');
     $dirs[] = FileUtils::path_join(PH_APP_ROOT, 'cache', 'config');
     $dirs[] = 'locale';
     $dirs[] = 'app';
     $dirs[] = 'bin';
     $dirs[] = 'bundles';
     $dirs[] = 'config';
     $dirs[] = 'webroot';
     /* for hard links */
     $dirs[] = 'webroot' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . 'images';
     $dirs[] = 'webroot' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . 'css';
     $dirs[] = 'webroot' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . 'js';
     $dirs[] = 'webroot' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . 'upload';
     $dirs[] = 'webroot' . DIRECTORY_SEPARATOR . 'upload';
     FileUtils::mkpath($dirs, true);
     // TODO: create .htaccess file
     $this->logger->info("Changing permissions...");
     $chmods = [];
     $chmods[] = ["0777", "cache"];
     $chmods[] = ["0777", "cache/view"];
     $chmods[] = ["0777", "cache/config"];
     $chmods[] = ["0777", $kernel->webroot . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . 'upload'];
     $chmods[] = ["0777", $kernel->webroot . DIRECTORY_SEPARATOR . 'upload'];
     foreach ($chmods as $mod) {
         $this->logger->info("Changing mode to {$mod[0]} on {$mod[1]}", 1);
         if ($this->options->system) {
             system("chmod -R {$mod[0]} {$mod[1]}");
         } else {
             chmod($mod[1], octdec($mod[0]));
         }
     }
     $this->logger->info("Creating link of bin/phifty");
     if (!file_exists('bin/phifty')) {
         symlink('../vendor/bin/phifty', 'bin/phifty');
     }
 }
Exemplo n.º 5
0
 public function execute()
 {
     $kernel = kernel();
     $localeDir = $kernel->config->get('framework', 'Services.LocaleService.Directory') ?: 'locale';
     $frameworkLocaleDir = PH_ROOT . DIRECTORY_SEPARATOR . $localeDir;
     if ($langsConfig = $kernel->config->get('framework', 'Services.LocaleService.Langs')) {
         $langs = $langsConfig->config;
     } else {
         $this->logger->warn("Services.LocaleService.Langs is required.");
         $this->logger->warn("Using default lang 'en' for locale");
         $langs = array('en');
     }
     $cwd = getcwd();
     $appPoFiles = array();
     $frameworkId = Kernel::FRAMEWORK_ID;
     $appId = $kernel->config->framework->ApplicationID;
     $frameworkPoFilename = $frameworkId . '.po';
     $appPoFilename = $appId . '.po';
     // prepare po files from framework po source files,
     // if we don't have one for the specific language.
     foreach ($langs as $langId) {
         $poDir = $localeDir . DIRECTORY_SEPARATOR . $langId . DIRECTORY_SEPARATOR . 'LC_MESSAGES';
         $sourcePoPath = $frameworkLocaleDir . DIRECTORY_SEPARATOR . $langId . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $frameworkId . '.po';
         $targetPoPath = $localeDir . DIRECTORY_SEPARATOR . $langId . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $appId . '.po';
         if (!file_exists($poDir)) {
             mkdir($poDir, 0755, true);
         }
         if ($this->options && $this->options->force || file_exists($sourcePoPath) && !file_exists($targetPoPath)) {
             $this->logger->info("Creating {$targetPoPath}");
             if ($sourcePoPath != $targetPoPath) {
                 copy($sourcePoPath, $targetPoPath);
             }
         }
     }
     $engine = new Phifty\View\Twig();
     $twig = $engine->getRenderer();
     $designTemplateDir = 'design/production';
     if (file_exists($designTemplateDir)) {
         $this->logger->info("Compiling design templates...");
         foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($designTemplateDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
             // force compilation
             if (preg_match('/\\.(html?|twig)$/', $file)) {
                 $this->logger->info("Compiling " . $file->getPathname(), 1);
                 $twig->loadTemplate(substr($file, strlen($designTemplateDir) + 1));
             }
         }
     }
     // Compile templates from bundles
     $this->logger->info("Compiling bundle templates...");
     foreach ($kernel->bundles as $bundle) {
         $pluginDir = $bundle->locate();
         $templateDir = $bundle->getTemplateDir();
         if (!file_exists($templateDir)) {
             continue;
         }
         foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($templateDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
             // force compilation
             if (preg_match('/\\.(html?|twig)$/', $file)) {
                 $this->logger->info(FileUtils::remove_cwd($file->getPathname()), 1);
                 $twig->loadTemplate(substr($file, strlen(dirname($pluginDir)) + 1));
             }
         }
     }
     $potFile = $localeDir . DIRECTORY_SEPARATOR . 'messages.pot';
     $this->logger->info("Creating pot file: {$potFile}");
     touch($potFile);
     $scanDirs = func_get_args();
     // get paths from command-line
     if (empty($scanDirs)) {
         $scanDirs[] = PH_APP_ROOT . DIRECTORY_SEPARATOR . 'applications';
         $scanDirs[] = PH_APP_ROOT . DIRECTORY_SEPARATOR . 'bundles';
         $scanDirs[] = $kernel->getCacheDir();
     }
     $scanDirs = array_filter($scanDirs, 'file_exists');
     if (empty($scanDirs)) {
         throw new Exception("Non of existing directories");
     }
     foreach ($scanDirs as $scanDir) {
         $this->logger->info("Parsing from {$scanDir}...");
         $phpFinder = Finder::create()->files()->name('*.php')->in($scanDir);
         $phpFiles = array();
         foreach ($phpFinder as $phpFile) {
             $phpFiles[] = $phpFile;
         }
         if (empty($phpFiles)) {
             continue;
         }
         $cmd = sprintf("xgettext -j --no-location --sort-output --package-name=%s -o %s --from-code=UTF-8 --language PHP " . join(" ", $phpFiles), kernel()->applicationID, $potFile);
         $this->logger->debug($cmd, 1);
         system($cmd, $retval);
         if ($retval != 0) {
             die('xgettext error');
         }
     }
     $this->logger->info("Updating message catalog...");
     // Update message catalog
     $finder = Finder::create()->files()->name('*.po')->in($localeDir);
     foreach ($finder->getIterator() as $file) {
         $shortPathname = $file;
         $this->logger->info("Updating {$shortPathname}");
         $cmd = sprintf('msgmerge --no-location --previous --verbose --no-fuzzy-matching --update %s %s', $shortPathname, $potFile);
         $this->logger->debug($cmd);
         system($cmd, $retval);
         if ($retval != 0) {
             die('xgettext error');
         }
         /*
         $this->logger->info("Removing obsolete messages for $shortPathname");
         $msg = sprintf('msgattrib --output-file=%s --no-obsolete %s', $shortPathname, $potFile);
         $this->logger->debug($cmd);
         */
     }
     // Compile to mo files
     $finder = Finder::create()->files()->name('*.po')->in($localeDir);
     foreach ($finder->getIterator() as $file) {
         $shortPathname = $file;
         $moPathname = futil_replace_extension($shortPathname, 'mo');
         $this->logger->info("Compiling messages {$shortPathname}");
         $cmd = sprintf('msgfmt -v -o %s %s', $moPathname, $shortPathname);
         $this->logger->debug($cmd);
         system($cmd, $retval);
         if ($retval != 0) {
             die('xgettext error');
         }
     }
     // Get translations
     $this->logger->info("Compiling bundle translation...");
     $languages = kernel()->locale->available();
     $dictionary = array();
     foreach ($languages as $locale => $languageName) {
         $dictionary[$locale] = array();
     }
     foreach ($kernel->bundles as $bundle) {
         $defaultDict = $bundle->getTranslation(kernel()->locale->getDefault());
         foreach ($languages as $locale => $languageName) {
             $bundleDict = $bundle->getTranslation($locale);
             if (empty($bundleDict)) {
                 if ($defaultDict) {
                     $bundleDict = $defaultDict;
                 } else {
                     continue;
                 }
             }
             $dictionary[$locale] = array_merge($dictionary[$locale], $bundleDict);
         }
     }
     // write dictionary to po file.
     foreach ($dictionary as $lang => $subdictionary) {
         $poFile = kernel()->locale->getLocalePoFile($lang);
         $fp = fopen($poFile, 'a+');
         foreach ($subdictionary as $msgId => $msgStr) {
             $idStrs = explode("\n", $msgId);
             $msgStrs = explode("\n", $msgStr);
             fputs($fp, "msgid ");
             foreach ($idStrs as $idStr) {
                 fputs($fp, '"' . addslashes($idStr) . '"' . "\n");
             }
             fputs($fp, "msgstr ");
             foreach ($msgStrs as $msgStr) {
                 fputs($fp, '"' . addslashes($msgStr) . '"' . "\n");
             }
             fputs($fp, "\n");
         }
         fclose($fp);
         $this->logger->info("Running msguniq on {$poFile}...");
         system("msguniq {$poFile} > {$poFile}.new");
         system("mv -v {$poFile}.new {$poFile}");
     }
     $this->logger->info("Removing obsolete entry comments..");
     system("find locale -type f -iname '*.po' | xargs -I{} perl -i -pe 's/^#~ //' {}");
 }
Exemplo n.º 6
0
 public function move($newFileName = null)
 {
     if ($this->field) {
         if (!isset($_FILES[$this->field]['name'])) {
             throw new Exception("File field {$this->field}: name is empty");
         }
         if ($err = $_FILES[$this->field]['error']) {
             throw new Exception("File field {$this->field} error: {$err}");
         }
         /* process with $_FILES */
         // $_FILES['upload']['tmp_name'];
         $filename = $newFileName ? $newFileName : $_FILES[$this->field]['name'];
         $path = $this->uploadDir . DIRECTORY_SEPARATOR . $filename;
         $path = FileUtils::filename_increase($path);
         if (move_uploaded_file($_FILES['upload']['tmp_name'], $path) === false) {
             return false;
         }
         return $path;
     } else {
         $content = $this->getContent();
         $filename = $newFileName ? $newFileName : $this->getFileName();
         $path = $this->uploadDir . DIRECTORY_SEPARATOR . $filename;
         $path = FileUtils::filename_increase($path);
         if (file_put_contents($path, $content) === false) {
             return false;
         }
         return $path;
     }
 }