public function __construct($content)
 {
     $templateSettings = array('escape' => "<%-([\\s\\S]+?)%>", 'interpolate' => "<%=([\\s\\S]+?)%>", 'evaluate' => "<%([\\s\\S]+?)%>");
     $matcher = '/' . implode('|', $templateSettings) . '/';
     preg_match_all($matcher, $content, $matches, PREG_PATTERN_ORDER);
     $jsContent = "__p+='";
     foreach ($matches[0] as $key => $value) {
         if ($matches[1][$key]) {
             //escape
             $jsContent .= "'+\n((__t=(" . $matches[1][$key] . "))==null?'':_.escape(__t))+\n'";
         } else {
             if ($matches[2][$key]) {
                 //interpolate
                 $jsContent .= "'+\n((__t=(" . $matches[2][$key] . "))==null?'':__t)+\n'";
             } else {
                 if ($matches[3][$key]) {
                     //evaluate
                     $jsContent .= "';\n" . $matches[3][$key] . "\n__p+='";
                 }
             }
         }
     }
     $jsContent .= "';\n";
     $this->_results = Kwf_TrlJsParser_JsParser::parseContent($jsContent);
 }
Ejemplo n.º 2
0
 public function parse($output)
 {
     $trlElements = array();
     $fileCount = iterator_count($this->_fileFinder);
     $output->writeln('JS-Files:');
     $progress = new ProgressBar($output, $fileCount);
     foreach ($this->_fileFinder as $file) {
         $progress->advance();
         $trlElements = array_merge($trlElements, \Kwf_TrlJsParser_JsParser::parseContent($file->getContents()));
     }
     $progress->finish();
     $output->writeln('');
     return $trlElements;
 }
 /**
  * @dataProvider parseDataProvider
  */
 public function testParseData($content, $expectedTrlCalls)
 {
     $trlCalls = Kwf_TrlJsParser_JsParser::parseContent($content);
     $this->assertEquals(count($trlCalls), count($expectedTrlCalls));
     foreach ($trlCalls as $key => $trlCall) {
         //$trlCall['error']
         $this->assertEquals($trlCall['before'], $expectedTrlCalls[$key]['before']);
         $this->assertEquals($trlCall['text'], $expectedTrlCalls[$key]['text']);
         $this->assertEquals($trlCall['source'], $expectedTrlCalls[$key]['source']);
         $this->assertEquals($trlCall['type'], $expectedTrlCalls[$key]['type']);
         if (isset($expectedTrlCalls[$key]['context'])) {
             $this->assertEquals($trlCall['context'], $expectedTrlCalls[$key]['context']);
         } else {
             $this->assertEquals($trlCall['context'], null);
         }
         if (isset($expectedTrlCalls[$key]['plural'])) {
             $this->assertEquals($trlCall['plural'], $expectedTrlCalls[$key]['plural']);
         } else {
             $this->assertEquals($trlCall['plural'], null);
         }
     }
 }
Ejemplo n.º 4
0
 public function getContentsPacked()
 {
     $fileName = $this->getFileNameWithType();
     $rawContents = file_get_contents($this->getAbsoluteFileName());
     $usesUniquePrefix = strpos($rawContents, 'kwfUp-') !== false;
     $pathType = $this->getType();
     if ($pathType == 'ext2' && strpos($rawContents, 'ext2-gen') !== false) {
         $usesUniquePrefix = true;
     }
     $useTrl = $pathType != 'ext2';
     if (substr($this->getAbsoluteFileName(), 0, 24) == 'vendor/bower_components/') {
         //dependencies loaded via bower never use kwf translation system
         $useTrl = false;
     }
     if ($useTrl) {
         $useTrl = strpos($rawContents, 'trl') !== false && preg_match('#trl(c|p|cp)?(Kwf)?(Static)?\\(#', $rawContents);
     }
     $useBabel = strpos($rawContents, '"use es6";') !== false;
     if ($usesUniquePrefix || $useTrl || $useBabel) {
         //when contents contain .cssClass we must cache per app
         $buildFile = 'cache/uglifyjs/' . $fileName . '.v6' . md5(file_get_contents($this->getAbsoluteFileName()) . Kwf_Config::getValue('application.uniquePrefix'));
     } else {
         $buildFile = sys_get_temp_dir() . '/kwf-uglifyjs/' . $fileName . '.v6' . md5(file_get_contents($this->getAbsoluteFileName()));
     }
     if (!file_exists("{$buildFile}.min.js")) {
         $dir = dirname($buildFile);
         if (!file_exists($dir)) {
             mkdir($dir, 0777, true);
         }
         file_put_contents($buildFile, $rawContents);
         if ($useBabel) {
             $filter = new Kwf_Assets_Dependency_Filter_BabelJs($buildFile);
             $map = $filter->build();
             file_put_contents($buildFile, $map->getFileContents());
             //TODO: map support
         }
         $map = Kwf_Assets_Dependency_Filter_UglifyJs::build($buildFile, '/assets/' . $this->getFileNameWithType());
         $data = $map->getMapContentsData();
         $data->{'_x_org_koala-framework_masterFiles'} = array($this->getAbsoluteFileName());
         $contents = file_get_contents("{$buildFile}.min.js");
         $replacements = array();
         if ($pathType == 'ext2') {
             $replacements['../images/'] = '/assets/ext2/resources/images/';
         } else {
             if ($pathType == 'mediaelement') {
                 $replacements['url('] = 'url(/assets/mediaelement/build/';
             }
         }
         if ($usesUniquePrefix) {
             if ($pathType == 'ext2') {
                 //hack for ext2 to avoid duplicated ids getting generated
                 $uniquePrefix = Kwf_Config::getValue('application.uniquePrefix');
                 if ($uniquePrefix) {
                     $map->stringReplace('ext2-gen', $uniquePrefix . '-ext2-gen');
                 }
             }
             if (strpos($rawContents, 'kwfUp-') !== false) {
                 if (Kwf_Config::getValue('application.uniquePrefix')) {
                     $replacements['kwfUp-'] = Kwf_Config::getValue('application.uniquePrefix') . '-';
                 } else {
                     $replacements['kwfUp-'] = '';
                 }
             }
         }
         foreach ($replacements as $search => $replace) {
             $map->stringReplace($search, $replace);
         }
         if ($useTrl) {
             $trlData = array();
             foreach (Kwf_TrlJsParser_JsParser::parseContent($contents) as $trlElement) {
                 $d = Kwf_Assets_Util_Trl::getJsReplacement($trlElement);
                 $map->stringReplace($d['before'], $d['replace']);
                 $trlData[] = $d['trlElement'];
             }
             $data->{'_x_org_koala-framework_trlData'} = $trlData;
         }
         $map->save("{$buildFile}.min.js.map.json", "{$buildFile}.min.js");
         //adds last extension
     } else {
         $map = new Kwf_SourceMaps_SourceMap(file_get_contents("{$buildFile}.min.js.map.json"), file_get_contents("{$buildFile}.min.js"));
     }
     return $map;
 }