public override string GetPhpCode(PhpEmitStyle style) { var b = style != null && style.Compression != EmitStyleCompression.Beauty; var a = PhpValues.ToPhpCodeValue(Value, b); switch (a.Kind) { case PhpCodeValue.Kinds.Null: return("null"); default: return(a.PhpValue); } //if (_value == null) // return "null"; //var tt = _value.GetType(); //if (_value is string) // return PhpStringEmit(_value as string, style); //if (_value is double) // return ((double)_value).ToString(System.Globalization.CultureInfo.InvariantCulture); //if (_value is bool) // return ((bool)_value) ? "true" : "false"; //if (tt.IsEnum) //{ // var tmp = PhpValues.EnumToPhpCode(_value, ); // return tmp.Value; //} //return _value.ToString(); }
public static string CssBorder(object width, object style, object color) { width = PhpValues.ToPhpCodeValue(width, true); style = PhpValues.ToPhpCodeValue(style, true); color = PhpValues.ToPhpCodeValue(color, true); return(string.Format("{0} {1} {2}", width, style, color)); }
// Public Methods public static IPhpValue GetValueForExpression(IPhpValue phpTargetObject, string valueAsString) { valueAsString = (valueAsString ?? "").Trim(); if (valueAsString.ToLower() == "this") { return(phpTargetObject); } if (valueAsString == "false") { return(new PhpConstValue(false)); } if (valueAsString == "true") { return(new PhpConstValue(true)); } if (int.TryParse(valueAsString, out var i)) { return(new PhpConstValue(i)); } if (double.TryParse(valueAsString, NumberStyles.Float, CultureInfo.InvariantCulture, out var d)) { return(new PhpConstValue(d)); } { if (PhpValues.TryGetPhpStringValue(valueAsString, out var x)) { return(new PhpConstValue(x)); } } throw new Exception(string.Format("bald boa, Unable to convert {0} into php value", valueAsString)); }
public static string TagOpenOpen(object tagname, params object[] atts) { PhpStringBuilder sb = new PhpStringBuilder(); sb.Add("<" + PhpValues.ToPhpCodeValue(tagname)); for (int i = 1; i < atts.Length; i += 2) { sb.AddFormat(" {0}=\"{1}\"", atts[i - 1], atts[i]); } // sb.Add(">"); return(sb.ToString()); }
public static string Css(params object[] atts) { StringBuilder sb = new StringBuilder(); for (int i = 1; i < atts.Length; i += 2) { var k = PhpValues.ToPhpCodeValue(atts[i - 1]); var v = PhpValues.ToPhpCodeValue(atts[i]); sb.AppendFormat("{0}: {1};", k, v); } return(sb.ToString()); }
private EchoEmitItem[] GetEchoItems(PhpEmitStyle style) { var values = new List <IPhpValue>(); #region Przygotowanie listy elementów do wyświetlenia { var methodCall = _expression as PhpMethodCallExpression; if (methodCall == null) { return (null); } if (methodCall.CallType != MethodCallStyles.Procedural || methodCall.Name != "echo") { return(null); } foreach (var xx in methodCall.Arguments) { values.AddRange(ExpressionSimplifier.ExplodeConcats(xx, ".")); } values = values.Select(AAA).ToList(); #region Łączenie const string for (var i = 1; i < values.Count; i++) { var a1 = values[i - 1]; var a2 = values[i]; if (!(a1 is PhpConstValue) || !(a2 is PhpConstValue)) { continue; } var b1 = (a1 as PhpConstValue).Value; var b2 = (a2 as PhpConstValue).Value; var c1 = PhpValues.ToPhpCodeValue(b1); var c2 = PhpValues.ToPhpCodeValue(b2); if (c1.Kind != PhpCodeValue.Kinds.StringConstant || c1.Kind != PhpCodeValue.Kinds.StringConstant) { continue; } values[i - 1] = new PhpConstValue((string)c1.SourceValue + (string)c2.SourceValue); values.RemoveAt(i); i--; } #endregion } #endregion { IPhpValue echoArguments = null; Action <IPhpValue> vv = u => { // ReSharper disable once AccessToModifiedClosure echoArguments = echoArguments == null ? u : new PhpBinaryOperatorExpression(".", echoArguments, u); }; var result = new List <EchoEmitItem>(); foreach (var value in values) { if (value is PhpConstValue) { var constValue = (value as PhpConstValue).Value; var constStringValue = constValue as string; if (constStringValue != null) { #region Const-string var lines = SplitToLines(constStringValue); foreach (var i in lines) { if (i.EndsWith("\r\n")) { vv(new PhpConstValue(i.Substring(0, i.Length - 2))); vv(new PhpDefinedConstExpression("PHP_EOL", null)); // ReSharper disable once PossibleNullReferenceException result.Add(new EchoEmitItem(echoArguments.GetPhpCode(style), false)); echoArguments = null; } else { vv(new PhpConstValue(i)); } } continue; #endregion } } vv(value); } if (echoArguments != null) { result.Add(new EchoEmitItem(echoArguments.GetPhpCode(style), false)); } return(result.ToArray()); } }
public static string TagBound(object tagname, object inside, params object[] atts) { return(TagOpen(tagname, atts) + PhpValues.ToPhpCodeValue(inside) + TagClose(tagname)); }
public static string TagClose(object tagname) { return(string.Format("</{0}>", PhpValues.ToPhpCodeValue(tagname))); }
protected override IPhpValue VisitPhpBinaryOperatorExpression(PhpBinaryOperatorExpression node) { switch (node.Operator) { case ".": { var _left = Simplify(node.Left); var _right = Simplify(node.Right); var n = new PhpBinaryOperatorExpression(node.Operator, _left, _right); var c = ExplodeConcats(n, ".").ToList(); for (var i = 1; i < c.Count; i++) { var L = c[i - 1]; var R = c[i]; if (L is PhpConstValue && R is PhpConstValue) { var LValue = (L as PhpConstValue).Value; var RValue = (R as PhpConstValue).Value; if (LValue is string && RValue is string) { c[i - 1] = new PhpConstValue((string)LValue + (string)RValue); c.RemoveAt(i); i--; continue; } var LCode = PhpValues.ToPhpCodeValue(LValue); var RCode = PhpValues.ToPhpCodeValue(RValue); string left, right; if (LCode.TryGetPhpString(out left) && RCode.TryGetPhpString(out right)) { c[i - 1] = new PhpConstValue(left + right); c.RemoveAt(i); i--; continue; } var msg = string.Format("left={0}, right={1} '{2}+{3}'", LValue, RValue, LValue == null ? null : LValue.GetType().FullName, RValue == null ? null : RValue.GetType().FullName); #if DEBUG throw new NotImplementedException(msg); #else Console.WriteLine(msg); Console.WriteLine(L.GetPhpCode(null)); Console.WriteLine(R.GetPhpCode(null)); continue; #endif } } var result = c[0]; if (c.Count > 1) { foreach (var x2 in c.Skip(1)) { result = new PhpBinaryOperatorExpression(".", result, x2); } } return(ReturnSubst(node, result)); } case "|": { // var aLeft = node.Left as PhpConstValue; // var aRight = node.Right as PhpConstValue; // if (aLeft != null && aRight != null && aLeft.Value!=null && aRight.Value!=null) // { // var typeLeft = aLeft.Value.GetType(); // var typeRight = aRight.Value.GetType(); // if (typeLeft.IsEnum && typeLeft == typeRight) // { // var leftValue = (int)aLeft.Value; // var rightValue = (int)aRight.Value; // var c = leftValue | rightValue; // var dd = PhpCodeValue.FromInt(c, true); // var d = new PhpConstValue(c, false); // return d; // } // } } break; } return(node); }
protected override IPhpValue VisitClassFieldAccessExpression(ClassFieldAccessExpression src) { var tmp = state.Principles.NodeTranslators.Translate(state, src); if (tmp != null) { return(SimplifyPhpExpression(tmp)); } var isStatic = src.IsStatic; var member = src.Member; var memberName = member.Name; var memberDeclaringType = member.DeclaringType; { var tInfo = state.Principles.GetOrMakeTranslationInfo(src.Member); if (tInfo.IsDefinedInNonincludableModule) { var b = state.Principles.GetTi(state.Principles.CurrentType, true); if (tInfo.IncludeModule != b.ModuleName) { throw new Exception( string.Format( "Unable to reference to field {1}.{0} from {2}.{3}. Containing module is page and cannot be included.", memberName, memberDeclaringType == null ? "?" : (memberDeclaringType.FullName ?? memberDeclaringType.Name), state.Principles.CurrentType.FullName, state.Principles.CurrentMethod )); } } var fieldDeclaringType = memberDeclaringType; if (fieldDeclaringType == null) { throw new Exception("fieldDeclaringType"); } state.Principles.GetTi(fieldDeclaringType, false); { if (fieldDeclaringType.IsEnum) { if (!isStatic) { throw new NotSupportedException(); } var asDefinedConstAttribute = member.GetCustomAttribute <AsDefinedConstAttribute>(); if (asDefinedConstAttribute != null) { var definedExpression = new PhpDefinedConstExpression(asDefinedConstAttribute.DefinedConstName, tInfo.IncludeModule); return(SimplifyPhpExpression(definedExpression)); } var renderValueAttribute = member.GetCustomAttribute <RenderValueAttribute>(); if (renderValueAttribute != null) { if (PhpValues.TryGetPhpStringValue(renderValueAttribute.Name, out var strCandidate)) { var valueExpression = new PhpConstValue(strCandidate); #if DEBUG { var a1 = renderValueAttribute.Name.Trim(); var a2 = valueExpression.ToString(); if (a1 != a2) { throw new InvalidOperationException(); } } #endif return(SimplifyPhpExpression(valueExpression)); } else { var valueExpression = new PhpFreeExpression(renderValueAttribute.Name); return(SimplifyPhpExpression(valueExpression)); } } { // object v1 = ReadEnumValueAndProcessForPhp(member); var v1 = member.GetValue(null); var g = new PhpConstValue(v1); return(SimplifyPhpExpression(g)); } //throw new NotSupportedException(); } } var principles = state.Principles; switch (tInfo.Destination) { case FieldTranslationDestionations.DefinedConst: if (!member.IsStatic) { throw new NotSupportedException("Unable to convert instance field into PHP defined const"); } if (tInfo.IsScriptNamePhpEncoded) { throw new Exception("Encoded php values are not supported"); } var definedExpression = new PhpDefinedConstExpression(tInfo.ScriptName, tInfo.IncludeModule); return(SimplifyPhpExpression(definedExpression)); case FieldTranslationDestionations.GlobalVariable: if (!member.IsStatic) { throw new NotSupportedException( "Unable to convert instance field into PHP global variable"); } if (tInfo.IsScriptNamePhpEncoded) { throw new Exception("Encoded php values are not supported"); } var globalVariable = PhpVariableExpression.MakeGlobal(tInfo.ScriptName); return(SimplifyPhpExpression(globalVariable)); case FieldTranslationDestionations.JustValue: if (!member.IsStatic) { throw new NotSupportedException("Unable to convert instance field into compile-time value"); } var constValue = member.GetValue(null); var phpConstValue = new PhpConstValue(constValue, tInfo.UsGlueForValue); return(SimplifyPhpExpression(phpConstValue)); case FieldTranslationDestionations.NormalField: if (tInfo.IsScriptNamePhpEncoded) { throw new Exception("Encoded php values are not supported"); } var rr = new PhpClassFieldAccessExpression { FieldName = tInfo.ScriptName, IsConst = tInfo.Destination == FieldTranslationDestionations.ClassConst }; rr.SetClassName( principles.GetPhpType(memberDeclaringType, true, principles.CurrentType), principles.GetOrMakeTranslationInfo(memberDeclaringType) ); return(SimplifyPhpExpression(rr)); case FieldTranslationDestionations.ClassConst: if (tInfo.IsScriptNamePhpEncoded) { throw new Exception("Encoded php values are not supported"); } rr = new PhpClassFieldAccessExpression { FieldName = tInfo.ScriptName, IsConst = true }; rr.SetClassName( principles.GetPhpType(memberDeclaringType, true, principles.CurrentType), principles.GetOrMakeTranslationInfo(memberDeclaringType)); return(SimplifyPhpExpression(rr)); default: throw new NotSupportedException(string.Format( "Unable to translate class field with destination option equal {0}", tInfo.Destination)); } } }