Esempio n. 1
0
 public static IPhpValue MakePathValueRelatedToFile(string path)
 {
     path = MakeUnixPath(path + UNIX_SEP);
     if (!path.StartsWith(UNIX_SEP))
         path = UNIX_SEP + path;
     var _FILE_ = new PhpDefinedConstExpression("__FILE__", null);
     var dirinfo = new PhpMethodCallExpression("dirname", _FILE_);
     var a2 = new PhpConstValue(path);
     var concat = new PhpBinaryOperatorExpression(".", dirinfo, a2);
     return concat;
 }
        public IPhpValue MakeGetValueExpression()
        {
            if (translationInfo == null)
                throw new ArgumentNullException("translationInfo");
            if (translationInfo.IsStatic)
                throw new NotSupportedException();
            if (translationInfo.GetSetByMethod)
            {
                var a = new PhpMethodCallExpression(translationInfo.GetMethodName);
                a.TargetObject = targetObject;
                return a;
            }
            else
            {
                var a = new PhpInstanceFieldAccessExpression(translationInfo.FieldScriptName, targetObject, null);
                return a;
            }

        }
        // Public Methods 


        public static PhpMethodCallExpression MakeConstructor(string constructedClassName, MethodTranslationInfo translationInfo, params IPhpValue[] args)
        {
            var methodCallExpression = new PhpMethodCallExpression(ConstructorMethodName, args);
            methodCallExpression.SetClassName((PhpQualifiedName)constructedClassName, translationInfo);
            return methodCallExpression;
        }
 private IPhpValue MakeEchoIfNecessary(bool isecho)
 {
     var v = _JoinArray();
     if (isecho)
         v = new PhpMethodCallExpression("echo", v);
     return v;
 }
Esempio n. 5
0
        // Private Methods 

        /*
                static IPhpStatement[] MkArray(IPhpStatement x)
                {
                    return new IPhpStatement[] { x };
                }
        */

        #endregion Static Methods

        #region Methods

        // Public Methods 




        public void Translate(AssemblySandbox sandbox)
        {
            var classes = _info.GetClasses();
            var classesToTranslate = _info.ClassTranslations.Values.Where(u => u.Type.Assembly.FullName == _info.CurrentAssembly.FullName).ToArray();
            //            classesToTranslate = (from i in _info.ClassTranslations.Values
            //                                      where i.Type.Assembly.FullName == _info.CurrentAssembly.FullName
            //                                      select this.ge.ToArray();
            var interfaces = _info.GetInterfaces();
            //     var interfacesToTranslate = info.ClassTranslations.Values.Where(u => u.Type.Assembly == info.CurrentAssembly).ToArray();
            foreach (var classTranslationInfo in classesToTranslate)
            {
                if (classTranslationInfo.Skip)
                    Debug.Write("");
                PhpClassDefinition phpClass;
                var phpModule = GetOrMakeModuleByName(classTranslationInfo.ModuleName);
                // var assemblyTI = _info.GetOrMakeTranslationInfo(_info.CurrentAssembly);

                #region Szukanie / Tworzenie PhpClassDefinition
                {
                    PhpQualifiedName phpBaseClassName;
                    #region Szukanie nazwy klasy bazowej
                    {
                        var netBaseType = classTranslationInfo.Type.BaseType;
                        if ((object)netBaseType == null || netBaseType == typeof(object))
                            phpBaseClassName = PhpQualifiedName.Empty;
                        else
                        {
                            // _state.Principles.CurrentTyp is null so we will obtain absolute name
                            phpBaseClassName = _state.Principles.GetPhpType(netBaseType, true, null); // absolute name
                            var baseTypeTranslationInfo = _state.Principles.GetOrMakeTranslationInfo(netBaseType);
                            if (baseTypeTranslationInfo.Skip)
                                phpBaseClassName = PhpQualifiedName.Empty;
                        }
                    }
                    #endregion
                    phpClass = phpModule.FindOrCreateClass(classTranslationInfo.ScriptName, phpBaseClassName);
                }
                #endregion
                _state.Principles.CurrentType = classTranslationInfo.Type;
                _state.Principles.CurrentAssembly = _state.Principles.CurrentType.Assembly;
                Console.WriteLine(classTranslationInfo.ModuleName);

                Cs.Compiler.IClassMember[] members;

                if (classTranslationInfo.Type.IsInterface)
                {
                    var sources = interfaces.Where(i => i.FullName == classTranslationInfo.Type.FullName).ToArray();
                    members = (from i in sources
                               from j in i.ClassDeclaration.Members
                               select j).ToArray();
                    {
                        var fileNames = classTranslationInfo.Type.GetCustomAttributes<RequireOnceAttribute>().Select(i => i.Filename).Distinct().ToArray();
                        if (fileNames.Any())
                        {
                            var b = fileNames.Select(u => new PhpConstValue(u)).ToArray();
                            phpModule.RequiredFiles.AddRange(b);
                        }
                    }
                }
                else
                {
                    var sources = classes.Where(i => i.FullName == classTranslationInfo.Type.FullName).ToArray();
                    members = (from i in sources
                               from j in i.ClassDeclaration.Members
                               select j).ToArray();
                    {
                        var fileNames = classTranslationInfo.Type.GetCustomAttributes<RequireOnceAttribute>().Select(i => i.Filename).Distinct().ToArray();
                        if (fileNames.Any())
                        {
                            var b = fileNames.Select(u => new PhpConstValue(u)).ToArray();
                            phpModule.RequiredFiles.AddRange(b);
                        }
                    }
                }
                #region Constructors
                {
                    var c = members.OfType<ConstructorDeclaration>().ToArray();
                    if (c.Length > 1)
                        throw new Exception("PHP supports only one constructor per class");
                    if (c.Any())
                        TranslateConstructor(phpClass, c.First());
                }
                #endregion
                #region Metody
                {
                    foreach (var methodDeclaration in members.OfType<MethodDeclaration>())
                        TranslateMethod(phpClass, methodDeclaration);
                }
                #endregion
                #region Własności
                {
                    foreach (var pDeclaration in members.OfType<CsharpPropertyDeclaration>())
                        TranslateProperty(phpClass, pDeclaration);
                }
                #endregion
                #region Pola, stałe
                {
                    foreach (var constDeclaration in members.OfType<FieldDeclaration>())
                        TranslateField(phpModule, phpClass, constDeclaration);
                }
                #endregion

                _state.Principles.CurrentType = null;
                #region Wywołanie metody defaulttimezone oraz MAIN dla PAGE
                {
                    if (classTranslationInfo.IsPage)
                    {
                        #region Timezone
                        {
                            var ati = _info.GetOrMakeTranslationInfo(_info.CurrentAssembly);
                            if (ati.DefaultTimezone.HasValue)
                            {
                                // date_default_timezone_set('America/Los_Angeles');
                                var a = new PhpValueTranslator(_state);
                                var aa = a.Visit(new ConstValue(ati.DefaultTimezone.Value));
                                var dateDefaultTimezoneSet = new PhpMethodCallExpression("date_default_timezone_set", aa);
                                phpModule.BottomCode.Statements.Add(new PhpExpressionStatement(dateDefaultTimezoneSet));
                            }
                        }
                        #endregion
                        #region Wywołanie main
                        {
                            var mti = MethodTranslationInfo.FromMethodInfo(classTranslationInfo.PageMethod, classTranslationInfo);
                            var callMain = new PhpMethodCallExpression(mti.ScriptName);
                            callMain.SetClassName(
                                classTranslationInfo.ScriptName,
                                mti
                                );
                            phpModule.BottomCode.Statements.Add(new PhpExpressionStatement(callMain));
                        }
                        #endregion
                    }
                }
                #endregion
                #region includy
                {


                    var moduleCodeRequests = new List<ModuleCodeRequest>();
                    var codeRequests = (phpModule as ICodeRelated).GetCodeRequests().ToArray();
                    {
                        var classCodeRequests = (from request in codeRequests.OfType<ClassCodeRequest>()
                                                 where request.ClassName != null
                                                 select request.ClassName.FullName)
                                    .Distinct()
                                    .ToArray();

                        foreach (var req in classCodeRequests)
                        {
                            var m = _info.ClassTranslations.Values.Where(i => i.ScriptName.FullName == req).ToArray();
                            if (m.Length != 1)
                                throw new NotSupportedException();
                            var mm = m[0];
                            if (mm.DontIncludeModuleForClassMembers)
                                continue;
                            var includeModule = mm.IncludeModule;
                            if (includeModule == null || mm.ModuleName == phpModule.Name)
                                continue;
                            var h = new ModuleCodeRequest(includeModule, "class request: " + req);
                            moduleCodeRequests.Add(h);

                        }
                    }
                    {
                        var moduleRequests = (from i in codeRequests.OfType<ModuleCodeRequest>()
                                              where i.ModuleName != null
                                              select i).Union(moduleCodeRequests).ToArray();
                        var moduleNames = (from mReq in moduleRequests
                                           where mReq.ModuleName != phpModule.Name
                                           let mName = mReq.ModuleName
                                           where mName != null
                                           select mName
                                    ).Distinct().ToArray();
                        foreach (var i in moduleNames.Where(x => !PhpCodeModuleName.IsFrameworkName(x)))
                            AppendCodeReq(i, phpModule);

                    }
                }
                #endregion
            }
            {
                var emptyModules = _modules.Where(a => a.IsEmpty).ToArray();
                foreach (var module in _modules)
                {
                    // if (module.IsEmpty) 
                }
            }
        }
Esempio n. 6
0
 private IEnumerable<IPhpStatement> ConvertRequestedToCode()
 {
     var result = new List<IPhpStatement>();
     var alreadyDefined = new List<string>();
     var style = new PhpEmitStyle();
     foreach (var item in _requiredFiles.Distinct())
     {
         var code = item.GetPhpCode(style); //rozróżniam je po wygenerowanym kodzie
         if (alreadyDefined.Contains(code))
             continue;
         alreadyDefined.Add(code);
         var req = new PhpMethodCallExpression("require_once", item);
         result.Add(new PhpExpressionStatement(req));
     }
     return result.ToArray();
 }
Esempio n. 7
0
        // Private Methods 

        private IEnumerable<IPhpStatement> ConvertDefinedConstToCode()
        {
            var result = new List<IPhpStatement>();
            var alreadyDefined = new List<string>();
            if (!_definedConsts.Any()) return result.ToArray();
            var grouped = _definedConsts.GroupBy(u => GetNamespace(u.Key)).ToArray();

            var useNamespaces = grouped.Length > 1 || grouped[0].Key != PathUtil.UNIX_SEP;
            foreach (var group in grouped)
            {
                List<IPhpStatement> container;
                if (useNamespaces)
                {
                    var ns1 = new PhpNamespaceStatement((PhpNamespace)@group.Key);
                    container = ns1.Code.Statements;
                    result.Add(ns1);
                }
                else
                    container = result;
                foreach (var item in @group)
                {
                    var shortName = GetShortName(item.Key);
                    if (alreadyDefined.Contains(item.Key))
                        continue;
                    alreadyDefined.Add(item.Key);
                    var defined = new PhpMethodCallExpression("defined", new PhpConstValue(shortName));
                    var notDefined = new PhpUnaryOperatorExpression(defined, "!");
                    var define = new PhpMethodCallExpression("define", new PhpConstValue(shortName), item.Value);
                    var ifStatement = new PhpIfStatement(notDefined, new PhpExpressionStatement(define), null);
                    container.Add(ifStatement);
                }
            }
            return result.ToArray();
        }